How to use Preferences in android?

By | February 9, 2012

Hello all…

Today I will show you how to use preferences in android?

First we will create preferences using

EditTextPreference

Create a folder named “xml” inside the “res” folder and inside that create an xml named “preferences.xml”.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
	<EditTextPreference android:title="User Name"
		android:key="username"
		android:summary="Please enter username">
	</EditTextPreference>
	<EditTextPreference android:title="Password"
		android:password="true"
		android:key="password"
		android:summary="Please enter password">
	</EditTextPreference>
</PreferenceScreen>

Now create a java file named “PreferenceDemo.java” in your src folder and copy this code into it.

package com.coderzheaven.pack;

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

public class PreferencesDemo extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);
    }
}

Now in your main java file copy this code..

package com.coderzheaven.pack;

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

		SharedPreferences myprefs;
        EditText user = null;
		EditText password = null;
        Button login_but;

        @Override
        public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        Intent i = new Intent(MainActivity.this,PreferencesDemo.class);
	        startActivity(i);
       }
}

Run it now. Your screen will appear like this

Now we will make some changes in the main java code, so that if the user has set preferences then we will navigate to another xml which will allow user to enter the values stored in the preferences and check it..

Copy this code to your main java file.

package com.coderzheaven.pack;

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

		SharedPreferences myprefs;
        EditText user = null;
		EditText password = null;
        Button login_but;

        @Override
        public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        myprefs = PreferenceManager.getDefaultSharedPreferences(this);

	        // get the preference values //
	        final String username_ = myprefs.getString("username", null);
	        final String password_ = myprefs.getString("password", null);

	        // check whether those are null or not //
		    if (username_ != null && password_ != null){

		        setContentView(R.layout.main);

		        user = (EditText)findViewById(R.id.userText);
		        password = (EditText)findViewById(R.id.passwordText);
		        login_but = (Button)findViewById(R.id.loginButton);
		        login_but.setOnClickListener(new OnClickListener() {

		        public void onClick(View v) {
			        try {
				        if(username_.equalsIgnoreCase(user.getText().toString())
					        && password_.equals(password.getText().toString())) {
					        Toast.makeText(MainActivity.this, "Preferences value matching!!", Toast.LENGTH_SHORT).show();
					        Intent i = new Intent(MainActivity.this,PreferencesDemo.class);
					        startActivity(i);
				        } else {
				        	Toast.makeText(MainActivity.this,"Preferences value not matching!!", Toast.LENGTH_SHORT).show();
				        }
			        } catch (Exception e) {
			        	e.printStackTrace();
			        }
		        }
		        });
		    } else {
		    	Intent i = new Intent(this, PreferencesDemo.class);
		    	startActivity(i);
	        }
        }
}

Your main.xml will look like this.

<?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="username"
		/>
		<EditText
			android:id="@+id/userText"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
		/>
		<TextView
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:text="password"
		/>
		<EditText
			android:id="@+id/passwordText"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:password="true"
		/>
		<Button
			android:id="@+id/loginButton"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="login"
			android:textSize="20dp"
		/>
</LinearLayout>

Please leave your valuable comments on this post.

2 thoughts on “How to use Preferences in android?

Leave a Reply

Your email address will not be published. Required fields are marked *