0

I want to show a custom dialog box when I start my first activity without using a button. I try to search but I'm not finding the proper solution what I really want to do. Many of them are using onClick Listener to achieve that the scenario. Below image is showing an activity with a dialog box, that's what I'm looking for but without using onClick Listener.

enter image description here

How can we implement without using onClick Listener?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96

4 Answers4

1

Any code contained in a click listener also works elsewhere in the class (unless you use the view that's clicked)

Create the dialog in onCreate. It will open immediately when the activity is started

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

In your main Activity oncreate method:

createCustomizeDialog();

now create this method outside of oncreate:

private void createCustomizeDialog() {
        final AlertDialog.Builder builder=new AlertDialog.Builder(this);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        @SuppressLint("InflateParams") final View alertLayout = inflater.inflate(R.layout.customize_dialog, null);
        Button submit=(Button)alertLayout.findViewById(R.id.sButton);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }
        });
        builder.setView(alertLayout);
        alertDialog=builder.create();
        //noinspection ConstantConditions
        alertDialog.show();
    }
Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30
1

Just try this way

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("TADAAAA!").create().show();
}
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
-1

If you want to show the dialog only for the FIRST launch of this activity, you should put the code for your dialog in onCreate method of this activity, if it should be done for EVERY launch of this activity - then in onStart() method.

UmAnusorn
  • 10,420
  • 10
  • 72
  • 100
Hetfieldan24
  • 198
  • 11