Search code examples
androidpopupmessage

Is it possible to make a pop up message without clicking on any button?


   AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
    myAlert.setMessage(username)
            .setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .setTitle("Welcome To iShop!")
            .create();
    myAlert.show();

It is possible to make it just popup when the activity starts?


Solution

  • Just put the code you wrote inside the the Activity's onCreate() method.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
        myAlert.setMessage(username)
            .setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .setTitle("Welcome To iShop!")
            .create();
        myAlert.show();
    }