May 13, 2013

Implement EditTextPreference in PreferenceFragment

EditTextPreference is a Preference that allows for string input. It is a subclass of DialogPreference and shows the EditText in a dialog. This EditText can be modified either programmatically via getEditText(), or through XML by setting any EditText attributes on the EditTextPreference.

EditTextPreference in PreferenceFragment
EditTextPreference in PreferenceFragment


Works on previous article of "ListPreference example".

Modify /res/xml/preferences.xml to add <EditTextPreference>.

<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>


Modify /res/layout/activity_main.xml to add TextView, setting_edittext, to display the EditTextPreference.
<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>


Modify MainActivity.java to update preferences once activity returned.
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, settingList;
 TextView settingEditText;

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


}


Keep using PrefFragment.java and PrefActivity.java in the article "PreferenceFragment example for Android 3.0 (HoneyComb)".

No comments:

Post a Comment

Infolinks In Text Ads