Posted By Our Mentors
Amreen Khan
Android Developer
animesh bansal
iphone App Developer
Devendra chouksey
Android Developer
Dilip Saket
IOS apps developer
Hemraj Jhariya
iOS Developer
Jaihind singh Rajput
Smart Phone App Dev
Mujahid khan
Android developer
Narendra Yadav
android developer
Narinder Gupta
Mobile App Architect
Pradeep singh Thakur
Mobile App Architect
Sanjay Sharma
Andriod Developer
Shakti Rajpal
IT Architect
Soniya Vishwakarma
iPhone App Developer
Sunil Raghuwanshi
iPhone App Developer
Supriya Kapse
Senior developer
View current Topics
How to store user preferences in Android Android
While developing apps for mobile devices, we often come across situation where we want to store user preferences so that user experience gets more friendly and more customized. In this article we will learn how we can manage these preferences in Android.
Posted By: Mujahid khan Date: 11 Jul 2012 View: 1011
      Rate This !!


This article assumes that you have basic knowledge of Android framework and some basic knowledge of developing Android apps. To be able to run this project, you should have Android development environment set up and working in your windows system, if you don’t  have both or any one of the above mentioned requisites, I highly recommend you to go through this article first. Check itout here  

 http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=3e2bcb43-27c0-4d50-9d41-b3b97e69f981

Since, full source code is available below for a sample project, I am not going to cover the basics, I will be pointing to how we achieve maintaining user preferences instead.

For storing values such as user id, passwords or some other values that you want your application to remember, Android provides a mechanism called ‘SharedPreferences’. By using this, we can store values as key-value pair in a folder private to our app. However,  you can control this information to private to your app or world readable.

To  store values, we need to get an instance of SharedPreferences.Editor class which have methods like

putInt(String key, int val),  putString(String key, String val), commit()  etc. After putting values, we need to commit values by calling commit() method.  This is done like this :

SharedPreferences sp = getSharedPreferences(LOGIN_CREDENTIALS, MODE_PRIVATE);

SharedPreferences.Editor editor;

editor = sp.edit();

editor.putString("user_id", uid); 

        editor.putString("password", password); 

        editor.commit();

While for retrieving data, we simply use ShredPreferences object initiated with the same name that was used to store the values, in our case it is String LOGIN_CREDENTIALS = "login_credentials"

To retrieve :

String loaded_uid = sp.getString("user_id", "");;

      String loaded_password = sp.getString("password", "");

Where sp is the ShredPreferences object.

That’s it, we have used this functionality for implementing “remember me” functionality for user id and password.

 

Any queries or suggestions are welcome

Regards,

Mujahid Hussain Khan

I cheated on my boyfriend men and women open

Posted Queries