-4

I have two buttons in application with default color WHITE . When I click on first button it's color change to GREEN then I click second button it's color change to RED and button one to WHITE . It works fine but when I start application again color of both button is WHITE . What I want to do is retrieve previous button state . For example previously if I clicked button one then after start application again color of button one already become GREEN .

JAVA Code:

    package com.example.pratik.sf2020;

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

    public class Pattern extends AppCompatActivity {
    Button b1,b2;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.pattern);


            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            b1 = (Button) findViewById(R.id.mp1n);
            b2 = (Button) findViewById(R.id.mp1o);

            b1.setBackgroundColor(Color.WHITE);
            b2.setBackgroundColor(Color.WHITE);

            b1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                b1.setBackgroundColor(Color.GREEN);
                                b2.setBackgroundColor(Color.WHITE);


                            }
                        }
             });
             b2.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    b1.setBackgroundColor(Color.WHITE);
                                    b2.setBackgroundColor(Color.RED);


                                }
                            }
              });
        }

    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 2
    https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – Ricardo Oct 13 '17 at 09:03
  • may b u have to use sharedPreferences to store button values and then using getSharedPreferences()method retrieve the last saved values – Pranita Oct 13 '17 at 09:04

2 Answers2

0

You have to use SharedPreference to save the current events. Whenever you want to save some instant.

Click to see more...

Peter
  • 587
  • 6
  • 16
0

try this

SharedPreferences sp=getSharedPreferences("Button", Context.MODE_PRIVATE);
SharedPreferences.Editor Ed=sp.edit();

// get status of button to set backround from SharedPreferences in oncrate() methosd

   if(sp.getBoolean("button1",false)){
        b1.setBackgroundColor(Color.WHITE);
    }else {
        b1.setBackgroundColor(Color.GREEN);
    }
    if(sp.getBoolean("button2",false)){
        b2.setBackgroundColor(Color.WHITE);
    }else {
        b2.setBackgroundColor(Color.GREEN);
    }


// set button background status in SharedPreferences
b1.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
       b1.setBackgroundColor(Color.GREEN);
       b2.setBackgroundColor(Color.WHITE);
       Ed.putBoolean("button1", true);
       Ed.commit();

        }
     }
  });

   b2.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
               b1.setBackgroundColor(Color.WHITE);
               b2.setBackgroundColor(Color.RED);
               Ed.putBoolean("button2", true);
               Ed.commit();

             }
           }
      });
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • This works fine, but only works for single page .If I have two activities then the problem is same that i have asked in question. – Pratik Sonigra Oct 13 '17 at 10:56