Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a button border color programmatically in Android?

I want to have a button in Android with differnt color for button border.

        Button Bt = new Button(this);
        Bt.setId(i+1);
        Bt.setBackgroundColor(getResources().getColor(R.color.white)) ;
        Bt.setText(restList.get(i));
        Bt.setLayoutParams(params3);
        Bt.setTextColor(Color.parseColor("gray"));
        layout.addView(Bt);

How can I do this programmatically?

like image 564
hhl Avatar asked Dec 03 '15 20:12

hhl


1 Answers

 yourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShapeDrawable shapedrawable = new ShapeDrawable();
                shapedrawable.setShape(new RectShape());
                shapedrawable.getPaint().setColor(Color.RED);
                shapedrawable.getPaint().setStrokeWidth(10f);
                shapedrawable.getPaint().setStyle(Style.STROKE);     
                yourButton.setBackground(shapedrawable);
            }
        });

try this but i am not sure 100%

like image 78
Tano Avatar answered Oct 01 '22 23:10

Tano