Reputation: 5140
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!
Upvotes: 1
Views: 10500
Reputation: 309
1. import activity_1 2. write class variable Dialog_activity activity1 inside below class 3. call reset method using this activity1 reference activity1.resetButtons(); inside yesbutton onclocklistener
1. import activity_1
public class Dialog_activity extends Activity {
Button yesbutton,nobutton;
2. write class variable Dialog_activity activity1 inside below class
Dialog_activity activity1;
@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) {`enter code here`
finish();
**//call the resetButtons() method using reference activity1
activity1.resetButtons();
}
});
Upvotes: 0
Reputation: 11131
There two ways I can suggest,
1) start the Dialog activity from Game activity for result by using startActivityForResult() and override onActivityResult()
in GameActivity
2) Register GameActivity to listen for the Custom Intent action by using registerReceiver()
and broadcast that action in DialogActivity by using sendBroadCast()
Upvotes: 0
Reputation: 4659
Here are few options:
You can make reset button() as static but for that you will also have to make the buttons static.This method is not recommended.
Use this How to create a Custom Dialog box in android? to create a custom dialog and inflate this custom dialog in your Activity1 only .You can make CustomDialogClass as an inner class and thus it will be able to access all Activity1 methods.
Hope it helps.
Upvotes: 3
Reputation: 542
Make instance of Dialog_activity in Activity1 using the instance call the method which you want.
Class Activity1 extends Activity
{
Dialog_activity dialog_activity;
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
dialog_activity.the_method_you_want;
}
});
}
Upvotes: 2