Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a button visible in one activity when clicking a radiobutton in another?

Im making a simple Androidgame , where the user selects an answer to a question (ind Activity1) by clicking a radiobutton. When the correct radiobutton is clicked, a button in the "Credits" (Activity2) will get VISIBLE and be available for the user.

How can I make this happen? i can´t get the two activities to work together?

The code from Activity 1 (the Question) where the user clicks the radiobutton:

 final Button s1 = (Button) findViewById(R.id.radio0);
 final Button s2 = (Button) findViewById(R.id.radio1);
 final Button s3 = (Button) findViewById(R.id.radio2);

 s1.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) {
      btnEliminar.setVisibility(View.VISIBLE);
      btnKort.setVisibility(View.VISIBLE);
      s1.setVisibility(View.GONE);
      s2.setVisibility(View.GONE);
      s3.setVisibility(View.GONE);




      AlertDialog.Builder builder = new AlertDialog.Builder(Activity1.this);
        builder.setMessage("...");
        builder.setCancelable(true);
        builder.setPositiveButton("...", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();

                }
            });

        AlertDialog alert = builder.create();
        alert.show();

        }

  });

The code from Activity2 where the button should get visible:

public class Activity2 extends Activity {

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);

        Button credit1 = (Button) findViewById(R.id.buttoncredit1);
        credit1.setVisibility(View.INVISIBLE);
        ....
        credit1.setVisibility(View.VISIBLE);

Hope someone is able to help me Thank you

like image 897
user1154161 Avatar asked Nov 27 '25 17:11

user1154161


1 Answers

This can be done via Intent's extras. Should look something like this:

//Somewhere in Activity1
Intent intent = new Intent();
intent.setClass(getApplicationContext(), Activity2.class);
intent.putExtra("makeButtonVisible", true); // Or false
startActivity(intent);

//Somewhere in Activity2
boolean isButtonVisible = getIntent().getBooleanExtra("makeButtonVisible");
// Change button's visibility accordingly
like image 148
Ash Avatar answered Nov 30 '25 07:11

Ash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!