Tuesday, March 16, 2021

Android Preference

 

Android Preferences Example

Android shared preference is used to store and retrieve primitive information. In android, string, integer, long, number etc. are considered as primitive data type.

Android Shared preferences are used to store data in key and value pair so that we can retrieve the value on the basis of key.

It is widely used to get information from user such as in settings.

Android Preferences Example

Let's see a simple example of android shared preference.

android preference directory output 1

activity_main.xml

Drag one textview and two buttons from the pallete.

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.preferences.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/txtPrefs"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_centerVertical="true"  
  14.         android:text="Data:" />  
  15.   
  16.     <Button  
  17.         android:id="@+id/storeinformation"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_below="@+id/showinformation"  
  21.         android:layout_centerHorizontal="true"  
  22.         android:layout_marginTop="18dp"  
  23.         android:text="Store Information" />  
  24.   
  25.     <Button  
  26.         android:id="@+id/showinformation"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_alignParentTop="true"  
  30.         android:layout_centerHorizontal="true"  
  31.         android:layout_marginTop="17dp"  
  32.         android:text="Show Information" />  
  33.   
  34. </RelativeLayout>  

array.xml

It is created inside res/values directory.

File: array.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="listOptions">  
  4.         <item>English</item>  
  5.         <item>Hindi</item>  
  6.         <item>Other</item>  
  7.     </string-array>  
  8.   
  9.     <string-array name="listValues">  
  10.         <item>English Language</item>  
  11.         <item>Hindi Language</item>  
  12.         <item>Other Language</item>  
  13.     </string-array>  
  14. </resources>  

prefs.xml

It is created inside res/xml directory.

File: prefs.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <PreferenceCategory  
  4.         android:summary="Username and password information"  
  5.         android:title="Login information" >  
  6.         <EditTextPreference  
  7.             android:key="username"  
  8.             android:summary="Please enter your login username"  
  9.             android:title="Username" />  
  10.         <EditTextPreference  
  11.             android:key="password"  
  12.             android:summary="Enter your password"  
  13.             android:title="Password" />  
  14.     </PreferenceCategory>  
  15.   
  16.     <PreferenceCategory  
  17.         android:summary="Username and password information"  
  18.         android:title="Settings" >  
  19.         <CheckBoxPreference  
  20.             android:key="checkBox"  
  21.             android:summary="On/Off"  
  22.             android:title="Keep me logged in" />  
  23.   
  24.         <ListPreference  
  25.             android:entries="@array/listOptions"  
  26.             android:entryValues="@array/listValues"  
  27.             android:key="listpref"  
  28.             android:summary="List preference example"  
  29.             android:title="List preference" />  
  30.     </PreferenceCategory>  
  31. </PreferenceScreen>  

Main Activity Class

File: MainActivity.java
  1. package example.javatpoint.com.preferences;  
  2.   
  3. import android.content.Intent;  
  4. import android.content.SharedPreferences;  
  5. import android.preference.PreferenceManager;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12. public class MainActivity extends AppCompatActivity {  
  13.     TextView textView;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         Button storeinformation = (Button) findViewById(R.id.storeinformation);  
  20.         Button showinformation = (Button) findViewById(R.id.showinformation);  
  21.         textView = (TextView) findViewById(R.id.txtPrefs);  
  22.   
  23.         View.OnClickListener listener = new View.OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 switch (v.getId()) {  
  27.                     case R.id.storeinformation:  
  28.                         Intent intent = new Intent(MainActivity.this,PrefsActivity.class);  
  29.                         startActivity(intent);  
  30.                         break;  
  31.                     case R.id.showinformation:  
  32.                         displaySharedPreferences();  
  33.                         break;  
  34.                     default:  
  35.                         break;  
  36.                 }  
  37.             }  
  38.         };  
  39.         storeinformation.setOnClickListener(listener);  
  40.         showinformation.setOnClickListener(listener);  
  41.     }  
  42.   
  43.   
  44.     private void displaySharedPreferences() {  
  45.         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);  
  46.         String username = prefs.getString("username""Default NickName");  
  47.         String passw = prefs.getString("password""Default Password");  
  48.         boolean checkBox = prefs.getBoolean("checkBox"false);  
  49.         String listPrefs = prefs.getString("listpref""Default list prefs");  
  50.   
  51.   
  52.         StringBuilder builder = new StringBuilder();  
  53.         builder.append("Username: " + username + "\n");  
  54.         builder.append("Password: " + passw + "\n");  
  55.         builder.append("Keep me logged in: " + String.valueOf(checkBox) + "\n");  
  56.         builder.append("List preference: " + listPrefs);  
  57.         textView.setText(builder.toString());  
  58.   
  59.     }  
  60.   
  61. }  

PrefsActivity class

File: PrefsActivity.java
  1. package example.javatpoint.com.preferences;  
  2.   
  3. import android.os.Bundle;  
  4. import android.preference.PreferenceActivity;  
  5.   
  6. public class PrefsActivity extends PreferenceActivity {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         addPreferencesFromResource(R.xml.prefs);  
  11.     }  
  12. }  

AndroidManifest.xml

File: AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="example.javatpoint.com.preferences">  
  4.   
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@mipmap/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:roundIcon="@mipmap/ic_launcher_round"  
  10.         android:supportsRtl="true"  
  11.         android:theme="@style/AppTheme">  
  12.         <activity android:name=".MainActivity">  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.   
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             </intent-filter>  
  18.         </activity>  
  19.         <activity  android:name=".PrefsActivity"  
  20.             android:theme="@android:style/Theme.Black.NoTitleBar" >  
  21.         </activity>  
  22.     </application>  
  23.   
  24. </manifest>  

Output:

android preference example output 1 android preference example output 2 android preference example output 3 android preference example output 4

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile