Android Custom Switch Widget for SwitchPreferenc

I search around stackoverflow and find the following related topics:

  • How can I configure an Android switch?
  • Custom toggle widget in Android 4
  • Install switchStyle - get error resource not found - why?

I also found a bugreport in google group: Problem 36636: Failed to override switchStyle style And finally, find new problems with Switch widget:

  • I tried to create my own Preference.SwitchPreference and define the layout using the 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 the error message: Error: the resource is not public. (at 'id' with the value ' @ + android: id / switchWidget '). Therefore, I cannot use this method.

  • Secondly, I tried to extend the Switch class to add dialing resources from code. But I find that the setThumbResource method is only available from API 16. But I still can’t use @ + android: id / switchWidget because it is not public.

So, how can I get the switch customization for SDK API 15 ??? Or how can I configure the switch in the settings?

+3
android android-widget preferences uiswitch
source share
5 answers

Just found a terrible 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 bit complicated, and only works with Android> 16.

+3
source share

I don't know much about switch issues, but you can use ToggleButton as follows:

Define the button in the 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); } } 
+1
source share

Edit:

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

To:

 android:id="@+id/switchWidget" 

An example of a simple switch can be found here .

The widget switch only supports API level 14 and above, but if you want to use the 14th level API Pre Pre-Preference, check this one .

UPDATE: if you want to create your own switch, try this

0
source share

Inherit the SwitchPreference class and use it in the preferences.xml file with a layout pointing to your own layout. Then, in the onBind method of the inherited SwitchPreference class, you can find the corresponding view by id and set listeners. Remember to call super in onBind ().

0
source share

Edit:

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

To:

 android:id="@*android:id/switchWidget" 
-2
source share

All Articles