カテゴリー
Android Java

Custom Button in Android

// activity

public class MainActivity extends AppCompatActivity {

    Button button1;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().hide();

        RelativeLayout rootLinearLayout = new RelativeLayout(this);
        rootLinearLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
        rootLinearLayout.setBackgroundColor(Color.CYAN);
        setContentView(rootLinearLayout);


        OvalShape ovalShape = new OvalShape();
        ShapeDrawable shapeDrawable = new ShapeDrawable(ovalShape);
        shapeDrawable.getPaint().setColor(Color.GRAY); // Change this to your desired color.

        button1 = new Button(this);
        button1.setText("Task1");
        int button1Id = View.generateViewId();
        button1.setId(button1Id);

        int size = 200; // Change this to your desired size.
        RelativeLayout.LayoutParams buttonLP = new RelativeLayout.LayoutParams(size, size);
        buttonLP.addRule(RelativeLayout.CENTER_IN_PARENT);

        RelativeLayout.LayoutParams buttonLP1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        buttonLP1.addRule(RelativeLayout.CENTER_IN_PARENT);
        button1.setLayoutParams(buttonLP);
        button1.setOnClickListener(new OnButtonClicked());

        button1.setOnTouchListener(new OnButtonTouched(Color.GRAY, Color.BLUE));

        button1.setBackground(shapeDrawable);
        rootLinearLayout.addView(button1);
    }
}

// custom onTouchListener

public class OnButtonTouched implements View.OnTouchListener {
    private final int normalColor;
    private final int pressedColor;

    private static final String TAG = "ButtonClicked";

    public OnButtonTouched(int normalColor, int pressedColor) {
        this.normalColor = normalColor;
        this.pressedColor = pressedColor;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d(TAG, "in touched");
        if (!(v.getBackground() instanceof ShapeDrawable)) {
            return false; // Return false if the background is not of the expected type
        }

        ShapeDrawable shapeDrawable = (ShapeDrawable) v.getBackground();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // Button pressed: change color
                shapeDrawable.getPaint().setColor(pressedColor);
                v.invalidate(); // Force the button to be redrawn
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // Button released or touch cancelled: revert to original color
                shapeDrawable.getPaint().setColor(normalColor);
                v.invalidate(); // Force the button to be redrawn
                break;
        }
        return false; // Return false to indicate not to consume touch events, let them propagate so the onClick events will fire.
    }
}