Data stroage is important topic in application, to use it futher in app. Data stroage in android app can be done in three ways
- Shared preferences
- File System
- SQLite databse
Here I will discuss about Shared Pref.
Why Shared Pref?
- Shared Pref is used to store primitive type data in into xml file, we will see demo futhere.
- Using file system make coding complicated just to store simple data For example if user want to save some option in application we can use shared pref(like login data)
- SQLite is used when large number of data are used, using db for small data may affect runtime performance.
Example 1 : Simple Shared Pref Example
Step 1 : Create New Project
Structure of Project (Screen Shot)
Step 2 : Create UI for your application
User Inter face(Screen Shot)
Xml File
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="46dp"
android:layout_marginTop="52dp"
android:hint="Enter the text"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/remeber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_centerHorizontal="true"
android:layout_marginTop="155dp"
android:text="Remember" />
</RelativeLayout>
Step 2 :