Create android application to Understand Android Share Preference. - code, android programming
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dharmik.p11"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
//MainActivity.java
package com.example.dharmik.p11; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText username,password; Button insert,show,delete; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); username=(EditText)findViewById(R.id.et1); password=(EditText)findViewById(R.id.et2); insert=(Button)findViewById(R.id.bt1); show=(Button)findViewById(R.id.bt2); delete=(Button)findViewById(R.id.bt3); final SharedPreferences sp=getApplicationContext().getSharedPreferences("MyPrefs",MODE_PRIVATE); final SharedPreferences.Editor et=sp.edit(); insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"all previous data is being deleted",Toast.LENGTH_SHORT).show(); et.clear(); et.commit(); et.putString("username",username.getText().toString()); et.putString("password",password.getText().toString()); et.commit(); username.setText(null); password.setText(null); Toast.makeText(getApplicationContext(),"data stored successfully",Toast.LENGTH_SHORT).show(); } }); show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String un=sp.getString("username",null); String pass=sp.getString("password",null); username.setText(un); password.setText(pass); Toast.makeText(getApplicationContext(),"data fetched successfully",Toast.LENGTH_SHORT).show(); if (un==null || pass==null) Toast.makeText(getApplicationContext(),"no data found",Toast.LENGTH_SHORT).show(); } }); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.clear(); et.commit(); username.setText(null); password.setText(null); Toast.makeText(getApplicationContext(),"all data deleted successfully",Toast.LENGTH_SHORT).show(); } }); } }
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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" tools:context="com.example.dharmik.p11.MainActivity"> <TextView android:text="@string/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginStart="15dp" android:layout_marginTop="91dp" android:id="@+id/tv1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:text="@string/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="69dp" android:id="@+id/tv2" android:layout_below="@+id/tv1" android:layout_alignLeft="@+id/tv1" android:layout_alignStart="@+id/tv1" /> <Button android:text="@string/insert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="69dp" android:id="@+id/bt1" android:layout_below="@+id/et2" android:layout_centerHorizontal="true" /> <Button android:text="@string/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="31dp" android:id="@+id/bt2" android:layout_below="@+id/bt1" android:layout_centerHorizontal="true" /> <Button android:text="@string/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt3" android:layout_below="@+id/bt2" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/et1" android:layout_alignBaseline="@+id/tv1" android:layout_alignBottom="@+id/tv1" android:layout_alignLeft="@+id/bt2" android:layout_alignStart="@+id/bt2" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="text" android:ems="10" android:id="@+id/et2" android:layout_alignBaseline="@+id/tv2" android:layout_alignBottom="@+id/tv2" android:layout_alignLeft="@+id/et1" android:layout_alignStart="@+id/et1" /> </RelativeLayout>
//strings.xml
<resources> <string name="app_name">p11</string> <string name="username">username</string> <string name="password">password</string> <string name="insert">insert</string> <string name="show">show data</string> <string name="delete">delete</string> </resources>
OUTPUT:-
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment