Skip to main content

Save data using SharedPreferences in Android

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

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

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



Step-3

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);
editor=sharedPref.edit();

final EditText editText=(EditText) findViewById(R.id.editText);

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

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

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

@Override
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.

Comments

Popular posts from this blog

Real life Jarvis-Talk With Your Computer like Jarvis in Iron Man ....!

By:Prayag nao                                            Code to Make your Computer like Jarvis New Speech macro..>> Choose Advanced and change the code like this.. <speechMacros>   <command>     <listenFor></listenFor>   </command> </speechMacros> You have to add a commands  <listenFor>........</listenFor> - computer listens the words you specify here and respond accordingly. <speak>............</speak> - computer speaks what is written in this field according to the command which it got. Similarly, You can Edit more commands in the same way.   <speechMacros> <command> <listenFor>What's going on dude</listenFor> <speak>Nothing special tony</speak> </command> </speechMacros> ...

HOW INTERNAL EXPANDING BRAKE WORKS

By:Prayag nao An internal expanding brake consists of two shoes S 1 and S 2 . 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 O 1 and O 2 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 ...

Types of gears used in daily life

By:Prayag nao Spur Gears: Spur gears are the most common type used. Tooth contact is primarily rolling, with sliding occurring during engagement and disengagement. Some noise is normal, but it may become objectionable at high speeds.   Rack and Pinion. Rack and pinion gears are essentially a linear shaped variation of spur gears The spur rack is a portion of a spur gear with an infinite radius. Internal Ring Gear: Internal gear is a cylindrical shaped gear with the meshing teeth inside or outside a circular ring. Often used with a spur gear. Internal ring gears may be used within a planetary gear arrangement. ...