Home

Friday 24 April 2015

Saving Data using Shared Preference

Shared Preference : 

Shared preference is used to save and retrieve the data within the app.
Note : Once the application is uninstalled the saved data gets erased.
There 2 process one to save and another to retrieve. The following code depicts :

MainActvity.java  :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.appyware.sharedpreference_demo;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    EditText editText;
    TextView textView;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.edittext);
        textView = (TextView) findViewById(R.id.textview);

        // Retrieve and hold contents
        sharedPreferences = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);

        // Get the string value and set text
        textView.setText(sharedPreferences.getString("key", null));

    }

    /* to save data using shared preference */
    public void save(View v) {
        if (editText.getText().toString().trim().length() > 0) {

            String text = editText.getText().toString();
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("key", text);
            editor.commit();
            textView.setText(sharedPreferences.getString("key", null));
        }

    }

}

Activity_main.xml :



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="Enter the text" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="save"
        android:text="Save" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="20dp"
        android:textSize="30sp" />
</LinearLayout>


Complete Source Code : SharedPreference-Demo

For any doubts you can comment below, i'll respond ASAP.

Monday 20 April 2015

Starting new Activity(Intents)

Starting new Activity :

Intents are used to transfer the flow of control from current activity to another. They used as messages which are passed and can also be used to pass data.

To create an Intent :

1
2
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(intent);

To pass data :

We will pass a string "Extra_Data" :


1
2
3
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
intent.putExtra("Extra_DataId", "Extra_Data");
startActivity(intent);

To receive data in another activity : 

1
String data= getIntent().getStringExtra("Extra_DataId");