Изменить цвет изображения Radiobutton, отображаемого при нажатии/нажатии

У меня есть переключатель, который имеет как текст, так и изображение. Я пытаюсь изменить цвет изображения при нажатии кнопки, применяя цвет поверх него, как оттенок. Я пытался использовать android:state_checked="true", но это не позволяет мне применять цвет поверх изображения.

Я также пытался сделать это программно с помощью:

RadioButton radioButtonShare= (RadioButton) findViewById(R.id.image_share);
radioButtonShare.getButtonDrawable().setColorFilter(getResources().getColor(R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP);

Но он вылетает с нулевым исключением...

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference

Любезно помочь

Переключатель

<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" />


person Bmbariah    schedule 21.12.2016    source источник
comment
если вы говорите об исключении нулевого указателя, проверьте R.id.image_share, находится ли он в текущем файле макета   -  person Mr.Popular    schedule 21.12.2016
comment
@Mr.Popular Это правильный файл ...   -  person Bmbariah    schedule 21.12.2016


Ответы (1)


Я думаю, что приведенный ниже код поможет вам:

Вставьте приведенный ниже код в файл style.xml.

style.xml

<style name="radionbutton"
    parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
    <item name="android:button">@android:color/transparent</item>
    <item name="android:drawableTop">@drawable/radiobutton_drawable</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>

Создайте файл radiobutton_drawable.xml в папке с возможностью рисования и вставьте приведенный ниже код в файл radiobutton_drawable.xml.

radiobutton_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/radio_uncheck" android:state_checked="false" android:state_focused="true"/>
    <item android:drawable="@drawable/radio_uncheck" android:state_checked="false" android:state_focused="false"/>
    <item android:drawable="@drawable/radio_check" android:state_checked="true" android:state_focused="true"/>
    <item android:drawable="@drawable/radio_check" android:state_checked="true" android:state_focused="false"/>
</selector>

Вставьте приведенный ниже код в файл activity_main.xml.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radio2"
            style="@style/radionbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="male" />

        <RadioButton
            style="@style/radionbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/radio2`enter code here`"
            android:text="female" />
    </RadioGroup>
</RelativeLayout>
person Dimple Dobariya    schedule 22.12.2016