Rashmi.B
Rashmi.B

Reputation: 1787

Android SharedPreferences

I have 3 webviews in my search engine app.When the user enters his query, by default he gets Google results, followed by yahoo and Ask buttons at the bottom, on clicking either of them, he gets results for that query from those sites. Now I want to give the user the privilege to change the default result site. I have created 3 radiobuttons. Upon confirmation, say he chooses Yahoo, how can i set it as Yahoo till the next time he changes it to some other site,

Upvotes: 0

Views: 3375

Answers (2)

Aman Aalam
Aman Aalam

Reputation: 11251

Accessing data from SharedPreferences:

SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
String webViewChoice = sharedPref.getString("userChoice","null");

if no choice was saved (in the case when the application is running for the first time), you'll get "null" in webViewChoice. use this condition as you wish

Saving data in SharedPreferences:

SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoice",usersChoice);
prefEditor.commit();

I hope it helps.

Upvotes: 13

Gopinath
Gopinath

Reputation: 13051

Save the user's preference as default-engine=google by default in a shared preferences file. On app loading, read the file and set the default engine during the app runtime. When user chooses a different engine as default, then update the preferences file.

Hope this helps.

Upvotes: 2

Related Questions