EDECoder
EDECoder

Reputation: 59

Dialogue box does not show up

I am creating a dialogue box that should appear to the user in case of the GPS service is disabled. But what is happening is, although I disabled the service manually to force the dialogue appear, the App starts and nothing is happening.

The code below shows how I tried to create the dialogue box and when. Please let me know if there is any mistake.

JavaCode:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gpstest00);

    locMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    gpsEnable = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (!gpsEnable) {
        showGPSDialogueBox();
    } 
    locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this.locationListener);
}

/*if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}*/

private void showGPSDialogueBox() {
    AlertDialog.Builder alertDialogue = new AlertDialog.Builder(this);
    alertDialogue.setTitle("GPS Settings");
    alertDialogue.setMessage("GPS is deactivated. Do you want to switch " + to settings menu to activate it?");

    alertDialogue.setPositiveButton("Settings",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    });

    alertDialogue.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.cancel();

        }
    });
}// Enf of showDialogueBox function.

Upvotes: 0

Views: 58

Answers (2)

Hamid Shatu
Hamid Shatu

Reputation: 9700

You need to create AlertDialog from AlertDialog.Builder object then show the AlertDialog using show() method as follows...

AlertDialog dialog = alertDialogue.create();
dialog.show();

Update your showGPSDialogueBox() as below...

private void showGPSDialogueBox() {
    AlertDialog.Builder alertDialogue = new AlertDialog.Builder(this);
    alertDialogue.setTitle("GPS Settings");
    alertDialogue.setMessage("GPS is deactivated. Do you want to switch " +
            "                 to settings menu to activate it?");

    alertDialogue.setPositiveButton("Settings",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    });

    alertDialogue.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.cancel();

        }
    });

    AlertDialog dialog = alertDialogue.create();
    dialog.show();
}

Upvotes: 0

nandeesh
nandeesh

Reputation: 24820

You need to call show function for the dialog box to show

alertDialogue.show();

Upvotes: 1

Related Questions