0

So I'm having a little bit of trouble with an easy code modification: I can't seem to make it so a button's color changes after being held down, even after using a Color Resource File. When I do, it only changes the background and the icon disappears.

Here's my button's code:

<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="#1072E1"
        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" />

So is there a simple way to change the background color of it? As you can see the background color set to it is #1072E1, but I want to make it more dynamic and change it so when someone holds the button down the color changes and goes back to normal after it's released (button's color could be #0E63C4, for example).

It's so simple, yet I'm having trouble with it. I'll give further details if answers come in.

1 Answers1

0

You need to set different colors for pressed state of button as shown in this SO answer

akhil nair
  • 1,371
  • 1
  • 11
  • 19
  • Ok, that answer fixed it, but do I need to have the onCreate method still, or is it a personal thing for that user? Also, how would I apply those different colors to the icon "inside" the button? Should I do the same exact thing? – André_Almeida Nov 05 '21 at 23:39
  • @André_Almeida The `onCreate` method has nothing to do with the color state of a button, you can override the `onCreate` method if you have the need which I don't know. To apply those colors to a button just do the same thing as on the answer. – akhil nair Nov 06 '21 at 21:07
  • I don't want just the background color of the button to change though, I want the inside of it, because there's also an icon. Do I connect a Color Resource File to the ImageButton in total, if that's even possible? – André_Almeida Nov 08 '21 at 10:21
  • If you'd like to respond, I made a new question just for it with more details: https://stackoverflow.com/questions/69882087/how-to-change-the-color-of-a-buttons-icon-when-the-button-itself-is-being-held – André_Almeida Nov 08 '21 at 10:36
  • @André_Almeida Wasn't this the answer to your original problem you asked here? – akhil nair Nov 08 '21 at 15:36
  • Not really, because this answer only helped me with the background color of the button changing when being held. This new question I have is to change the ICON inside the button as well. I just didn't know if it was the same procedure because the only android states I've found were "state_pressed", "state_focused" and then the drawable which is the normal one. The icon itself isn't being held, so I'd figure it would have some connection to the xml file with those states, so that if the button is being held, not only will the background change color but so will the icon. – André_Almeida Nov 08 '21 at 18:56