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

Asked

Viewed 456 times

0

I would like besides my button to have a lower edge more than had the button states pressed, not pressed would be another color...

I am trying to do all this in my drawable xml to set a background for my button. It follows 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 that?

2 answers

0

One way would be to do programmatically. Example:

private boolean isChecked

Change the 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;
            }


        }
    });

0

To add the edge on the button has a tag called Stroke and for there to be only the bottom edge, you need to negate the top, right and left attributes of the item tag.

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.