Android Button Selector

Asked 1 years ago, Updated 1 years ago, 72 views

The source below is usually a button selector that appears gray when the red button is pressed. Um... Can I change the size of the text and the color of the text when the button is pressed?

<item android:state_pressed="true" >         
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="2dp" android:color="@color/black" />
        <solid android:color="@color/grey"/>
        <padding android:left="5dp" android:top="2dp" 
            android:right="5dp" android:bottom="2dp" /> 
        <corners android:radius="5dp" /> 
    </shape>    
</item>

<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="2dp" android:color="@color/black" />
        <solid android:color="#FF6699"/>
        <padding android:left="5dp" android:top="2dp" 
            android:right="5dp" android:bottom="2dp" /> 
        <corners android:radius="5dp" /> 
    </shape>
</item>

android selector

2022-09-22 21:33

1 Answers

You need to set the button selector like the layout below.

<Button
     android:id="@+id/button1"
     android:background="@drawable/the name of the selector to set"
     android:layout_width="200dp"
     android:layout_height="126dp"
     android:text="Hello" />

And please add a selector to the drawable folder. Button selector name.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_normal"></item>

</selector>

If you look at the item section above, there are three drawables, and you can write button_effect for each.


2022-09-22 21:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.