0% found this document useful (0 votes)
15 views

Tutorial 5 - Data Storage

The document discusses different methods for data storage in Android applications including Shared Preferences, local files, and bundles. It provides code examples for storing and retrieving data from Shared Preferences and local files. It also demonstrates using bundles to retain data during configuration changes like orientation changes.

Uploaded by

batoolnf11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Tutorial 5 - Data Storage

The document discusses different methods for data storage in Android applications including Shared Preferences, local files, and bundles. It provides code examples for storing and retrieving data from Shared Preferences and local files. It also demonstrates using bundles to retain data during configuration changes like orientation changes.

Uploaded by

batoolnf11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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

)activity-main.xml( 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);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, textView.getText().toString());

editor.apply();
Toast.makeText(this, "Data saved", Toast.LENGTH_SHORT).show();
public void load1(View v) { String text = editText.getText().toString();
FileInputStream fis = null; FileOutputStream fos = null;
try { try {
fis = openFileInput(FILE_NAME); fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
InputStreamReader isr = new InputStreamReader(fis); fos.write(text.getBytes());
BufferedReader br = new BufferedReader(isr); editText.getText().clear();
StringBuilder sb = new StringBuilder(); Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME,
String text; Toast.LENGTH_LONG).show();
while ((text = br.readLine()) != null) { } catch (FileNotFoundException e) {
sb.append(text).append("\n"); e.printStackTrace();
} } catch (IOException e) {
text_View.setText(sb.toString()); e.printStackTrace();
} catch (FileNotFoundException e) { } finally {
e.printStackTrace(); if (fos != null) {
} catch (IOException e) { try {
e.printStackTrace(); fos.close();
} finally { } catch (IOException e) {
if (fis != null) { e.printStackTrace();
try { }
fis.close(); }
} catch (IOException e) { }
e.printStackTrace();
} }
}
}
Shared Preference

Demo
Bundle

8
)activity-main.xml(

<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) {
super.onSaveInstanceState(outState);

public void performAction(View view) {

switch (view.getId()){
outState.putInt("score",score);
case R.id.bincrement:

score +=1; }

break; @Override
case R.id.bshovValue: protected void onRestoreInstanceState( Bundle
Toast.makeText(getApplicationContext(),"Your Score savedInstanceState) {
is"+score,Toast.LENGTH_SHORT).show();
super.onRestoreInstanceState(savedInstanceState);
break;
}

}
score = savedInstanceState.getInt("score");
}
Bundel

Demo
Resources

● https://codinginflow.com/tutorials/android/sharedpreferences
● https://codinginflow.com/tutorials/android/write-text-file-to-internal-storage
● https://www.youtube.com/watch?v=W3NsvUX_Fwo

You might also like