Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

public class Main {
    /**
     * set btn selector with corner drawable for special position
     */
    public static StateListDrawable btnSelector(float radius, int normalColor, int pressColor, int postion) {
        StateListDrawable bg = new StateListDrawable();
        Drawable normal = null;
        Drawable pressed = null;

        if (postion == 0) {// left btn
            normal = cornerDrawable(normalColor, new float[] { 0, 0, 0, 0, 0, 0, radius, radius });
            pressed = cornerDrawable(pressColor, new float[] { 0, 0, 0, 0, 0, 0, radius, radius });
        } else if (postion == 1) {// right btn
            normal = cornerDrawable(normalColor, new float[] { 0, 0, 0, 0, radius, radius, 0, 0 });
            pressed = cornerDrawable(pressColor, new float[] { 0, 0, 0, 0, radius, radius, 0, 0 });
        } else if (postion == -1) {// only one btn
            normal = cornerDrawable(normalColor, new float[] { 0, 0, 0, 0, radius, radius, radius, radius });
            pressed = cornerDrawable(pressColor, new float[] { 0, 0, 0, 0, radius, radius, radius, radius });
        } else if (postion == -2) {// for material dialog
            normal = cornerDrawable(normalColor, radius);
            pressed = cornerDrawable(pressColor, radius);
        }

        bg.addState(new int[] { -android.R.attr.state_pressed }, normal);
        bg.addState(new int[] { android.R.attr.state_pressed }, pressed);
        return bg;
    }

    public static Drawable cornerDrawable(final int bgColor, float cornerradius) {
        final GradientDrawable bg = new GradientDrawable();
        bg.setCornerRadius(cornerradius);
        bg.setColor(bgColor);

        return bg;
    }

    public static Drawable cornerDrawable(final int bgColor, float[] cornerradius) {
        final GradientDrawable bg = new GradientDrawable();
        bg.setCornerRadii(cornerradius);
        bg.setColor(bgColor);

        return bg;
    }

    public static Drawable cornerDrawable(final int bgColor, float[] cornerradius, int borderwidth,
            int bordercolor) {
        final GradientDrawable bg = new GradientDrawable();
        bg.setCornerRadii(cornerradius);
        bg.setStroke(borderwidth, bordercolor);
        bg.setColor(bgColor);

        return bg;
    }
}