3

I search around stackoverflow and find next related topics:

  1. How can i style an Android Switch?
  2. Custom switch widget in Android 4
  3. Set switchStyle - get error resource not found - why?

I also find bugreport on google group: Issue 36636: Unable to override style switchStyle And at last find new probles with Switch widget:

  • I tried to make my own Preference.SwitchPreference and define layout with Switch widget

    android:id="@+android:id/switchWidget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:thumb="@drawable/switch_thumb"
    android:layout_gravity="center"
    android:padding="16dip"
    android:focusable="false" />
    

but I get an compliation error: Error: Resource is not public. (at 'id' with value '@+android:id/switchWidget'). So I can't use this way.

  • Second way I tried to extend Switch class add set resources from code. But I find that method setThumbResource is availible only from API 16. But I still can't apply @+android:id/switchWidget because it's not public.

So, How can I get custom Switch Preference for SDK API 15 ??? Or how can I customize Switch in Preferences?

Community
  • 1
  • 1
Borys
  • 1,793
  • 2
  • 17
  • 32

5 Answers5

3

Just found an awful way to achieve this.

First, src/com/myapp/views/preference/MySwitchPreference.java

public class MySwitchPreference extends SwitchPreference {
    public MySwitchPreference(Context context) {
        super(context);
    }

    public MySwitchPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySwitchPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        if (view instanceof ViewGroup) {
            setLayout((ViewGroup) view);
        }
    }

    @SuppressLint("NewApi")
    private void setLayout(ViewGroup viewGroup) {
        int count = viewGroup.getChildCount();
        for(int n = 0; n < count; ++n) {
            View childView = viewGroup.getChildAt(n);
            if(childView instanceof Switch) {
                final Switch switchView = (Switch) childView;

                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                    switchView.setThumbResource(com.myapp.R.drawable.switch_inner);
                    switchView.setTrackResource(com.myapp.R.drawable.switch_track);
                }
                return;
            }
            else if (childView instanceof ViewGroup)
                setLayout((ViewGroup) childView);
        }
    }
}

And now, res/xml/preferences.xml

<com.myapp.views.preference.MySwitchPreference
            android:switchTextOff="Off"
            android:switchTextOn="On"
            android:title="whatever"
            android:key="switch" />

A little bit tricky, and only working with Android > 16.

Romain Pellerin
  • 2,470
  • 3
  • 27
  • 36
1

Don't know much about the switch issues but you could use a ToggleButton as follows:

Define the button in your layout:

<ToggleButton
            android:id="@+id/your_awesome_toggle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical|center_horizontal"
            android:layout_marginRight="15dp"
            android:textOn=""
            android:textOff=""
            android:background="@drawable/toggle_button"
        />

Create a selector:

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

OnClickListener:

    toggleOnOff = (ToggleButton) findViewById(R.id.your_awesome_toggle);

    toggleOnOff.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            updateButtons();
            if(toggleOnOff.isChecked()){
                 SharedPreferences emailPrefs = getSharedPreferences(rememberToggleOnOff,MODE_PRIVATE);
                 SharedPreferences.Editor editor = yourPrefs.edit();
                 editor.putBoolean("mon", true);
                 editor.commit();
            }
            else {
                SharedPreferences emailPrefs = getSharedPreferences(rememberToggleOnOff,MODE_PRIVATE);
                SharedPreferences.Editor editor = yourPrefs.edit();
                editor.putBoolean("mon", false);
                editor.commit();
            }
        }
    });
    checkToggleState();

checkToggleState method:

/**
     * Checks the state of the Toggle button preferences.  
     * If preferences are true set the toggle to on, if false set the toggle off.  
     * 
     */
    private void checkToggleState() {
        SharedPreferences yourPrefs = getSharedPreferences(rememberToggleOnOff,MODE_PRIVATE);
        boolean mON = yourPrefs.getBoolean("mon", true);
        if(mON) {
            toggleOnOff.setChecked(true);
        }
        else {
            toggleOnOff.setChecked(false);
        }
    }
Andrei
  • 2,607
  • 1
  • 23
  • 26
0

Change:

android:id="@+android:id/switchWidget"

To:

android:id="@+id/switchWidget"

A simple switch example can be found here.

Switch widget supports 14 and above API level only, but if you want to use Switch Preference pre API level 14, check this.

UPDATE: If you want to style your own switch, try this

Community
  • 1
  • 1
Alejandro Colorado
  • 6,034
  • 2
  • 28
  • 39
  • I need to use "@+android:id/switchWidget" id because this id used inside of SwitchPreference. And my question was "How can I get custom Switch Preference for SDK API 15 ??? Or how can I customize Switch in Preferences?" – Borys Jun 20 '13 at 06:26
  • If you want to use ` – Alejandro Colorado Jun 20 '13 at 08:29
  • Can you give me solution how to change color of default switch in preference? Or change drawable of on/off state? – Borys Jun 20 '13 at 08:37
  • Alejandro, did you tried it by your self? Or it's just a theory? Ofcource I tried it befor post my question. If you have WORKing styled preference example, post it here. Don't post links that works for all another situation exempt my. – Borys Jun 20 '13 at 10:23
  • 1
    I've customized many layouts but not specifically the `SwitchPreference`. Just by reading and analyzing the code, I am pretty sure the customization described in the link I gave you works and it's VERY similar to what you are asking for. In your question, you don't mention you tried that link, so I included it. Don't worry, I will stop posting anything that may help you if you try it, as it seems it bothers you. – Alejandro Colorado Jun 20 '13 at 11:25
0

Inherit SwitchPreference class and use it in preferences.xml with layout pointing to your custom layout. Then in the onBind method of inherited SwitchPreference class you can find corresponding view by id and set listeners. Don't forget to call super in onBind().

sgadde
  • 51
  • 3
-3

Change:

android:id="@+android:id/switchWidget"

To:

android:id="@*android:id/switchWidget"
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58