Atul
Atul

Reputation: 4320

How to change color of material design icon used in Button in Android

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

Upvotes: 2

Views: 3278

Answers (2)

Make it Simple
Make it Simple

Reputation: 1882

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.

Upvotes: 3

Patel Pinkal
Patel Pinkal

Reputation: 9512

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"

Upvotes: 3

Related Questions