Tutorial 5:
Android Data Storage
SWE 483 : Mobile Applications Development
Outline
● Shared Preference
● Local File
● Bundle
Shared Preference
3
MainActivity Class
You need to …
1. Add editText for write a data.
2. Add two Text view for display Shared preference and file data .
3. Add button for save data .
4. Add button for display data in text view.
5. Add another button for load data file.
6. Implement the Shared Preference and internal file storage(Next Slide)
<TextView
)[Link]( android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="30sp"
tools:text="Here will be our text" />
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/apply_text_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="apply text" />
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="save data" />
MainActivity Class public void saveData() {
SharedPreferences sharedPreferences =
getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
[Link] editor = [Link]();
[Link](TEXT, [Link]().toString());
[Link]();
[Link](this, "Data saved", Toast.LENGTH_SHORT).show();
public void load1(View v) { String text = [Link]().toString();
FileInputStream fis = null; FileOutputStream fos = null;
try { try {
fis = openFileInput(FILE_NAME); fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
InputStreamReader isr = new InputStreamReader(fis); [Link]([Link]());
BufferedReader br = new BufferedReader(isr); [Link]().clear();
StringBuilder sb = new StringBuilder(); [Link](this, "Saved to " + getFilesDir() + "/" + FILE_NAME,
String text; Toast.LENGTH_LONG).show();
while ((text = [Link]()) != null) { } catch (FileNotFoundException e) {
[Link](text).append("\n"); [Link]();
} } catch (IOException e) {
text_View.setText([Link]()); [Link]();
} catch (FileNotFoundException e) { } finally {
[Link](); if (fos != null) {
} catch (IOException e) { try {
[Link](); [Link]();
} finally { } catch (IOException e) {
if (fis != null) { [Link]();
try { }
[Link](); }
} catch (IOException e) { }
[Link]();
} }
}
}
Shared Preference
Demo
Bundle
8
)[Link](
<Button
android:id="@+id/bincrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="83dp"
android:layout_centerHorizontal="true"
android:onClick="performAction"
android:text="Increment counter" />
<Button
android:id="@+id/bshovValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bincrement"
android:layout_centerHorizontal="true"
android:onClick="performAction"
android:text="Show Counter Value" />
MainActivity Class
@Override
protected void onSaveInstanceState( Bundle outState) {
[Link](outState);
public void performAction(View view) {
switch ([Link]()){
[Link]("score",score);
case [Link]:
score +=1; }
break; @Override
case [Link]: protected void onRestoreInstanceState( Bundle
[Link](getApplicationContext(),"Your Score savedInstanceState) {
is"+score,Toast.LENGTH_SHORT).show();
[Link](savedInstanceState);
break;
}
}
score = [Link]("score");
}
Bundel
Demo
Resources
● [Link]
● [Link]
● [Link]