JRad the Bad
JRad the Bad

Reputation: 511

AlertDialog won't show in onCreate method

So I want to ask a user their name before continuing the build of an activity. Most of the activity is populated dynamically so it seems like this should be easy. For some reason, the Dialog never appears though. I've tried everything and the only thing I can think of is: Perhaps it doesn't like being in the onCreate method? It doesn't seem like this should be an issue though since it's literally the last method being called in onCreate. Check it out and let me know what you see:

onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initializeHotels();
    FIRST_TURN = true;

    clearOldBoard();
    setContentView(R.layout.activity_game_board);
    setUpBoardGUI();

    setOnPlayerSetUpEventListener(new onPlayerSetUpEventListener() {
        @Override
        public void onPlayerSetUp(){
            prepForFirstTurn();
        }
    }); 

    startGameDialog();
}

And the startGameDialog method:

public void startGameDialog(){
    Context context = getApplicationContext();

    ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.AppBaseTheme);

    AlertDialog.Builder startGameDialog = new AlertDialog.Builder(ctw);
    startGameDialog.setTitle(getResources().getString(R.string.whats_your_name));

    LinearLayout dialogLayout = new LinearLayout(context);

        final EditText newName = new EditText(context);
        newName.setText("");

        Button submit = new Button(context);
        OnClickListener onClick = new OnClickListener() {

            @Override
            public void onClick(View v) {
                GameBoardActivity.NAME = newName.toString();
                setUpPlayers();

            }
        };
        submit.setOnClickListener(onClick);

    dialogLayout.addView(newName);
    dialogLayout.addView(submit);

    startGameDialog.setView(dialogLayout);
    Dialog dialog = startGameDialog.create();
    dialog.show();
    dialog.setCancelable(false);

}

Upvotes: 1

Views: 1586

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

The create method return an instance of AlertDialog

Instead of initializing it to the Dialog when you call the create method of the AlertDialog

Dialog dialog = startGameDialog.create();
dialog.show();

pass it to the AlertDialog

AlertDialog alertDialog = startGameDialog.create();
alertDialog.show();

use the currents activity context rather than using the whole application contex.t

Context context = Your_activity.this;

Upvotes: 4

Related Questions