Aug 14, 2011

Example of using CheckBoxPreference

Example of using CheckBoxPreference

- Create a xml file, to define the CheckBoxPreference, /res/xml/checkboxpref.xml.
<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="checkbox_pref"
android:title="Check Box Preferences">
<CheckBoxPreference
android:key="pref_opt1"
android:title="Opt 1"
android:summary="option 1"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="pref_opt2"
android:title="Opt 2"
android:summary="option 2" />
<CheckBoxPreference
android:key="pref_opt3"
android:title="Opt 3"
android:summary="option 3" />
<CheckBoxPreference
android:key="pref_opt4"
android:title="Opt 4"
android:summary="option 4" />
</PreferenceScreen>


- Create a SetPreference.java extends PreferenceActivity, it simple call the function addPreferencesFromResource(R.xml.checkboxpref) in onCreate().
package com.AndroidCheckBoxPreference;


import android.os.Bundle;
import android.preference.PreferenceActivity;

public class SetPreference extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.checkboxpref);
}

}


- Modify the layout main.xml to have a TextView to show the preferences, and a Button to start SetPreference.java.
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/pref"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/setpref"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Preferences"
/>
</LinearLayout>


- Main code
package com.AndroidCheckBoxPreference;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidCheckBoxPreferenceActivity extends Activity {

TextView pref;
Button setPref;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pref = (TextView)findViewById(R.id.pref);
setPref = (Button)findViewById(R.id.setpref);

setPref.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(
AndroidCheckBoxPreferenceActivity.this,
SetPreference.class);
startActivityForResult(intent, 0);
}});

checkPref();
}

private void checkPref(){
SharedPreferences myPref
= PreferenceManager.getDefaultSharedPreferences(
AndroidCheckBoxPreferenceActivity.this);
String _pref =
"Option 1: " + myPref.getBoolean("pref_opt1", true) + "\n"
+ "Option 2: " + myPref.getBoolean("pref_opt2", false) + "\n"
+ "Option 3: " + myPref.getBoolean("pref_opt3", false) + "\n"
+ "Option 4: " + myPref.getBoolean("pref_opt4", false);

pref.setText(_pref);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
checkPref();
}
}


- Modify AndroidManifest.xml to add activity of SetPreference.java
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidCheckBoxPreference"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidCheckBoxPreferenceActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SetPreference"/>
</application>
</manifest>



1 comment:

  1. simplest example, works in lollipop sdk too. (y) *thumbsup*

    ReplyDelete

Infolinks In Text Ads