Showing posts with label e14.1.1. Show all posts
Showing posts with label e14.1.1. Show all posts
SharedPreference

SharedPreference

MainActivity.java
 public class MainActivity extends AppCompatActivity {
  String FileName = "myFile";
  Button BtnSave, BtnRead;
  EditText editName;
  TextView readName;
  String name;
  @Override 
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   BtnSave = (Button) findViewById(R.id.idBtnSave);
   BtnSave.setOnClickListener(new View.OnClickListener() {
    @Override 
     public void onClick(View v) {
     saveFile();
   }
  });
   BtnRead = (Button) findViewById(R.id.idBtnRead);
   BtnRead.setOnClickListener(new View.OnClickListener() {
   @Override 
    public void onClick(View v) {
     readFile();
   }
  });
   editName = (EditText) findViewById(R.id.idName);
   readName = (TextView) findViewById(R.id.idReadtxt);
 }
  private void readFile() {
   SharedPreferences sharedPref = getSharedPreferences(FileName, Context.MODE_PRIVATE);
   String defaultValue = "DefaultName";
   String name = sharedPref.getString("name", defaultValue);
   readName.setText(name);
   Toast.makeText(this, "Data :" + name, Toast.LENGTH_SHORT).show();
  
 }
  private void saveFile() {
   String strName = editName.getText().toString();
   SharedPreferences sharedPref = getSharedPreferences(FileName, Context.MODE_PRIVATE);
   SharedPreferences.Editor editor = sharedPref.edit();
   editor.putString("name", strName);
   editor.commit();
   Toast.makeText(this, "Data Saved Successfully", Toast.LENGTH_SHORT).show();
 }
}

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: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"
     android:orientation="vertical" 
     tools:context="com.example.arjun.storage.MainActivity"> 
    
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName"
     android:hint="Name:" 
     android:ems="10" 
     android:id="@+id/idName"  />
    
    <LinearLayout
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
    
        <Button 
         android:text="Save To File" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:layout_margin="5dp" 
         android:id="@+id/idBtnSave" /> 
        <Button 
         android:text="Read File" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:layout_margin="5dp"
         android:id="@+id/idBtnRead" /> 
    </LinearLayout>  
    <TextView 
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />
</LinearLayout>