Change Android settings icon with SwitchPreference setting

I have an application SwitchPreferencein my Android application that can be installed on " on" or " off". I have an icon for this SwitchPreference. Xml code below

<SwitchPreference
    android:title="@string/psm_pinterest"
    android:defaultValue="false"
    android:key="@string/pref_social_pinterest"
    android:icon="@drawable/pinterest_bw"
    android:summaryOn="@string/psm_pinterest_summary_on"
    android:summaryOff="@string/psm_pinterest_summary_off"/>

However, I also want to iconchange how and when it SwitchPreferencewas changed. Is there any way to do this through XML? I tried to create an drawablexml using the state settings, but none of the states are called when I change the parameter SwitchPreference.

Now I am processing this programmatically, but I am considering a more efficient way to do this using XML itself. Rate help

+4
source share
2

, XML - . , , anser.

0

, , , xml , , - - java/kotlin.

:

, , , , , . .

SwitchPreference

<SwitchPreference
    android:title="@string/psm_pinterest"
    android:defaultValue="false"
    android:key="@string/pref_social_pinterest"/>

-, /

SwitchPreference . , :

SwitchPreference prefSocPin = (SwitchPreference) findPreference(getString(R.string.pref_social_pinterest));
prefSocPin.setWidgetLayoutResource(R.layout.pinterest_switch); // THIS IS THE KEY OF ALL THIS. HERE YOU SET A CUSTOM LAYOUT FOR PreferenceSwitch

prefSocPin.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // Here you can enable/disable whatever you need to
        return true;
    }
});

-, pinterest_switch.xml

, :

<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pinterest_switch_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:textIsSelectable="false"
    android:track="@drawable/pinterest_track" 
    android:thumb="@drawable/pinterest_thumb"/>

-, drawable

, xml drawable state listenener, 2 . , .

pinterest_track.xml

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/switch_bg_focused" android:state_focused="true"/>
    <item android:drawable="@drawable/switch_bg"/>

</selector>

pinterest_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">

     <item android:drawable="@drawable/switch_thumb_disabled" android:state_enabled="false"/>
     <item android:drawable="@drawable/switch_thumb_pressed" android:state_pressed="true"/>
     <item android:drawable="@drawable/switch_thumb_activated" android:state_checked="true"/>
     <item android:drawable="@drawable/switch_thumb"/>

</selector>

.

, , xml, - , . , , . .

+1

Source: https://habr.com/ru/post/1687060/


All Articles