How to open a Alertdialog?

Asked

Viewed 1,001 times

2

Well, I’m learning now about Fragments. From what I understand, fragments are components, something you want to repeat on several screens, without having to keep creating several classes or Activity’s.

I am 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 all the Dialog code, however I do not know how I 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 THAT WILL BE DISPLAYED THE DIALOG]:

/*=============================================================================== 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. ===============================================================================*/

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();*/
            }
        });

    }


}

1 answer

2


Create a variable of type DialogoStartGPS, which you have created, in which the class extends DialogFragment. Then use the method show to be launched on the screen by passing as parameter getFragmentManager and a string. See how:

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

That using the libs android.app.Dialog and android.app.DialogFragment. See 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();
    }
}
  • then I inserted it like this in my Aboutscreen.java, but it asks to create a method on the screen of my Fragment like this: public void show(Fragmentmanager fragmentManager, String s) { } MY CÒDIGO: mStartButton = (Button) findViewById(R.id.button_start); mStartButton.setOnClickListener(new Onclicklistener() { @Override public void onClick(View v) { Dialogostartgps dsg = new Dialogostartgps(); dsg.show(getFragmentManager(), "Jon Snow"); } });

  • 1

    @Thiagosaad you need to insert inside the button in your class AboutScreen, but the way I did in the DialogStartGPS using the libs android.app.Dialog and android.app.DialogFragment

  • @Thiagosaad replaces its class DialogoStartGPS by the one I shared in the reply, using the libs I mentioned.

  • Gave error @acklay, so I click the button. FATAL EXCEPTION: main Process: com.vuforia.samples.Books, PID: 3646 java.lang.Illegalstateexception: You need to use a Theme.Appcompat Theme (or Descendant) with this Activity.

  • 1

    thank you very much for the help, I got it! I had to take out such of . v4 and . v7

  • @Thiagosaad beauty! Need, I’m around!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.