I am working on an android application in which I am using an activity as a custom dialog.I've named my custom dialog activity as Dialog_activity
and my game activity as Activity1
. In Dialog_activity
, there are two buttons namely yes and no. The dialog asks the user if he/she wants to start a new game. So, how can I call a method from Activity1
in Dialog_activity
in the OnClick
method of the yes button. This is a tic tac toe application.
Here is the code:
Activity1
public class Dialog_activity extends Activity {
Button yesbutton,nobutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
Button btn4 = (Button) findViewById(R.id.button4);
Button btn5 = (Button) findViewById(R.id.button5);
Button btn6 = (Button) findViewById(R.id.button6);
Button btn7 = (Button) findViewById(R.id.button7);
Button btn8 = (Button) findViewById(R.id.button8);
Button btn9 = (Button) findViewById(R.id.button9);
}
public void resetButtons()
{
btn1.setText("");
btn2.setText("");
btn3.setText("");
btn4.setText("");
btn5.setText("");
btn6.setText("");
btn7.setText("");
btn8.setText("");
btn9.setText("");
}
}
Dialog Activity
public class Dialog_activity extends Activity {
Button yesbutton,nobutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.dialog_activity);
Intent startdialog = getIntent();
yesbutton = (Button) findViewById(R.id.button);
nobutton = (Button) findViewById(R.id.button2);
nobutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
yesbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
//call the resetButtons() method
}
});
Thanks!