2

I am having this Button in my XML:

<Button
     android:id="@+id/button_message_me"
     style="@style/Widget.AppCompat.Button.Borderless.Colored"
     android:layout_width="0dp"
     android:layout_weight="0.25"
     android:layout_height="wrap_content"
     android:adjustViewBounds="true"
     android:background="@drawable/ic_chat_white_48dp"
     android:onClick="clickMessageMe"/>

However, I want to change the color of ic_chat_white_48dp from white to blue. How to do change?

What I already tried so far:

After reading this post, I tried using android:tint="@color/blue" but it did not work.

So, thinking that I might need to use ImageButton instead of Button (as mentioned in the answer) I replaced Button with ImageButton in my XML but I landed up getting exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{chat.knowme.knowme/chat.knowme.knowme.ShowProfileActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button

Any help would be really appreciated. Many thanks!

Update: The crash was because I was casting the ImageButton to Buttonin my source. I fixed it and now doesn't crash anymore (thanks to Patel Pinkal for his answer).

However, color still remain unchanged even with ImageButton

Community
  • 1
  • 1
Atul
  • 3,778
  • 5
  • 47
  • 87
  • post your `Button` initialize code – Patel Pinkal Jan 05 '17 at 06:17
  • There is no initialize code as such for this button. I've used `(Button) findViewById(R.id.button_message_me);` whenever I wanted to show/hide this button. And have written `clickMessageMe` method for the button. However, crash occurs as soon as I launch the activity. – Atul Jan 05 '17 at 06:22
  • What you are using `ImageButton` or `AppCompatImageButton`? – Patel Pinkal Jan 05 '17 at 06:24

2 Answers2

3

You are trying to initialize Button instead of AppCompatImageButton. Just replace like this

 AppCompatImageButton appCompatImageButton = (AppCompatImageButton) findViewById(R.id.appCompatImageButton);

Instead of :

 Button button = (Button) findViewById(R.id.button);

AND

if you want to change color using Button or AppCompatImageButton, you have to change like

android:backgroundTint="@Color/yourColor"

Instead of

android:tint="@Color/yourColor"

Patel Pinkal
  • 8,984
  • 4
  • 28
  • 50
3

I also face this problem. Finally found the solution, using Android Material Design Icon Generator Plugin. Through this you can generate icons in various colors and sizes.

Step1: File-> Settings->Plugin

Step2: Search for android material icon, you will get Android Material Design Icon Generator Plugin in the list

Step3: Select that and click Apply

Once its installed, Right click on your project from studio->new->Material design icon or ctrl+alt+M

It will be useful for developers.

Make it Simple
  • 1,832
  • 5
  • 32
  • 57
  • That is nice suggestion indeed. If I couldn't change color through code I'll surely go for this. – Atul Jan 05 '17 at 07:42