Monday, August 31, 2015

// //

Android SharedPreferences example

Hello how are you? This time i have to teach you some important aspect in android programming.

 Do you want to know the sharedPreferences in android? will this time dont worry because i wrote a clean and easy to understand tutorial about that.

What is sharedpreferences? A preferences is used to store information in your application, ex. the setting of your application.

All Preferences can be declared in an xml file in which the PreferenceScreen is the root element. Some commonly used Preferences are CheckboxPreference, EditTextPreference, and ListPreference.

The ListPreference displays a list of items that can be selected. You should remember that a value to be stored in the SharePreferences can be one of the following types: boolean, float, int, long, and String.


Ok. so lets get started...

Make a folder and named it xml inside the res or (resources):
(Right-Click in the res and select new folder and name it xml)

Now you have your folder this time make a xml file in your newly created xml folder and name it, settings.xml..

settings.xml




    
        
        
        
        
        
         
    
    
    




I have to say that this time i am going to show you storing only the string data..so you've notice only EditTextPreference..

And in your strings.xml in values folder add this code:

 Set pet name
    Set address name
    please enter your pet name :)
       please enter your address :)
         who are you?
     please enter your name :)


And also in the dimens.xml in the values folder add this code:


    8dp
    8dp
    16dp


Now in your activity_main.xml in layout folder add this code:



    
    
    
    

And in your MainActivity.java add this code:
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 private static final int RESULT_SETTINGS = 1;
 Button button;
 public String a="dd";

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
 
   button = (Button) findViewById(R.id.btnoptions);
  
  
  setContentView(R.layout.activity_main);

 // showUserSettings();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.settings, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {

  case R.id.menu_settings:
   Intent i = new Intent(this, UserSettingActivity.class);
   startActivityForResult(i, RESULT_SETTINGS);
   break;

  }

  return true;
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  switch (requestCode) {
  case RESULT_SETTINGS:
   showUserSettings();
   break;

  }

 }

 private void showUserSettings() {
  SharedPreferences sharedPrefs = PreferenceManager
    .getDefaultSharedPreferences(this);

  StringBuilder builder = new StringBuilder();

  builder.append("\n Pet: "
    + sharedPrefs.getString("prefpetname", "NULL"));

  builder.append("\n Address:"
    + sharedPrefs.getString("prefaddress","NULL" ));

  builder.append("\n Your name: "
    + sharedPrefs.getString("prefname", "NULL"));

  TextView settingsTextView = (TextView) findViewById(R.id.textUserSettings);
  

  settingsTextView.setText(builder.toString());
  
 
 }

 
 public void yeah(View v){
  
  Intent i = new Intent(this, UserSettingActivity.class);
  startActivityForResult(i, RESULT_SETTINGS);
  
 }
 
 
}

Now make a new activity file and name it UserSettingActivity and then add this code.

UserSettingActivity.java
import android.os.Bundle;
import android.preference.PreferenceActivity;

public class UserSettingActivity extends PreferenceActivity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  addPreferencesFromResource(R.xml.settings);

 }
}

Now I think it's ready! Run it on the emulator.