I made a pop-up window using alertdialog at Android Studio. (This is a beginner)

Asked 2 years ago, Updated 2 years ago, 121 views

When you click the button in LoginActivity, you want to display a pop-up window before moving to the main screen and then move on to the main activity screen. When you run below, a pop-up window appears, but it disappears quickly before you click the close button and moves on to the main activity. Is there any way to keep the float long?

Is there any other way to make the Login Activity screen pop up instead of clicking the button? I'm a beginner, so I'd appreciate it if you could tell me the detailed code or method.ㅠ<

Can you tell me both ways? I'd like to apply the second method more. Thank you....

Below is LoginActivity.It's java.

public class LoginActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
    }


public void buttonOnClick(View v) {
        Button button = (Button) v;

        switch (v.getId()) {

            case R.id.button:

                new AlertDialog.Builder(LoginActivity.this)

                        .setTitle ("Alarm Pop-up")

                        .setMessage ("This is the content of the pop-up window."\n\n TEST!!")

                        .setNeutralButton("Close", new DialogInterface.OnClickListener() {


                            public void onClick(DialogInterface dlg, int sumthin) {

                            }


                        }).show(); // show popup window
                break;
        }

        startActivity(new Intent(getApplicationContext(), MainActivity.class));
    }
}

android alertdialog

2022-09-21 15:38

1 Answers

If you want to do it like #2, you can write the dialog window code in the onCreate() implementation section of LoginActivity. I think it'll be helpful if you search for Android life cycle.

To answer number 1,

If you look at the code above, you will exit to break immediately after the show(); is called.

Immediately after that, startActivity(); is called and the pop-up window is immediately turned off.

There is also a way to implement the dialog window in advance, but I think you can do it this way based on the current code. I haven't tested it, so if there are any mistakes, you can correct them little by little. I think you'd better search for the switch case door~

final AlertDialog builder = new AlertDialog.Builder(LoginActivity.this)
                        .setTitle ("Alarm Pop-up")
                        .setMessage ("This is the content of the pop-up window."\n\n TEST!!")
                        .setNeutralButton("Close", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dlg, int sumthin) {
                            }
                        }).show(); // show popup window

                Handler handler = new Handler();
                handler.postDelayed(new Runnable(){
                    public void run(){
                        builder.dismiss();
                        startActivity(new Intent(getApplicationContext(), MainActivity.class));
                    }
                }, //wanted time);
            }


2022-09-21 15:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.