Skip to main content

Save data using SharedPreferences in Android

          Today we are going to save any type of data using sharePreferances in android mobile.

Make a new android app.
if you don't know how to do that then Read this post.

Just drag and drop Edit Text and 2 Buttons into virtual mobile GUI as shown in figure.


Just Copy and paste following code before onCreate method

SharedPreferences sharedPref;
SharedPreferences.Editor editor;

now just copy and paste following code into onCreate method

sharedPref = MainActivity.this.getSharedPreferences("xyz", Context.MODE_PRIVATE);

final EditText editText=(EditText) findViewById(;

Button btn=(Button) findViewById(;
btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
editor.putString("city", String.valueOf(editText));

Button btn2=(Button) findViewById(;
btn2.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
String s =sharedPref.getString("city", "mumbai");

Toast.makeText(MainActivity.this, String.valueOf(s), Toast.LENGTH_SHORT).show();

Now just run app into your mobile.


Popular posts from this blog


By:Prayag nao

An internal expanding brake consists of two shoes S1 and S2. The outer surface of the shoes are lined with some friction material (usually with Ferodo) to increase the coefficient of friction and to prevent wearing away of the metal. Each shoe is pivoted at one end about a fixed fulcrum O1and O2 and made to contact a cam at the other end. When the cam rotates, the shoes are pushed outwards against the rim of the drum. The friction between the shoes and the drum produces the braking torque and hence reduces the speed of the drum. The shoes are normally held in
off position by a spring . The drum encloses the entire mechanism to keep out dust and moisture. This type of brake is commonly used in motor cars and light trucks.

We shall now consider the forces acting on such a brake, when the drum rotates in the anticlockwise direction. It may be noted that for the anticlockwise direction, the left hand shoe is known as leading or primary shoe while the right hand sho…

How an engine cooling system works

By: Prayag nao

Awesome iris mechanism

By:Prayag nao

An iris is a mechanism that is used as a compact solution to close and open holes. It is made up of usually a series of metal plates that can fold in on each other or expand out. Iris mechanisms are commonly found in optical shutters (diaphragms). Irises are sometimes used in science fiction as doors.