How to open an AlertDialog?

1

Well, I'm learning about Fragments now. From what I understand, fragments are components, something you want to repeat on several screens, without having to create multiple class or activity's.

I'm trying to create a dialog, it will be using on a screen and will be displayed as soon as I press a certain button. I created the whole Dialog code, however I do not know how I would call it on the other screen.

- Dialogue :

package com.vuforia.samples.Books.Neoris.Componentes;

import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v4.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;

import com.vuforia.samples.Books.R;

public class DialogoStartGPS extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_start_gps)
                .setPositiveButton(R.string.permitir, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // FIRE ZE MISSILES!
                    }
                })
                .setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}
  • AboutScreen.java [SCREEN DISPLAYED DIALOGUE] :

/ * =================================================== =================================== Copyright (c) 2016 PTC Inc. All Rights Reserved.

Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved.

Vuforia is a trademark of PTC Inc., registered in the United States and other countries. countries. ====================================================================== ============================= * /

package com.vuforia.samples.Books.ui.ActivityList;

import com.vuforia.samples.Books.R;
import com.vuforia.samples.Books.Neoris.Componentes.DialogoStartGPS;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;



public class AboutScreen extends Activity {

    private Button mStartButton;
    private String mClassToLaunch;
    private String mClassToLaunchPackage;

    private AlertDialog alertGPS;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_screen);

        Bundle extras = getIntent().getExtras();
        mClassToLaunchPackage = getPackageName();
        mClassToLaunch = mClassToLaunchPackage + "." + extras.getString("ACTIVITY_TO_LAUNCH");

        alertGPS = new AlertDialog.Builder(DialogoStartGPS.class);
        mStartButton = (Button) findViewById(R.id.button_start);
        mStartButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                alertGPS.show();

               /* Intent i = new Intent();
                i.setClassName(mClassToLaunchPackage, mClassToLaunch);
                startActivity(i);
                finish();*/
            }
        });

    }


}
    
asked by anonymous 10.06.2017 / 00:58

1 answer

2

Create a variable of type DialogoStartGPS , which you created, in which it extends the DialogFragment class. Then use the show method to get it on the screen by passing it getFragmentManager and a string parameter. Here's how:

DialogoStartGPS dialogGPS = new DialogoStartGPS();
dialogGPS.show(getFragmentManager(), "Jon Snow!");

This is using libs android.app.Dialog and android.app.DialogFragment . Here's how it should look:

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

public class DialogoStartGPS extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Um teste qualquer")
                .setPositiveButton("Permitir", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // FIRE ZE MISSILES!
                    }
                })
                .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                });
        return builder.create();
    }
}
    
10.06.2017 / 01:49