Monday, 31 October 2016

SharedPreferences

The SharedPreferences class gives a general system that permits you to spare and recover determined key-esteem sets of primitive information sorts. You can utilize SharedPreferences to spare any primitive information: booleans, coasts, ints, yearns, and strings. This information will continue crosswise over client sessions (regardless of the possibility that your application is executed).

There are two methods of SharedPreferences.

1) get SharedPreferences()
2)get Preferences()

 Example Code for  SharedPreferences.

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity" 
 android:background="#be2f2f">

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_marginTop="20dp" 
 android:background="#f3e8e8" 
 android:allowUndo="false" 
 android:hint="Name" />

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText2" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" 
 android:background="#e8dfdf" 
 android:hint="Location" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Save" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Show" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button2" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

showactivity.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" android:layout_width="match_parent" 
 android:layout_height="match_parent">

<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:id="@+id/textView" 
 android:text="Large Text" 
 android:layout_gravity="center_horizontal" />

<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:id="@+id/textView2" 
 android:text="Large Text" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>
 
MainActivity.java :-

package com.kiranapp;

import android.content.Intent;
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 {
    Button savebtn,showbtn;
    EditText uname,uloc;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 savebtn=(Button)findViewById(R.id.button);
 uname=(EditText)findViewById(R.id.editText);
 uloc=(EditText)findViewById(R.id.editText2);
 showbtn=(Button)findViewById(R.id.button2);
 savebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 String s1=uname.getText().toString();
 String s2=uloc.getText().toString();
 SharedPreferences sp=getSharedPreferences("myfile",MODE_PRIVATE);
 SharedPreferences.Editor e=sp.edit();
 e.putString("name",s1);
 e.putString("loc",s2);
 e.commit();
 Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
 }
 });
 showbtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 Intent in=new Intent(MainActivity.this,ShowActivity.class);
 startActivity(in);
 }
 });
}
}

ShowActivity.java :-

package com.kiranapp;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

/** * Created by Dell on 10/31/2016. */
 public class ShowActivity extends AppCompatActivity {

    TextView t1, t2;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.showactivity);
 t1=(TextView)findViewById(R.id.textView);
 t2=(TextView)findViewById(R.id.textView2);
 SharedPreferences sp=getSharedPreferences("myfile",MODE_PRIVATE);
 String s1=sp.getString("name",null);
 String s2=sp.getString("loc",null);
 t1.setText(s1);
 t2.setText(s2);
 }
}
 

Output :-

























7 comments:

  1. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    Devops Training courses
    python Training in chennai
    Devops Training in Bangalore
    Best Devops Training in pune

    ReplyDelete
  2. I am really very happy to find this particular site. I just wanted to say thank you for this huge read!! I absolutely enjoying every petite bit of it and I have you bookmarked to test out new substance you post.
    Python Online training
    python Course institute in Chennai
    Python Course institute in Bangalore

    ReplyDelete
  3. Concept involves in the contents are very beneficial, put the hands together for all your Effective works.To get More Beneficial Click on here...
    python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery

    ReplyDelete
  4. This post is so interactive and informative.keep update more information...
    Salesforce Training in Tambaram
    Salesforce Training in Chennai

    ReplyDelete
  5. This is exactly the guide I needed! As a student new to Android, the concept of auto-starting an app after boot felt like magic. Your explanation of the BroadcastReceiver, the permission, and the manifest entries made it click. I got it working on my physical device after realizing the app needs to be launched at least once manually (Android 3.1+ requirement)—maybe worth adding a note about that.

    To become job-ready, I'm not just learning individual Android components; I'm also learning how to manage the entire development lifecycle. That's why I enrolled in DevOps Training with Placement in Electronic City, which covers everything from coding to deployment automation.

    Thanks again for this beginner-friendly tutorial—it's a great starting point for anyone exploring background services.

    ReplyDelete