1

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

user3422687
  • 229
  • 1
  • 8
  • 27

3 Answers3

1

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);

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Thanks for this but when i set it i get an error "The method setBackgroundResource(int) in the type View is not applicable for the arguments (Drawable)" – user3422687 Aug 09 '14 at 08:27
1

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

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

            }
        });
yuva ツ
  • 3,707
  • 9
  • 50
  • 78