PreferenceFragment example |
Create /res/xml/preferences.xml to define our preferences:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="PreferenceCategory A"> <CheckBoxPreference android:key="PREF_CHECKBOX" android:title="Title" android:summary="summary" /> </PreferenceCategory> </PreferenceScreen>
PrefFragment.java
package com.example.androidpreferencefragment; import android.os.Bundle; import android.preference.PreferenceFragment; public class PrefFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } }
PrefActivity.java
package com.example.androidpreferencefragment; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; public class PrefActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PrefFragment prefFragment = new PrefFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(android.R.id.content, prefFragment); fragmentTransaction.commit(); } }
MainActivity.java
package com.example.androidpreferencefragment; import android.os.Bundle; import android.preference.PreferenceManager; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button buttonSetPreference; TextView settingCheckBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonSetPreference = (Button)findViewById(R.id.setpreference); settingCheckBox = (TextView)findViewById(R.id.setting_checkbox); buttonSetPreference.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { Intent intentSetPref = new Intent(getApplicationContext(), PrefActivity.class); startActivityForResult(intentSetPref, 0); }}); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); Boolean prefCheckBox = sharedPreferences.getBoolean("PREF_CHECKBOX", false); settingCheckBox.setText("CHECKBOX preference = " + prefCheckBox.toString()); } }
Layout, /res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/setpreference" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Set Preference" /> <TextView android:id="@+id/setting_checkbox" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
Also need to modify AndroidManifest.xml to add activity of PrefActivity.
Next:
- Implement ListPreference in PreferenceFragment
No comments:
Post a Comment