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