Home > database >  How to change the color of a button's icon when the button itself is being held in Android Stud
How to change the color of a button's icon when the button itself is being held in Android Stud

Time:11-09

I had a problem making a button getting darker when it was being held and, after some help, it worked. But it's only the button's background and I think it would look better if the icon inside of it would darken too. Here's an example:

Not held

Held

As you can see the button gets darker, but not the icon inside of it.

Here's the code for the ImageButton, whose src indicates the image which is already from Android Studio:

<ImageButton
        android:id="@ id/call_button"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginStart="248dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/my_button"
        android:contentDescription="@string/call"
        android:minWidth="48dp"
        android:src="@android:drawable/stat_sys_phone_call"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@id/iv_image"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.466" />

And the code for the "@drawable/my_button", which ONLY changes the background:

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

Are the changes made to the icon, because technically the button is the one which is gonna be held, not the icon, meaning that there's probably gonna be a connection between two files like this, where if one is held the other is too (?).

If there's an easier solution like applying a filter to change the color of the entire thing, then that would make it much easier.

CodePudding user response:

Add this to your ImageButton

app:tint="@color/your_color"

CodePudding user response:

Add this to your button's attributes, it helps to show an overlay background color when item or layout is clicked.

android:background="?android:attr/selectableItemBackground"
  • Related