3

I have worked out a design for settings by using Android's Preference API.

But I don't know how to create a "Master on/off switch". It is mentioned here http://developer.android.com/design/patterns/settings.html but there is no document of how to implement it.

Technically it is a SwitchPreference located in the Action Bar which disables all child settings when it is turned off.

Any ideas?

NoobieNoob
  • 887
  • 2
  • 11
  • 31

2 Answers2

6

you have to create it manually . This is a pattern that you should implement. you can add a button in action bar and on click you can enable or disable the feature and change the drawable accordingly. you can use switch tag but this is introduce in api 14 only. example,

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Switch
        android:id="@+id/switchForActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</RelativeLayout>

Then, in your mainmenu.xml add the item as follows

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/myswitch"
        android:title=""
        android:showAsAction="always"
        android:actionLayout="@layout/switch_layout"
    />   
</menu>

you can add use this as well

public class TogglePreference extends Preference {

    public TogglePreference(Context context) {
        super(context);
    }

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

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

    public View getView(View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new LinearLayout(getContext());
            ((LinearLayout) convertView)
                    .setOrientation(LinearLayout.HORIZONTAL);

            TextView txtInfo = new TextView(getContext());

            txtInfo.setText("Test");
            ((LinearLayout) convertView).addView(txtInfo,
                    new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.FILL_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 1));

            ToggleButton btn = new ToggleButton(getContext());
            ((LinearLayout) convertView).addView(btn);
        }

        return convertView;
    }

And the preferences.xml:

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

    <PreferenceCategory android:title="Test custom preferences" >

        <android.dumdum.TogglePreference />
    </PreferenceCategory>

</PreferenceScreen>
nitesh goel
  • 6,338
  • 2
  • 29
  • 38
1

You have to create custom action bar with switch then you can achieve this, for help see this

How to add a switch to android action bar?

and

http://www.vogella.com/articles/AndroidActionBar/article.html

Community
  • 1
  • 1
keshav
  • 3,235
  • 1
  • 16
  • 22