9 Getpreferences

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

//// strings.

xml

<resources>
<string name="app_name">SharedPreferencesEx</string>
<string name="action_settings">Settings</string>
<string name="name">name</string>
<string name="phone">phone</string>
<string name="address">address</string>
<string name="save">save</string>
<string name="load">load</string>
</resources>

////// content_main.xml

<?xml version="1.0" encoding="utf-8"?>


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="@string/name"/>

<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="3"
android:id="@+id/etname"/>

</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="@string/phone"/>

<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="3"
android:inputType="phone"
android:id="@+id/etphone"/>

</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="@string/address"/>

<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="3"
android:id="@+id/etaddress"/>

</TableRow>
</TableLayout>

///// menu_main.xml

<menu 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"
tools:context="com.example.sharedpreferencesex.MainActivity">

<item
android:id="@+id/action_save"
android:title="@string/save"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_load"
android:title="@string/load"
app:showAsAction="ifRoom"/>
</menu>

////// MainActivity.java

package com.example.sharedpreferencesex;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

EditText ename, ephone, eaddress;


String name="";
int phone;
String address="";
String nameK ="namekey";
String phoneK = "phonekey";
String addressK= "addresskey";

SharedPreferences sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

ename=(EditText)findViewById(R.id.etname);
ephone=(EditText)findViewById(R.id.etphone);
eaddress=(EditText)findViewById(R.id.etaddress);

sp=this.getPreferences(Context.MODE_PRIVATE);

if(sp.contains(nameK))
{
name=sp.getString(nameK, "");
ename.setText(name);
}

if(sp.contains(phoneK))
{
phone=sp.getInt(phoneK, 0);
ephone.setText(String.valueOf(phone));
}

if(sp.contains(addressK))
{
address=sp.getString(addressK, "");
eaddress.setText(address);
}

FloatingActionButton fab = findViewById(R.id.fab);


fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_save) {

name=ename.getText().toString();
phone=Integer.parseInt(ephone.getText().toString());
address=eaddress.getText().toString();

SharedPreferences.Editor e = sp.edit();

e.putString(nameK, name);
e.putInt(phoneK, phone);
e.putString(addressK, address);
e.commit();

return true;
}

else if (id == R.id.action_load) {

if(sp.contains(nameK))
{
name=sp.getString(nameK, "");
ename.setText(name);
}

if(sp.contains(phoneK))
{
phone=sp.getInt(phoneK, 0);
ephone.setText(String.valueOf(phone));
}

if(sp.contains(addressK))
{
address=sp.getString(addressK, "");
eaddress.setText(address);
}

return true;
}

return super.onOptionsItemSelected(item);
}
}

You might also like