Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add buttons to PreferenceFragment

I want to add a couple buttons to the bottom of my preferences screen for setting defaults and restoring defaults. This answer doesn't cover how to do this using PreferenceFragment. What is the recommended way to accomplish this.

Activity that loads the preferences fragment:

public class SettingsActivity extends Activity {

    @Override
    public void onCreate( Bundle savedInstanceState) {

        super.onCreate( savedInstanceState);

        // load up the preferences fragment
        getFragmentManager().beginTransaction().replace( android.R.id.content, new PrefsSettingsFragment()).commit();
    }
}

PreferenceFragment implementation:

public class PrefsSettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate( Bundle savedInstanceState) {

        super.onCreate( savedInstanceState);  

        addPreferencesFromResource( R.xml.preferences);             
    }       
}

preferences.xml:

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

    <EditTextPreference android:key="edit_text_preference_server_address" android:title="@string/preference_server_address"/>
    <SwitchPreference android:key="switch_preference_bat" android:title="@string/preference_bat"/>
    <SwitchPreference android:key="switch_preference_comm" android:title="@string/preference_comm"/>
    <SwitchPreference android:key="switch_preference_dev_mode" android:title="@string/preference_dev_mode" android:defaultValue="true"/>

</PreferenceScreen>
like image 700
Namaste Avatar asked Sep 06 '12 17:09

Namaste


People also ask

How do I add preferences to a fragment?

To load the settings into the fragment, load the preferences in the onCreatePreferences () method. To get an instance of a preference, search for a preference using its key through the PreferenceManager within onCreateView (). You can use this instance to customize the preference, such as add a custom OnPreferenceChangeListener.

Is it possible to add a button to a preference screen?

I don't think there is an easy way to just add a Button to a preference screen. Preferences are limited to: While using a preference screen, you are limited to the use of these options, and there are no single "button-preference" which could be easily used for your need.

Where should I use the preferencefragment?

The look and feel provided by the PreferenceFragment should be used across all settings pages in almost all apps. This way the user knows exactly how to change settings and knows exactly what to expect when interacting with different preferences on the settings page.

What is the difference between a button preference and preferences?

Preferences are limited to: While using a preference screen, you are limited to the use of these options, and there are no single "button-preference" which could be easily used for your need.


1 Answers

Just create your own layout for the settings activity which contains list view with id @android:id/list

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fragment"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>

    <Button
        android:id="@+id/button"
        android:text="Save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

And then in the activity class set the content view before adding preference fragment

public class SettingsActivity extends Activity {

    @Override
    public void onCreate( Bundle savedInstanceState) {    
        super.onCreate( savedInstanceState);
        setContentView(R.layout.settings_activity)

        // load up the preferences fragment
        getFragmentManager().beginTransaction().replace( android.R.id.content, new PrefsSettingsFragment()).commit();
    }
}
like image 139
iq_1 Avatar answered Sep 19 '22 18:09

iq_1