1

I would like to do many button in one page,and that page when user use the android phone to click one of the button in that page, other all button will disabled and only the button clicked will become red and when reopen the application, the button clicked still is red. If who know how to do, please tell me. Below is my coding:

   <Button
       android:text="Button"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button" />

   <Button
       android:text="Button"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button2" />

   <Button
       android:text="Button"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button3" />

   <Button
       android:text="Button"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button4" />

This is my main activity xml.

public class MainActivity extends AppCompatActivity {
    Button button,button1,button2,button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);
    button1 = (Button) findViewById(R.id.button2);
    button2 = (Button) findViewById(R.id.button3);
    button3 = (Button) findViewById(R.id.button4);

    if(button.isClickable()) {


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button.setEnabled(false);
                button.setBackgroundColor(Color.RED);
                button1.setEnabled(false);
                button2.setEnabled(false);
                button3.setEnabled(false);
            }
        });
    }
    if(button1.isClickable()) {


        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button.setEnabled(false);
                button1.setBackgroundColor(Color.RED);
                button1.setEnabled(false);
                button2.setEnabled(false);
                button3.setEnabled(false);
            }
        });
    }
    if(button2.isClickable()) {


        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button.setEnabled(false);
                button2.setBackgroundColor(Color.RED);
                button1.setEnabled(false);
                button2.setEnabled(false);
                button3.setEnabled(false);
            }
        });
    }
    if(button3.isClickable()) {


        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button.setEnabled(false);
                button3.setBackgroundColor(Color.RED);
                button1.setEnabled(false);
                button2.setEnabled(false);
                button3.setEnabled(false);
            }
        });
    }
    Log.e("onCreate: ", "UniqueKey: " + getUniqueKey());
}

public String getUniqueKey(){
    String android_id = Settings.Secure.getString(this.getContentResolver(),
            Settings.Secure.ANDROID_ID);
    return android_id;
}

} This is my main activity java. This is every android phone can click the button one time, after click and reopen the application, the button will disabled and red and also other button will disable ,thanks

lun L
  • 47
  • 8
  • https://stackoverflow.com/questions/44962705/how-to-hide-views-for-always/44962743#44962743 this will help you . – Adnan Maqbool Jul 16 '17 at 09:04
  • above link store states for visiblity you can use it for click enable/disable – Adnan Maqbool Jul 16 '17 at 09:05
  • can show me the code, i dont know understand how to save – lun L Jul 16 '17 at 09:09
  • check my answer , i think its clear . – Adnan Maqbool Jul 16 '17 at 09:20
  • cannot, only first button disabled, other button still can click......., i want do is after user use android phone click one of the button, every button will disabled and the button clicked will become red, mean he or she choose that button, and close the application, open again, the button clicked just now still red and disabled and other button also disabled..... – lun L Jul 16 '17 at 10:02

3 Answers3

0

Save the state of the button in SharedPreferences , and when reopening the application again, check the value inside preferences and load the layout.

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
  • can show me the code, i dont know how to save the state of button in shared prederences ,thanks – lun L Jul 16 '17 at 09:04
0

There is an option called SharedPreferences in Android which allows you to save the instance of any state of your App to the memory and then to retrieve it from the settings. So just save the button ID to the SharedPreferences and when you open the application get the ID of the button in the preference and then disable the other buttons you need i be disabled.

Praveen Thirumurugan
  • 977
  • 1
  • 12
  • 27
0

Shared Preference Example

1.store value in sharedPrefernces:

SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("storevalue", false);
editor.commit();

2.get value from sharedPreferences: In Oncreate of your Activity

   SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
    preferences.getBoolean("storevalue", false);

    if(preferences.getBoolean("storevalue", false))
    {
    button.setclickable(true);
   button.setenable(true);
    }
    else{
     button.setclickable(false);
   button.setenable(false);
}
  1. onButtonCLikListner update sharefpreffernce value

    final Button button=new Button(this); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {

                SharedPreferences preferences = getApplicationContext().getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
    
                if(preferences.getBoolean("storevalue", false))
                {
    
    
                    editor.putBoolean("storevalue", false);
                    editor.commit();
                }
                else{
    
                    editor.putBoolean("storevalue", true);
                    editor.commit();
                }
    
                button.setclickable(true);
                button.setenable(true);
    
    
    
            }
        });
    
Adnan Maqbool
  • 452
  • 3
  • 10
  • i would like ask the 2 code above, both put in oncreate? or got one put in onclick? – lun L Jul 16 '17 at 09:36
  • SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("storevalue", false); editor.commit(); this code i put in onclick button SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE); preferences.getBoolean("storevalue", false); if(preferences.getBoolean("storevalue", false)) { button.setclickable(true); button.setenable(true); } else{ button.setclickable(false); button.setenable(false); } this one i put in oncreate – lun L Jul 16 '17 at 09:46
  • but when i open application, only first button disable and other button still can click – lun L Jul 16 '17 at 09:47
  • You also have to set otherbutton.setclicable(false) and otherbutton.setenable(false); – Adnan Maqbool Jul 16 '17 at 10:27
  • But this code look like is direct set the button become disabled, what I want do is after click button, it just disabled and reopen it still disabled – lun L Jul 16 '17 at 10:30
  • still same, cannot – lun L Jul 16 '17 at 12:11
  • i want make sure the thing, no.1 code is put at where? no.2 code is put on oncreate of main activity? – lun L Jul 16 '17 at 12:13
  • You have to first Search on Basic's of android than you will give better Idea. – Adnan Maqbool Jul 16 '17 at 12:16