简体   繁体   中英

android change button stroke color

I have a button that on start has a white border. This is set in the button def by applying a xml background using the following:

android:background="@drawable/butt1"

The butt1 def is as follows:

<shape xmlns:android="http://schemas.android.com/apk/res/android"     
android:shape="rectangle" >
<corners
android:topLeftRadius="30dp"
android:topRightRadius="30dp"
android:bottomLeftRadius="30dp"
android:bottomRightRadius="30dp"
/>

<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>

<stroke
android:width="4dp"
android:color="#FFFFFF"
/>
</shape>

This works fine as long as I have a dark background for the activity

My app can change backgrounds and when I change to a light background I cant see the button because of the stroke color being white (#FFFFFF)

How do I change the color of the border to say black(#000000) if the background is changed to white

I can change the text color to black using

Button view5 = (Button) findViewById(R.id.sett);  
view5.setTextColor(Color.parseColor("#000000"));

but cant work out how to apply a new xml background style

Any help appreciated

mark

You can create a second butt1 xml for black border and when the background color change to light then you can call view5.setBackgroundResource(R.drawable.butt1_black_border); to change the background of the button using the xml with black border

EDIT:

view5.setBackgroundResource(R.drawable.butt1_black_border);

How do I change the color of the border to say black(#000000)

Alter view's drawable:

Button view5 = (Button) findViewById(R.id.sett); 
ShapeDrawable gradientDrawable = (ShapeDrawable)view5.getBackground(); 
gradientDrawable.setStroke(2, color); 

See setStroke() docs .

you can use on focus change listener & set background drawable.There will be two drawable with different stroke color by checking boolean value set button background -

view5.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM