set View Rounded Corners With Radius - Android User Interface

Android examples for User Interface:View Property

Description

set View Rounded Corners With Radius

Demo Code


//package com.java2s;
import android.annotation.SuppressLint;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;

import android.view.View;

public class Main {
    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    public static void setRoundedCornersWithRadius(View view, float radius,
            int backgroundColor) {

        //Try to obtain the current background color of the view
        //Note: only works with views with simple color background
        if (backgroundColor <= 0) {
            Drawable currentBackground = view.getBackground();
            if (currentBackground instanceof ColorDrawable) {
                backgroundColor = ((ColorDrawable) currentBackground)
                        .getColor();/*ww  w.  j a  va 2s .c  o m*/
            }
        }

        //Construct a rounded corner shape with that color
        GradientDrawable shape = new GradientDrawable();
        shape.setCornerRadius(radius);
        shape.setColor(backgroundColor);

        if (Build.VERSION.SDK_INT > 16) {
            view.setBackground(shape);
        } else {
            view.setBackgroundDrawable(shape);
        }
    }
}

Related Tutorials