EditText Preference Example

This example shows how you can use edit text preference in android. EditTextPreference is a Preference that allows for string input.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it EditTextPreference.

2.) Write following into 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" />
<TextView
android:id="@+id/setting_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/setting_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

 

 

3.) Create and write following into res/xml/preference.xml:

 

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

<PreferenceCategory
android:title="PreferenceCategory CheckBoxPreference">
<CheckBoxPreference
android:key="PREF_CHECKBOX"
android:title="Title"
android:summary="summary" />
</PreferenceCategory>

<PreferenceCategory
android:title="PreferenceCategory ListPreference">
<ListPreference
android:key="PREF_LIST"
android:title="ListPreference title"
android:summary="ListPreference summary"
android:entries="@array/listentries"
android:entryValues="@array/listvalues" />
</PreferenceCategory>

<PreferenceCategory
android:title="PreferenceCategory Intent">
<PreferenceScreen
android:title="Android Coding"
android:summary="android-coding.blogspot.com">
<intent android:action="android.intent.action.VIEW"
android:data="http://android-coding.blogspot.com/" />
</PreferenceScreen>
<PreferenceScreen
android:title="Android Developers"
android:summary="developer.android.com">
<intent android:action="android.intent.action.VIEW"
android:data="http://developer.android.com/" />
</PreferenceScreen>
<PreferenceScreen
android:title="Google"
android:summary="www.google.com">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.google.com/" />
</PreferenceScreen>
</PreferenceCategory>

<PreferenceCategory
android:title="PreferenceCategory EditTextPreference">
<EditTextPreference
android:key="PREF_EDITTEXT"
android:title="EditText title"
android:summary="EditText summary"
android:dialogTitle="EditText Dialog" />
</PreferenceCategory>
</PreferenceScreen>

 

 

4.) Create and write following into res/values/arrays.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listentries">
<item>List item 1</item>
<item>List item 2</item>
<item>List item 3</item>
<item>List item 4</item>
</string-array>
<string-array name="listvalues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
</resources>

 

 

5.) Add following activity to your manifest.

 

<activity android:name="PrefActivity"></activity>

 

 

6.) Create and write following into PrefActivity.java:

 

package com.example.edittextpreference;

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();
}
}

 

 

7.) Create and write following into PrefFragment.java:

 

package com.example.edittextpreference;

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.preference);
}
}

 

 

8.) Run for output.

Steps:

1.) Create a project named EditTextPreference and set the information as stated in the image.

Build Target: Android 4.3
Application Name: EditTextPreference
Package Name: com.example. EditTextPreference
Activity Name: EditTextPreferenceActivity

2.) Open EditTextPreferenceActivity.java file and write following code there:

 

package com.example.edittextpreference;

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 EditTextPreferenceActivity extends Activity {

Button buttonSetPreference;
TextView settingCheckBox, settingList;
TextView settingEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSetPreference = (Button)findViewById(R.id.setpreference);
settingCheckBox = (TextView)findViewById(R.id.setting_checkbox);
settingList = (TextView)findViewById(R.id.setting_list);
settingEditText = (TextView)findViewById(R.id.setting_edittext);

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());
String prefList = sharedPreferences.getString("PREF_LIST", "no selection");
settingList.setText("LIST preference = " + prefList);

String prefEditText = sharedPreferences.getString("PREF_EDITTEXT", "default");
settingEditText.setText("EDITTEXT preference = " + prefEditText);
}
}

 

 

3.) Compile and build the project.

Output