11

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>
Community
  • 1
  • 1
Namaste
  • 265
  • 1
  • 4
  • 11
  • 1
    I could be wrong but isn't it the same way? you would just have to define your own layout with buttons on the bottom and `addPreferencesFromResource(...)` should populate the listview. Although I do not recommend having buttons floating on the bottom the screen. Unless you mean at the bottom of your list view? – StrikeForceZero Sep 06 '12 at 18:23
  • Ah, you are correct. Adding the ListView and Button entry to my activity xml and calling `this.setContentView( R.layout.activity_settings)` after `getFragmentManager().beginTransaction().replace( android.R.id.content, new PrefsSettingsFragment()).commit()` gives me a button at the bottom like I wanted. Next up is figuring out how to make the button work. – Namaste Sep 06 '12 at 20:47
  • 1
    as in resetting to defaults? set an `onClickListner()` for the Button and call `PreferenceManager.setDefaultValues(context, PREFS_FILENAME, 0, R.xml.preferences, true);` (false if you don't want to overwrite already set preferences, and are just setting defaults for the first time) – StrikeForceZero Sep 06 '12 at 21:46
  • The issue is the buttons are unclickable. I fiddled with it for a while without success so I decided to go with a [simpler approach](http://stackoverflow.com/questions/5298370/how-to-add-a-button-to-a-preferencescreen-android). It is also awkward to have the buttons floating at the bottom of the screen. With this approach, they can appear at the end of the preferences list. – Namaste Sep 06 '12 at 21:53

4 Answers4

10

I meet the same question, and I found a way to handle this issue.

Overwrite the method onCreateView in PreferenceFragment, using the given the LayoutInfalter by parameters to create your own View, and return this view.

It's my code. I hope this can be helpful

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View v = inflater.inflate(R.layout.set_alarm, null);

        Button save = (Button)v.findViewById(R.id.save);
        Button revert = (Button)v.findViewById(R.id.revert);
        Button delete = (Button)v.findViewById(R.id.delete);

        save.setOnClickListener(this);
        revert.setOnClickListener(this);
        delete.setOnClickListener(this);

        if(mId == -1){
            delete.setEnabled(false);
        }

        return v;
    }
NicotIne
  • 155
  • 1
  • 10
9

I modified the previous post a little bit, so that the button will get attached at the bottom of the view.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout v = (LinearLayout) super.onCreateView(inflater, container, savedInstanceState);

        Button btn = new Button(getActivity().getApplicationContext());
        btn.setText("Button on Bottom");

        v.addView(btn);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            }
        });

        return v;
    }
Sebastian Ullrich
  • 1,007
  • 11
  • 21
  • Overiding (via the IDE, slightly different method signature) the onCreateView in the PreferenceFragment subclass worked for me. – knarf Jun 02 '18 at 21:15
5

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();
    }
}
iq_1
  • 61
  • 2
  • 2
1

you may try this

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            LinearLayout v = (LinearLayout) super.onCreateView(inflater, container, savedInstanceState);

            Button SendLogbtn = new Button(getActivity().getApplicationContext());
            SendLogbtn.setText("send log file");

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);

            params.setMargins(100, 0, 0, 500);
            SendLogbtn.setLayoutParams(params);

            v.addView(SendLogbtn);

            SendLogbtn.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    // do your code 

                }
            });

            return v;
        }