Friday, June 3, 2011

how to use SharedPreferences in android?

aura.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout android:id="@+id/TopBar"
        android:layout_alignParentTop="true" android:layout_above="@+id/BottomBar"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:background="@drawable/aura">
        <LinearLayout android:id="@+id/ClientSide" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentTop="true"
            android:layout_marginTop="5dip" android:orientation="vertical"
            android:layout_alignParentLeft="true">
            <TextView android:id="@+id/EnterName" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="@string/enterclientname" android:textColor="#000000"
                android:textSize="22sp" android:textStyle="bold" android:layout_marginLeft="3dip">
            </TextView>
            <TextView android:id="@+id/Date" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="@string/date" android:textColor="#000000"
                android:textSize="10sp" android:textStyle="bold" android:layout_marginLeft="3dip">
            </TextView>
        </LinearLayout>
        <LinearLayout android:id="@+id/ServerSide" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentTop="true"
            android:layout_marginTop="5dip" android:orientation="vertical"
            android:layout_alignParentRight="true">
            <TextView android:id="@+id/ProvidedBy" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="@string/providedby" android:textColor="#000000"
                android:textSize="10sp" android:textStyle="bold" android:layout_marginRight="3dip">
            </TextView>
            <TextView android:id="@+id/CenterName" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="@string/centername" android:textColor="#000000"
                android:textSize="10sp" android:textStyle="bold" android:layout_marginRight="3dip">
            </TextView>
            <TextView android:id="@+id/CenterAddress" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="@string/centeraddress" android:textColor="#000000"
                android:textSize="10sp" android:textStyle="bold" android:layout_marginRight="3dip">
            </TextView>
        </LinearLayout>
    </RelativeLayout>
    <LinearLayout android:id="@+id/BottomBar"
        android:layout_alignParentBottom="true" android:gravity="center"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:background="@drawable/bar" android:layout_centerVertical="true">
        <Button android:id="@+id/Scan" android:layout_width="wrap_content"
            android:layout_height="wrap_content" style="@style/Button"
            android:text="@string/scan" android:textColor="#FFFFFF" android:textStyle="bold"
            android:layout_weight="1" android:layout_marginLeft="10dip">
        </Button>
        <Button android:id="@+id/Tune" android:layout_width="wrap_content"
            android:layout_height="wrap_content" style="@style/Button"
            android:text="@string/tune" android:textColor="#FFFFFF" android:textStyle="bold"
            android:layout_weight="1" android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip">
        </Button>
        <Button android:id="@+id/Settings" android:layout_width="wrap_content"
            android:layout_height="wrap_content" style="@style/Button"
            android:text="@string/settings" android:textColor="#FFFFFF" android:textStyle="bold"
            android:layout_weight="1" android:layout_marginRight="10dip">
        </Button>
    </LinearLayout>
</RelativeLayout>


getclientname.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="vertical">
    <EditText android:id="@+id/GetClientName" android:layout_height="wrap_content"
        android:layout_width="fill_parent" />
</LinearLayout>

create new folder xml and put this file
preferences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Simple Preferences">
        <EditTextPreference
            android:key="providedcentername"
            android:title="Set Center Name"
            android:summary="Please set the center name here"
            android:dialogTitle="Center Name" />
        <EditTextPreference
            android:key="providedcenterinfo"
            android:title="Set Center Info"
            android:summary="Please set the center info here"
            android:dialogTitle="Center Info" />
    </PreferenceCategory>
</PreferenceScreen>


Aura.java

package com.kihealer.aura;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Aura extends Activity
{
    protected static final String TAG = Aura.class.getName();
    private static int mHeight, mWidth;
    TextView mClientName, mDate, mProvidedBy, mCenterName, mCenterAddress;
    String mGetClientNameString;
    Button mScan, mTune, mSettings;
   
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aura);
       
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        mHeight = displayMetrics.heightPixels;
        Log.v(TAG, "Device Height: " + mHeight);
        mWidth = displayMetrics.widthPixels;
        Log.v(TAG, "Device Width: " + mWidth);
       
        mClientName = (TextView)findViewById(R.id.EnterName);
        mDate = (TextView)findViewById(R.id.Date);
        mProvidedBy = (TextView)findViewById(R.id.ProvidedBy);
        mCenterName = (TextView)findViewById(R.id.CenterName);
        mCenterAddress = (TextView)findViewById(R.id.CenterAddress);
        mScan = (Button)findViewById(R.id.Scan);
        mTune = (Button)findViewById(R.id.Tune);
        mSettings = (Button)findViewById(R.id.Settings);
       
        mClientName.setOnTouchListener(new OnTouchListener()
        {
            public boolean onTouch(View mView, MotionEvent mMotionEvent)
            {
                getClientNameDialog();
                return false;
            }
        });
       
        Configuration mConfiguration = new Configuration();
        Settings.System.getConfiguration(getContentResolver(), mConfiguration);
        final TimeZone mTimeZone = Calendar.getInstance(mConfiguration.locale).getTimeZone();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("MMMMM dd, yyyy - HH:mm");
        mSimpleDateFormat.setTimeZone(mTimeZone);
        String mCurrentTime = mSimpleDateFormat.format(new Date());
        mDate.setText(mCurrentTime);
       
        mScan.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Log.v(TAG, "Scan Tab Clicked");
            }
        });
       
        mTune.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Log.v(TAG, "Tune Tab Clicked");
            }
        });
       
        mSettings.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Log.v(TAG, "Settings Tab Clicked");
                startActivity(new Intent(Aura.this, EditPreferences.class));
            }
        });
       
    }//onCreate
   
    private void getClientNameDialog()
    {
        View mView = View.inflate(Aura.this, R.layout.getclientname, null);
        final EditText mSavedClientName = ((EditText) mView.findViewById(R.id.GetClientName));
       
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(Aura.this);
        mBuilder.setTitle(getString(R.string.enterclientname));
        mBuilder.setPositiveButton(getString(R.string.save), new Dialog.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                mGetClientNameString = mSavedClientName.getText().toString().trim();
                mClientName.setText(mGetClientNameString);
                dialog.dismiss();
            }
        });
        mBuilder.setView(mView);
        mBuilder.show();
     }//getClientNameDialog
   
    @Override
    public void onResume()
    {
        super.onResume();
        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(Aura.this);
        mCenterName.setText(mSharedPreferences.getString("providedcentername", "New York Healing Center"));
        mCenterAddress.setText(mSharedPreferences.getString("providedcenterinfo", "www.auraqi.com/100.htm"));
    }
   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((!(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT)
                &&keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0))
        {
            onBackPressed();
        }
        return super.onKeyDown(keyCode, event);
    }
   
    public void onBackPressed()
    {
        finish();
    }

}


EditPreferences.java

package com.kihealer.aura;

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

public class EditPreferences extends PreferenceActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

No comments:

Post a Comment