How to set a background color and borders for my button besides the states pressed and not pressed on the button with?

0

I'd like my button to have a lower border plus it had the states of the sample button pressed, not pressed would be another color ...

I'm trying to do all this in my drawable xml to set a background for my button. Here is the drawable xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
        <solid android:color="#362E2E" />
    </shape>
</item>
<item android:bottom="3dp" >
    <shape android:shape="rectangle">
        <solid android:color="#2a5aa4" />
    </shape>
</item>
<selector >

    <!-- pressed -->
    <item android:drawable="@android:color/transparent" android:state_pressed="true"/>

    <!-- focused -->
    <item android:drawable="#362E2E" android:state_focused="true"/>

    <!-- default -->
    <item android:drawable="#362E2E"/>

</selector>

How can I do this?

    
asked by anonymous 02.10.2017 / 02:14

2 answers

0

One way would be to do programmatically. Example:

private boolean isChecked

Change button color when clicked

    button_1 = (Button) view.findViewById(R.id.button_1);
    button_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(isChecked == true){
                button_1.setBackgroundColor(getResources().getColor(R.color.color_1));
                isChecked = false;
            }else {
                button_1.setBackgroundColor(getResources().getColor(R.color.color_2));
                isChecked = true;
            }


        }
    });
    
02.10.2017 / 02:51
0

To add the border to the button has a tag called stroke and to have only the bottom border, you have to negate the top, right and left attributes of the item tag.

The drawable would look like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <layer-list>
        <item android:bottom="-10dp" android:left="-10dp" android:right="-10dp">
            <shape android:shape="rectangle">
                <solid android:angle="270" android:color="#a0e1" />

                <stroke android:width="10dp" android:color="#5c3708" />
            </shape>
        </item>

    </layer-list>
</item>
<item android:state_enabled="true">
    <layer-list>
        <item android:left="-10dp" android:right="-10dp" android:top="-10dp">
            <shape android:shape="rectangle">
                <solid android:angle="270" android:color="#a0a67637" />

                <stroke android:width="10dp" android:color="#5c3708" />
            </shape>
        </item>
    </layer-list>
</item>
</selector>
    
02.10.2017 / 03:07