So, I’ve got this code for an AlertDialog in an Android activity, and although it works and pops up at the correct moment, when I press the “OK” buttons it’s supposed to save the 2 variables which I put in, into 2 Strings which after the activity should be able to use those strings.
private void showDialog(){
AlertDialog.Builder alertdg = new AlertDialog.Builder(this);
alertdg.setTitle("Choose page");
alertdg.setMessage("Choose episode/page");
final EditText page = new EditText(this);
final EditText episode = new EditText(this);
page.setWidth(210);
episode.setWidth(210);
LinearLayout layout = new LinearLayout(this);
layout.addView(episode);
layout.addView(page);
alertdg.setView(layout);
alertdg.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
episodeString = episode.getText().toString();
pageString = page.getText().toString();
}
});
alertdg.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertdg.show();
}
And yes if you’re wondering I already declared the 2 strings “episodeString” and “pageString” somewhere in the start of the activity, and I know I declared them correctly. Now what I’m wondering is, why can’t I return those values so that the rest of the activity can use them? I’ve tried many times but the Dialog just won’t return/save values… What am I doing wrong?
You code absolutely works