info@thistimebd.com

Sunday 28th of April 12:06:04am

shared preference android app source code part two

MainActivity.java

====================

package com.sam.web.sharedpreference;


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 implements View.OnClickListener

{

    private TextView scoreTextView;

    private Button increaseButton, decreaseButton;

    private int score=0;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        scoreTextView=(TextView) findViewById(R.id.TextViewId);

        increaseButton=(Button) findViewById(R.id.increaseScore);

        decreaseButton=(Button) findViewById(R.id.dcreaseScore);

        increaseButton.setOnClickListener(this);

        decreaseButton.setOnClickListener(this);


        if(loadScore()!=0){


            scoreTextView.setText("Score : "+loadScore());

        }


    }



    @Override

    public void onClick(View view) {

        if (view.getId()==R.id.increaseScore){

            score=score+10;

            scoreTextView.setText("Score : "+score);

            saveScore(score);



        }


        else if (view.getId()==R.id.dcreaseScore){

            score=score-10;

            scoreTextView.setText("Score : "+score);

            saveScore(score);




        }


    }private void saveScore(int score){


        SharedPreferences sharedPreferences=getSharedPreferences("gameScore",Context.MODE_PRIVATE);

        SharedPreferences.Editor editor=sharedPreferences.edit();

        editor.putInt("lastScore",score);

        editor.commit();

}

private int loadScore(){

    SharedPreferences sharedPreferences=getSharedPreferences("gameScore",Context.MODE_PRIVATE);

    int score=sharedPreferences.getInt("lastScore",0);

    return score;



}

}

==========================================================================================================

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:orientation="vertical"

    tools:context="com.sam.web.sharedpreference.MainActivity">




    <TextView


        android:layout_marginTop="10dp"

        android:text="Score : 0"

        android:textAlignment="center"

        android:textStyle="bold"

        android:textSize="20sp"

        android:id="@+id/TextViewId"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

    

    <Button

        android:layout_marginBottom="10dp"

        android:textStyle="bold"

        android:textSize="17sp"

        android:text="Increase score"

        android:id="@+id/increaseScore"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />


    <Button


        android:textStyle="bold"

        android:textSize="17sp"

        android:text="Decrease score"

        android:id="@+id/dcreaseScore"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />


</LinearLayout>


====================================================================================

styles.xml

=============

<resources>


    <!-- Base application theme. -->

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/colorPrimary</item>

        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

        <item name="colorAccent">@color/colorAccent</item>

    </style>


</resources>