简体   繁体   中英

How can I modify how a button looks while pressed in Android Studio

I have been experimenting with button attributes and what I can do with buttons in general. I wanted to modify the way a button looks when clicked. I created three drawable resource files, two for each button states(pressed or default) and one for state list to pass to the button in the main activity xml. However this does not work as the button stays the same when I click it.

custom_button_pressed.xml file code

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

         <solid android:color="@color/button_color_pressed"/>
         <corners android:radius="30dp"/>
    </shape>

custom_button.xml file (default state file) code:

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <corners android:radius="30dp"/>
        <solid android:color="@color/button_color"/>
    </shape>

custom_button_full.xml file code:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:drawable="@drawable/custom_button_pressed"
            android:state_pressed="true" />
        <item android:drawable="@drawable/custom_button"/>
    </selector>

code fragment for the button in the main xml file:

<Button
        android:id="@+id/buttonAboutApp"
        android:layout_width="160dp"
        android:layout_height="105dp"
        android:layout_marginEnd="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginBottom="44dp"
        android:background="@drawable/custom_button_full"
        android:drawableLeft="@mipmap/about_app_icon"
        android:text="@string/About_app"
        android:textAllCaps="false"
        android:textColor="@color/app_background"
        app:backgroundTint="@color/button_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

what am I doing wrong ? Thank you in advance

If you are using a version of the Material Components library less than 1.2.0, the android:background attribute is not supported by MaterialButton . If your app/activity uses a MaterialComponents theme, any <Button> tag will be automatically inflated as a MaterialButton instance, so your custom background won't work.

Since you're trying to override the button's appearance anyway, you probably want just a basic android.widget.Button instead of a MaterialButton . You can achieve this by specifying the fully-qualified name in the view tag of your layout file:

<android.widget.Button
    android:background="@drawable/custom_button_full"
    .../>

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