Search code examples
javaandroidandroid-studioandroid-sharedpreferences

SharedPreferences how to save text view and bg color


hi all first thing i was looking for answer all day for this and i dont find out nothing i try to save a simple thing i need to save the background color and the "points" that the user pres on a button i will explain the user need to press button that change background color and after a button that increse textview by +1.

after i close the app and return i want that the app will show the last background color that the user chose and how ,much time the +1 button was clicked:

here the code: xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    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.example.hanansanag.colorbeck.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="home work :)"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:textStyle="bold"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="change bg :"
        android:textStyle="bold"
        android:textSize="15sp"
        android:id="@+id/Tv2"
        android:textAllCaps="false"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/cred"
            android:text="red"
            android:background="@drawable/redbutton"
            android:textAllCaps="false"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:text="green"
            android:id="@+id/cgreen"
            android:layout_marginLeft="10dp"
            android:background="@drawable/greenbtm"
            android:textAllCaps="false"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:background="@drawable/blue222"
            android:id="@+id/cblue"
            android:layout_marginLeft="10dp"
            android:text="blue"
            android:textAllCaps="false"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="70dp"
            android:layout_height="70dp"

            android:background="@drawable/easyyyyy"
            android:id="@+id/btnadd"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:id="@+id/tv"
            android:textSize="20sp"
            android:layout_marginLeft="70dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="70dp"
            android:id="@+id/save"
            android:layout_height="70dp"
            android:layout_marginLeft="110dp"
            android:background="@drawable/save"/>
    </LinearLayout>


</LinearLayout>

and here is the java code :

    package com.example.hanansanag.colorbeck;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnred,btngreen,btnblue,btnpluse,btnsave;
    private LinearLayout mlinearLayout;
    private TextView tv;
    int point = 0;
    public SharedPreferences sp;
    private int progress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp= getSharedPreferences("save",0);
        String textValue = sp.getString("textvalue","");

        mlinearLayout=(LinearLayout)findViewById(R.id.activity_main);

        btnred = (Button) findViewById(R.id.cred);
        btnblue = (Button) findViewById(R.id.cblue);
        btngreen = (Button) findViewById(R.id.cgreen);
        btnpluse = (Button)findViewById(R.id.btnadd);
        btnsave = (Button)findViewById(R.id.save);
        tv=(TextView)findViewById(R.id.tv) ;
        btnred.setOnClickListener(this);
        btnsave.setOnClickListener(this);
        btnpluse.setOnClickListener(this);
        btnblue.setOnClickListener(this);
        btngreen.setOnClickListener(this);


        }



    @Override
    public void onClick(View v) {
        if (btnred == v){
            mlinearLayout.setBackgroundColor(Color.RED);

        }
        else if (btngreen == v){
            mlinearLayout.setBackgroundColor(Color.GREEN);
        }
        else if (btnblue == v){
            mlinearLayout.setBackgroundColor(Color.BLUE);

        }
        else if (btnpluse== v){
            point++;
            tv.setText("" + point);

        }
        else if (btnsave == v){
            Toast.makeText(this,"btn clickd",Toast.LENGTH_LONG).show();
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("textValue",tv.getText().toString());
            editor.commit();
        }
    }
}

like you see i try here SharedPreferences on the Textview but after i close and return i dont see nothing i know i do something wrong but i dont know what. ty all for the time and help


Solution

  • Change the SharedPreference bits in btnsave to:

    SharedPreferences.Editor editor = getSharedPreferences(save, MODE_PRIVATE).edit();
     editor.putString("textValue",tv.getText().toString());
     editor.commit();
    

    And in OnCreate change the SharedPreference bits to:

    SharedPreferences prefs = getSharedPreferences(save, MODE_PRIVATE); 
    String textValue= prefs.getString("textValue", "0");
    

    And add this code after you have declared tv as a TextView.

    tv.setText(textValue)
    

    In each button for changing background colour, put this code and change my variable 'COLOUR' to 'BLUE', 'RED' etc.

    SharedPreferences.Editor editor = getSharedPreferences(saveColour, MODE_PRIVATE).edit();
    editor.putString("colourValue",COLOUR);
    editor.commit();
    

    Then on create, call this:

    SharedPreferences prefs = getSharedPreferences(saveColour, MODE_PRIVATE); 
    String colourValue = prefs.getString("colourValue", WHITE);
    

    And after you have declared mLinearLayout:

    mlinearLayout.setBackgroundColor(Color.colourValue);