I have a radio button that has both text and a image. I am trying to change the color of the image when the button is clicked by applying a color on top of it like a tint. I tried using android:state_checked="true"
but it doesn't allow me to apply color on top of the image.
I also tried to do it programmatically with:
RadioButton radioButtonShare= (RadioButton) findViewById(R.id.image_share);
radioButtonShare.getButtonDrawable().setColorFilter(getResources().getColor(R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP);
But it crashes with nullpointexception...
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
Kindly assist
RadioButton
<RadioButton
android:id="@+id/image_share"
style="@style/style_radiobutton"
android:layout_height="match_parent"
android:drawableTop="@drawable/selector_image_share"
android:text="@string/edit_share" />
@drawable/selector_image_share
<item android:drawable="@drawable/viewer_ic_share" android:state_checked="true" />
<item android:drawable="@drawable/viewer_ic_share" android:state_pressed="true" />
<item android:drawable="@drawable/viewer_ic_share" />
I think below code help you:
Paste below code in your style.xml file
style.xml
Create radiobutton_drawable.xml file in your drawable folder and Paste below code in radiobutton_drawable.xml file.
radiobutton_drawable.xml
Paste below code in activity_main.xml file.
activity_main.xml