0

Hi I am total beginner in java and android stuff but I wanted to make an activity that works like settings: changing the background color and font color as well.

package com.example.bartek.smb;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class OptionActivity extends AppCompatActivity {
private Button but1;
private Button but2;
private View view;

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

    view =  this.getWindow().getDecorView();
    but1 = findViewById(R.id.rednut);
    but2 = findViewById(R.id.greenbut);
}
    public void goRed(View v){
        but1.setTextColor(Color.RED);
        view.setBackgroundResource(R.color.red);
    }

public void goGreen(View v){
    but2.setTextColor(Color.GREEN);
    view.setBackgroundResource(R.color.green);
}

}

So I made 2 buttons that change font color and background but it only applies to current activity and disappears after shutting the device down. I have read about shared preferences but I don't know how to implement it.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
  • https://stackoverflow.com/questions/23024831/android-shared-preferences-example this link could help. you should save your color in a variable and save it in sharedprefrence – Ali Khaki Oct 28 '18 at 18:10

2 Answers2

1
public class OptionActivity extends AppCompatActivity {
private String MY_PREFS_NAME = "sharedPref";
private Button but1;
private Button but2;
private View view;

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

    view =  this.getWindow().getDecorView();
    but1 = findViewById(R.id.rednut);
    but2 = findViewById(R.id.greenbut);
}
    public void goRed(View v){  
        //Set shared preference to red
        SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
        editor.putInt("backgroundColor", Color.RED);
        editor.commit();
        but1.setTextColor(Color.RED);
        view.setBackgroundResource(R.color.red);

    }

public void goGreen(View v){
    //Set shared preference to green
    SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
    editor.putInt("backgroundColor", Color.GREEN);
    editor.commit();
    but2.setTextColor(Color.GREEN);
    view.setBackgroundResource(R.color.green);
}

}

Whenever you need to retrieve the value then you can use this.

    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    int backgroundColor = prefs.getInt("backgroundColor",0);

    but2.setBackgroundColor(backgroundColor);
Martin Lund
  • 1,159
  • 9
  • 17
0

Incorporate SharedPreferences like as it is in onCreate() below

 Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_option);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());


    view =  this.getWindow().getDecorView();
    but1 = findViewById(R.id.rednut);
    but2 = findViewById(R.id.greenbut);
    if (prefs != null){
        String s = prefs.getString("button1colour");
        String st = prefs.getString("button2colour");
        if (s.equals("red")){

           but1.setTextColor(Color.red);          
           view.setBackgroundResource(R.color.red);
        } 
        if (st.equals("green")) {

         but2.setTextColor(Color.GREEN);       
         view.setBackgroundResource(R.color.green);
        }
    }
}
public void goRed(View v){
    but1.setTextColor(Color.RED);
    view.setBackgroundResource(R.color.red);

    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("botton1colour", red);
    editor.commit();
}

public void goGreen(View v){
   but2.setTextColor(Color.GREEN);
   view.setBackgroundResource(R.color.green);

   SharedPreferences.Editor editor = prefs.edit();
   editor.putString("botton2colour", green);
   editor.commit();
}

}