Here you'll get help for all programming languages like android,php, html, css, bootstrap, javaScript, java etc...

Sharedpreferences in Android

In this example i'm going to show you how to work with sharedpreferences in android and how to access sharedpreferences values in another activity in android studio.



Example ::







activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name" />

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email" />

    <EditText
        android:id="@+id/contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Contact" />

    <EditText
        android:id="@+id/city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="City" />

    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Insert"
        android:id="@+id/btnInsert"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        />
    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="View"
        android:id="@+id/btnView"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        />
    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Open HomeActivity"
        android:id="@+id/btnHOmeActivity"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        />

</LinearLayout>




MainActivity.java



package com.example.malik.blog;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    EditText name, email, contact, city;
    Button insert, view, homeActivity;

    public static final String mainKey = "mainKey";
    public static final String nameKey = "nameKey";
    public static final String emailKey = "emailKey";
    public static final String contactKey = "contactKey";
    public static final String cityKey = "cityKey";

    SharedPreferences sharedPreferences;

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

        name = (EditText) findViewById(R.id.name);
        email = (EditText) findViewById(R.id.email);
        contact = (EditText) findViewById(R.id.contact);
        city = (EditText) findViewById(R.id.city);

        insert = (Button) findViewById(R.id.btnInsert);
        view = (Button) findViewById(R.id.btnView);
        homeActivity = (Button) findViewById(R.id.btnHOmeActivity);

        insert.setOnClickListener(this);
        view.setOnClickListener(this);
        homeActivity.setOnClickListener(this);

        sharedPreferences = getSharedPreferences(mainKey,MODE_PRIVATE);

        if(sharedPreferences.contains(nameKey)){
            name.setText(sharedPreferences.getString(nameKey,""));
        }

        if(sharedPreferences.contains(emailKey)){
            email.setText(sharedPreferences.getString(emailKey,""));
        }

        if(sharedPreferences.contains(contactKey)){
            contact.setText(sharedPreferences.getString(contactKey,""));
        }

        if(sharedPreferences.contains(cityKey)){
            city.setText(sharedPreferences.getString(cityKey,""));
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnInsert:

                String NAME = name.getText().toString();
                String EMAIL = email.getText().toString();
                String CONTACT = contact.getText().toString();
                String CITY = city.getText().toString();

                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(nameKey,NAME);
                editor.putString(emailKey,EMAIL);
                editor.putString(contactKey,CONTACT);
                editor.putString(cityKey,CITY);
                editor.commit();
                Toast.makeText(getApplicationContext(),"Data inserted",Toast.LENGTH_LONG).show();
                name.setText("");
                email.setText("");
                contact.setText("");
                city.setText("");

                break;

            case R.id.btnView:

                sharedPreferences = getSharedPreferences(mainKey,MODE_PRIVATE);

                if(sharedPreferences.contains(nameKey)){
                    name.setText(sharedPreferences.getString(nameKey,""));
                }

                if(sharedPreferences.contains(emailKey)){
                    email.setText(sharedPreferences.getString(emailKey,""));
                }

                if(sharedPreferences.contains(contactKey)){
                    contact.setText(sharedPreferences.getString(contactKey,""));
                }

                if(sharedPreferences.contains(cityKey)){
                    city.setText(sharedPreferences.getString(cityKey,""));
                }

                break;

            case R.id.btnHOmeActivity:
                Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                startActivity(intent);
                break;
        }
    }
}



activity_home.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".HomeActivity">

    <Button
        android:id="@+id/showValues"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="50dp"
        android:text="Show Value" />

    <TextView
        android:id="@+id/name_here"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="6dp"
        android:text="Name here"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/email_here"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="6dp"
        android:text="Email here"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/contact_here"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="6dp"
        android:text="Contact here"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/city_here"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="6dp"
        android:text="City here"
        android:textSize="25sp" />

</LinearLayout>




HomeActivity.java


package com.example.malik.blog;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HomeActivity extends AppCompatActivity {

    Button buttonShowValue;
    TextView name, email, contact, city;

    public static final String mainKey = "mainKey";
    public static final String nameKey = "nameKey";
    public static final String emailKey = "emailKey";
    public static final String contactKey = "contactKey";
    public static final String cityKey = "cityKey";

    SharedPreferences sharedPreferences;

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

        buttonShowValue = (Button) findViewById(R.id.showValues);
        name = (TextView) findViewById(R.id.name_here);
        email = (TextView) findViewById(R.id.email_here);
        contact = (TextView) findViewById(R.id.contact_here);
        city = (TextView) findViewById(R.id.city_here);

        buttonShowValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPreferences = getSharedPreferences(mainKey,MODE_PRIVATE);

                if(sharedPreferences.contains(nameKey)){
                    name.setText(sharedPreferences.getString(nameKey,""));
                }

                if(sharedPreferences.contains(emailKey)){
                    email.setText(sharedPreferences.getString(emailKey,""));
                }

                if(sharedPreferences.contains(contactKey)){
                    contact.setText(sharedPreferences.getString(contactKey,""));
                }

                if(sharedPreferences.contains(cityKey)){
                    city.setText(sharedPreferences.getString(cityKey,""));
                }
            }
        });

    }
}




AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.malik.blog">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".HomeActivity"/>
    </application>

</manifest> 




build.gradle


apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.malik.blog"
        minSdkVersion 18
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}



User Help

If you want any source code or any other help just contact me at malik_himani@ymail.com. Your requirement's code will be uploaded in blog soon.