2

Within my Samsung Galaxy Note 2's SETTINGS under Network Connections is the Wi-Fi option. The row behaves like a PreferenceScreen but with a SwitchPreference off to the right; all this on the same row. By doing this one can tap the "Wi-Fi" name to open a new Preference Screen or can tap the Switch to enable or disable wifi.

How did they achieve this effect using the standard xml Preference settings elements ?

user2522885
  • 595
  • 1
  • 5
  • 15

1 Answers1

0

You can define a custom preference. The process goes something like this: In your XML declare the root view as id:widget_frame and declare the text views as title and summary. Then, add the other elements you'd like to use in the layout - in your case the toggle button.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/widget_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@android:id/title"
        style="@android:style/TextAppearance.DeviceDefault.SearchResult.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title" />

    <TextView
        android:id="@android:id/summary"
        style="@android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Summary" />

    <Switch
        android:id="@+id/switch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Then, in a class that derives from Preference, override the onCreateView method:

SwitchPreference.java

@Override
protected View onCreateView( ViewGroup parent )
{
  LayoutInflater li = (LayoutInflater)getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
  return li.inflate( R.layout.seekbar_preference, parent, false);
}

Then, in preferences.xml, use the preference:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <com.example.SwitchPreference
        android:key="pref_max_volume"
        android:title="@string/max_volume" />
    <com.example.SwitchPreference
        android:key="pref_balance"
        android:title="@string/balance" />

</PreferenceScreen>

Since Switch inherits from CompoundButton, you can implement the OnCheckedChangeListener to define the behavior when the switch is changed from on to off or vice-versa. And you can define onClickListener for the preference itself and make it open the second preference screen.

You may have to edit some of the code, as I've just written it here without checking it, but this is the basic concept. This is the original answer I used to adjust the code for you.

EDIT: Add answer to question in comments.

You can do something like this to implement the custom behavior when you click on the preference

SwitchPreference custom_preference = findPreference("preference_id");

custom_preference.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 @Override 
 public boolean onPreferenceClick(Preference preference) {
  //implement your custom behavior here 
}
});
deluxe1
  • 737
  • 4
  • 15
  • Very interesting. I will need to study this closer. Regarding the onClickListener for the Preference, what method should be invoked within the listener in order to open the second preference screen? The preference.xml has a hierarchy established -- will this be followed or am i programmatically deciding what to call similar to startActivity? – user2522885 Mar 28 '18 at 18:10
  • Try posting the code and maybe I, or someone else, can help you figure it out. – deluxe1 Mar 28 '18 at 20:08
  • Your approach involves subclassing Preference, inflating a custom view as its display, then presenting it as a PreferenceScreen child element, SwitchPreference. I understand what you are doing above. But what if i want to subclass PreferenceScreen itself bc i want it so when the user taps the text part of the element it takes him to the next preference screen. I dont know how to achieve this effect with a subclassed SwitchPreference but i assume the ability comes free with a PreferenceScreen. The prob is PreferenceScreen is final / no extending allowed. – user2522885 Mar 29 '18 at 08:21
  • As I said, I can't help any more without seeing the code you are using. Please post the relevant parts of the code & the xml, so I can take a look and help you if I can. – deluxe1 Mar 29 '18 at 08:52