set View Rounded Corners - Android User Interface

Android examples for User Interface:View Property

Description

set View Rounded Corners

Demo Code


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

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

import android.view.View;

public class Main {
    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    public static void setRoundedCorners(View view, float radius,
            int backgroundColor) {
        //Calculate the radius
        if (radius <= 0) {
            float width = view.getWidth();
            float height = view.getHeight();
            radius = Math.min(width, height) / 2.0f;
            if (radius <= 0)
                return;
        }//w  w w .java 2s  .co 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);
        }
    }

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    public static void setRoundedCorners(View view, float radius,
            int borderWidth, int backgroundColor, int borderColor) {
        //Calculate the radius
        if (radius <= 0) {
            float width = view.getWidth();
            float height = view.getHeight();
            radius = Math.min(width, height) / 2.0f;
            if (radius <= 0)
                return;
        }

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

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

Related Tutorials