ArhamTechMind

SharedPreferences 2

MainActivity.java

package com.blogspot.csdevbin.sharedpreferences;

import android.content.Context;
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.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText
food,shopping,fuel,telephone;
    TextView
previousExpense;
    SharedPreferences
sharedPreferences;

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

         food = (EditText)findViewById(R.id.etfood);
        
shopping = (EditText)findViewById(R.id.edShopping);
        
fuel = (EditText)findViewById(R.id.edFuel);
        
telephone = (EditText)findViewById(R.id.etTelephone);
        
previousExpense = (TextView) findViewById(R.id.textView_head);

        Button calculate = (Button)findViewById(R.id.button_calc);
       
sharedPreferences =getSharedPreferences(“EXPENCES”, Context.MODE_PRIVATE);
       
previousExpense.setText(String.valueOf(sharedPreferences.getInt(“EXP”,0)));

        calculate.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {
               
int expense =Integer.valueOf(food.getText().toString()) +
                        Integer.valueOf(
shopping.getText().toString()) +
                        Integer.valueOf(
fuel.getText().toString()) +
                        Integer.valueOf(
telephone.getText().toString()) ;

                Toast.makeText(MainActivity.this, “Total Expense is : ” +String.valueOf(expense), Toast.LENGTH_SHORT).show();
                
previousExpense.setText(“Total Previous  Expense was : ” +String.valueOf(expense));

                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putInt(
“EXP”,expense);
                edit.apply();

            }
        });
    }
}

activity_main.xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/activity_main"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
tools:context="com.blogspot.csdevbin.sharedpreferences.MainActivity">

    <
TextView

       
android:text="Previous Expense"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_alignParentTop="true"
       
android:layout_centerHorizontal="true"
       
android:layout_marginTop="18dp"
       
android:id="@+id/textView_head"
       
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <
TextView

       
android:text="Food"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/textView_head"
       
android:layout_alignParentLeft="true"
       
android:layout_alignParentStart="true"
       
android:layout_marginLeft="21dp"
       
android:layout_marginStart="21dp"
       
android:layout_marginTop="74dp"
       
android:id="@+id/textView_food" />

    <
EditText

       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:inputType="number"
       
android:ems="10"
       
android:id="@+id/etfood"
       
android:layout_alignBottom="@+id/textView_food"
       
android:layout_alignLeft="@+id/textView_head"
       
android:layout_alignStart="@+id/textView_head" />

    <
TextView

       
android:text="Shopping"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/textView_food"
       
android:layout_alignLeft="@+id/textView_food"
       
android:layout_alignStart="@+id/textView_food"
       
android:layout_marginTop="51dp"
       
android:id="@+id/textView_shopping" />

    <
EditText

       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:inputType="number"
       
android:ems="10"
       
android:layout_alignBottom="@+id/textView_shopping"
       
android:layout_alignLeft="@+id/etfood"
       
android:layout_alignStart="@+id/etfood"
       
android:id="@+id/edShopping" />

    <
TextView

       
android:text="Fuel"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/textView_shopping"
       
android:layout_alignLeft="@+id/textView_shopping"
       
android:layout_alignStart="@+id/textView_shopping"
       
android:layout_marginTop="68dp"
       
android:id="@+id/textView_fuel" />

    <
TextView

       
android:text="Telephone"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:id="@+id/textView_tele"
       
android:layout_below="@+id/textView_fuel"
       
android:layout_alignLeft="@+id/textView_fuel"
       
android:layout_alignStart="@+id/textView_fuel"
       
android:layout_marginTop="56dp" />

    <
EditText

       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:inputType="number"
       
android:ems="10"
       
android:id="@+id/etTelephone"
       
android:layout_alignBottom="@+id/textView_tele"
       
android:layout_alignLeft="@+id/edFuel"
       
android:layout_alignStart="@+id/edFuel" />

    <
Button

       
android:text="Calculate"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
        
android:layout_alignParentBottom="true"
       
android:layout_marginBottom="28dp"
       
android:id="@+id/button_calc"
       
android:layout_alignRight="@+id/etTelephone"
       
android:layout_alignEnd="@+id/etTelephone"
       
android:layout_alignLeft="@+id/textView_tele"
       
android:layout_alignStart="@+id/textView_tele" />

    <
EditText

       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:inputType="number"
       
android:ems="10"
        
android:id="@+id/edFuel"
       
android:layout_alignBottom="@+id/textView_fuel"
       
android:layout_alignLeft="@+id/edShopping"
       
android:layout_alignStart="@+id/edShopping" />

</
RelativeLayout>

Leave a Comment

Your email address will not be published. Required fields are marked *