PrefernceSettings

Preference Settings allow the user to select basic settings for an app. It’s a way of customising the app to suite the user. This tutorial will show you how you can create your own custom Preference Settings for your apps. We’ll be using fragments and Headers so this tutorial is suitable for apps developed for devices running Android 3.0 and up.

1. MainActivity.java

package com.tutorialsee.com;

import com.tutorialsee.com.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
String firstname;
EditText edt_lastname,edt_email,edt_password,edt_firstname;
Button btnreg;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
context = this;
PrefernceSettings.openDataBase(context);
edt_firstname = (EditText) findViewById(R.id.edt_firstname);
edt_lastname = (EditText) findViewById(R.id.edt_lastname);
edt_email = (EditText) findViewById(R.id.edt_email);
edt_password = (EditText) findViewById(R.id.edt_password);
btnreg = (Button) findViewById(R.id.btnreg);
btnreg.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String frist = edt_firstname.getText().toString();
String last = edt_lastname.getText().toString();
String email = edt_email.getText().toString();
String password = edt_password.getText().toString();
PrefernceSettings.setUserName(frist);
PrefernceSettings.setLastName(last);
PrefernceSettings.setEmail(email);
PrefernceSettings.setPassword(password);
Intent intent = new Intent(getApplicationContext(),GetData.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}

2. PrefernceSettings.java

package com.tutorialsee.com;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

@SuppressLint("CommitPrefEdits") public class PrefernceSettings
{
private static SharedPreferences myPrefs;
private static SharedPreferences.Editor prefsEditor;

@SuppressWarnings("static-access")
public static void openDataBase(Context context)
{
try
{
context=context.getApplicationContext();
myPrefs = context.getSharedPreferences("Reload", context.MODE_PRIVATE);
prefsEditor = myPrefs.edit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static String getLastName()
{
String i = myPrefs.getString("KEY_LastName","");
Log.e("dfg", i);
return i;
}
public static void setLastName(String m)
{
Log.e("dfgdfd", m);
prefsEditor.putString("KEY_LastName",m);
prefsEditor.commit();
}
public static String getUserName()
{
String i = myPrefs.getString("KEY_UserName","");
Log.e("dfg", i);
return i;
}
public static void setUserName(String s)
{
Log.e("dfgdfd", s);
prefsEditor.putString("KEY_UserName",s);
prefsEditor.commit();
}
public static String getRegFlag()
{
String i = myPrefs.getString("KEY_RegFlag","");
Log.e("dfg", i);
return i;
}
public static void setRegFlag(String l)
{
Log.e("dfgdfd", l);
prefsEditor.putString("KEY_RegFlag",l);
prefsEditor.commit();
}
public static String getEmail()
{
String i = myPrefs.getString("KEY_Email","");
Log.e("dfg", i);
return i;
}
public static void setEmail(String mm)
{
Log.e("dfgdfd", mm);
prefsEditor.putString("KEY_Email",mm);
prefsEditor.commit();
}
public static String getPassword()
{
String i = myPrefs.getString("KEY_Password","");
Log.e("dfg", i);
return i;
}
public static void setPassword(String m)
{
Log.e("dfgdfd", m);
prefsEditor.putString("KEY_Password",m);
prefsEditor.commit();
}
}

3. GetData.java

package com.tutorialsee.com;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

public class GetData extends Activity {
TextView first,last,email,pass;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_get_data);
context = this;
PrefernceSettings.openDataBase(context);
first = (TextView) findViewById(R.id.first);
last = (TextView) findViewById(R.id.last);
email = (TextView) findViewById(R.id.email);
pass = (TextView) findViewById(R.id.pass);
first.setText(PrefernceSettings.getUserName());
last.setText(PrefernceSettings.getLastName());
email.setText(PrefernceSettings.getEmail());
pass.setText(PrefernceSettings.getPassword());
}
}

4. activity_main.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" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_panel"
        android:background="@drawable/signup"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="60dp"
                        android:layout_marginRight="60dp"
                        android:gravity="center"
                        android:text="@string/loginwelcome"
                        android:textColor="@android:color/black"
                        android:textSize="15sp" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="45dp"
                    android:layout_marginRight="45dp"
                    android:gravity="center"
                    android:text="@string/loginshoping"
                    android:textColor="@android:color/black"
                    android:textSize="18sp" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/bottom_panel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ECEBEB"
                android:orientation="vertical" >

                <EditText
                    android:id="@+id/edt_firstname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/white"
                    android:ellipsize="start"
                    android:hint="@string/Firstname"
                    android:inputType="text"
                    android:padding="13dp"
                    android:textColor="#747373"
                    android:textColorHint="#747373"
                    android:textSize="14sp" />

                <EditText
                    android:id="@+id/edt_lastname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="0.1dp"
                    android:background="@android:color/white"
                    android:ellipsize="start"
                    android:hint="@string/Lastname"
                    android:inputType="text"
                    android:padding="13dp"
                    android:textColor="#747373"
                    android:textColorHint="#747373"
                    android:textSize="14sp" />

                <EditText
                    android:id="@+id/edt_email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="0.1dp"
                    android:background="@android:color/white"
                    android:ellipsize="start"
                    android:hint="@string/Email"
                    android:inputType="textEmailAddress"
                    android:padding="13dp"
                    android:textColor="#747373"
                    android:textColorHint="#747373"
                    android:textSize="14sp" />

                <EditText
                    android:id="@+id/edt_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="0.1dp"
                    android:layout_marginTop="0.1dp"
                    android:background="@android:color/white"
                    android:ellipsize="start"
                    android:hint="@string/Password"
                    android:inputType="textPassword"
                    android:padding="13dp"
                    android:textColor="#747373"
                    android:textColorHint="#747373"
                    android:textSize="14sp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="10dp" >

            <Button
                android:id="@+id/btnreg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/btnround"
                android:gravity="center"
                android:padding="5dp"
                android:text="Registration"
                android:textColor="@android:color/white"
                android:textSize="16sp" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

5. activity_get_data.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:background="@android:color/white"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#ffffff"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/first"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#EEEEEE"
                android:ellipsize="start"
                android:padding="13dp"
                android:text="FirstName"
                android:textColor="#747373"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/last"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="0.1dp"
                android:background="#EEEEEE"
                android:ellipsize="start"
                android:padding="13dp"
                android:text="LastName"
                android:textColor="#747373"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="0.1dp"
                android:background="#EEEEEE"
                android:ellipsize="start"
                android:padding="13dp"
                android:text="Email"
                android:textColor="#747373"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/pass"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="0.1dp"
                android:background="#EEEEEE"
                android:ellipsize="start"
                android:padding="13dp"
                android:text="Password"
                android:textColor="#747373"
                android:textSize="14sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>