-3

I have a setting activity with a TextView and a set off buttons. I Want each button to changes the background of textviews in other activities.

I need example codes on how to do this.

Thank you all in advance.

Code:

           btn.setOnClickListener( new
            View .OnClickListener () {
            public boolean stateChanged;
            public void onClick(View view) {
                if (stateChanged) {
          // reset background to default;

                    tv.setBackgroundResource
                     (R.drawable.favon);
                } else {
                    tv.setBackgroundResource
                    (R.drawable.favoff);

                }
                stateChanged = !stateChanged;
D.Freeman
  • 1
  • 1
  • 3
  • Welcome to StackOverflow! Please read the user guidelines on how to ask a good question before posting a question (http://stackoverflow.com/help/how-to-ask) Thank You – Salman Khakwani Apr 12 '17 at 07:01
  • Android - Shared Preferences https://www.tutorialspoint.com/android/android_shared_preferences.htm – Chetan Joshi Apr 12 '17 at 07:02

1 Answers1

0

In a nutshell, you would start by initializing it. Use get to grab any saved value or default if nothing was found. Or use edit and put to store the data.

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
sharedPreferences.getInt("key", defaultValue);
sharedPreferences.edit().putInt("key", value).apply()

More

EDIT:

    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    // Set up how the background was before
    if (sharedPreferences.getBoolean("textViewBackground", true)) {
        // Default background
        tv.setBackgroundResource(R.drawable.favon);
    } else {
        // Other background
        tv.setBackgroundResource(R.drawable.favoff);
    }

    btn.setOnClickListener(new View.OnClickListener() {
        public boolean stateChanged;

        public void onClick(View view) {
            if (stateChanged) {
                // reset background to default;
                tv.setBackgroundResource(R.drawable.favon);
            } else {
                tv.setBackgroundResource(R.drawable.favoff);
            }
            stateChanged = !stateChanged;
            sharedPreferences.edit().putBoolean("textViewBackground", stateChanged).apply();
        }
    });
Community
  • 1
  • 1
Kia
  • 124
  • 1
  • 1
  • 10
  • Thanks but can you show me an example using my onclick code – D.Freeman Apr 12 '17 at 07:11
  • I edited my answer. Not sure how much you know about sharedPref, but it stores data on the phone so that later on when for example the app restarts it will continue from where it left off. In this case, we will save stateChanged, so before we set the listener, we are setting it to the background it was in last. – Kia Apr 12 '17 at 07:29
  • I know nothing about shared preference but thanks a lot let me try to implement that and will give you feed back if it worked. – D.Freeman Apr 12 '17 at 07:34
  • So I have implemented your code, its working great but here is the problem: if I click the button it sets "favon" as persistant background but when I click the button again "favoff" is set and when i exit and re launch the activity the background becomes "favon" when it still should be"favoff" – D.Freeman Apr 12 '17 at 07:49
  • I would need more context for that, but I would suggest setting 'stateChanged = sharedPreferences.getBoolean("textViewBackground", true)' right after 'PreferenceManager.getDefaultSharedPreferences(this);' Right now, stateChanged always starts from being false in your onClickListener since you don't set it to any values and by default it will be false. – Kia Apr 12 '17 at 16:35