PreferenceActivity in android

October 10, 2012 | Rishabh Agrawal | General

PreferenceActivity -This is the base class for an activity to show a hierarchy of preferences to the user. It is usually  used to create a standard looking settings screen for any application.

The values are stored in SharedPreferences automatically by the PreferenceActivity.

The list of settings, are defined in xml file in xml folder  which is inside the res folder.

The given example explains its usage.

Firstly ,create a xml file in xml folder which is  inside the res folder.

settings.xml

<?xml version=“1.0” encoding=“utf-8”?>

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

<PreferenceCategory android:title=“Application Settings” >

<Preference

android:dialogTitle=“Picture”

android:key=“picture”

android:summary=“Select background picture”

android:title=“Select background type” />

<Preference

android:dialogTitle=“change password”

android:key=“password”

android:summary=“Change lock password”

android:title=“change lock password” />

 

<CheckBoxPreference

android:defaultValue=“true”

android:key=“cb_vibrate”

android:summaryOff=“Vibration Deactivated”

android:summaryOn=“Vibration Activated”

android:title=“Vibration Activation” />

<CheckBoxPreference

android:defaultValue=“false”

android:key=“cb_enable”

android:summaryOff=“Locker Disabled”

android:summaryOn=“Locker Enabled”

android:title=“Enable “ />

<CheckBoxPreference

android:defaultValue=“false”

android:key=“cb_home”

android:summaryOff=“Home Key Enabled”

android:summaryOn=“Home Key Disabled”

android:title=“Disable Home Key” />

</PreferenceCategory>

</PreferenceScreen>

The layout for the SettingActivity which uses a ListView and all the setting preferences will be displayed in the list view.  The same is given below.

setting_screen.xml

<RelativeLayout 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:orientation=“vertical” >

 

<ListView android:id=“@android:id/list”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent” />

</RelativeLayout>

Now, this class extends PreferenceActivity implements OnPreferenceChangeListener.When ever the preference value is changed the OnPreferenceClickListener is used to do the required action.

public class SettingActivity extends PreferenceActivity implements

Preference.OnPreferenceChangeListener {

static ChartBoost cb;

String TAG = “chartboost”;

Preferences preferences;

@SuppressWarnings(“deprecation”)

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.setting_screen);// list view

addPreferencesFromResource(R.xml.settings);// adding preferences

}

//fetching the required prefereence

final Preference prefimg = (Preference) findPreference(“picture”);

//setting PreferenceClickListener on the preference

prefimg.setOnPreferenceClickListener(new OnPreferenceClickListener() {

// if select background is selected

public boolean onPreferenceClick(Preference preference) {

//preference clicked

Intent i = new Intent(SettingActivity.this, GalleryView.class);

startActivity(i);

// put your code here

return true;

}

 

 

Similarly , you can add action code whenever there is click on the preference setting.

Hence ,PrefrenceActivity  provides massive advantage over Prefrences as saving of preferences is automatic and hassle free.

Screenshot:

 

screen shot

THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Comments are closed.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal