Can't cancel dialog with custom buttons

How can I dismiss my own dialogue? I am getting an error with .cancel()

and .dismiss()

as if they are not allowed outside.setpositive/Negative button

Tried this one but still doesn't work.

this is my dialog code:

public void showSettingsAlert(){

    final AlertDialog.Builder alertdialog = new AlertDialog.Builder(mcontext);
    LayoutInflater inflater = LayoutInflater.from(mcontext);
    final View customView = inflater.inflate(R.layout.custom_gps,null);
    alertdialog.setView(customView);
    alertdialog.setCancelable(true);
    FlatButton bouton_ok = (FlatButton)customView.findViewById(R.id.custom_ok_button);
    FlatButton bouton_quitter = (FlatButton)customView.findViewById(R.id.custom_cancel_button);

    bouton_ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mcontext.startActivity(intent);


        }
    });
    bouton_quitter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //to close the whole application : 
            finish();
            System.exit(0);
        }
    });
    alertdialog.show();
}

      

In my host work, I check if gps is enabled or not:

    protected void onResume() {
    super.onResume();
    gps = new GPSTracker(MainActivity.this);
    // check if GPS enabled
    if(gps.canGetlocation() ){
        //stuff...
    }else{
        gps.showSettingsAlert();
    }
}

      

+3


source to share


1 answer


AlertDialog.Builder is used to create an alert dialog. The create () method then returns an AlertDialog object, which allows you to call the fire () function.



AlertDialog.Builder builder = new AlertDialog.Builder(this);

LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.brush_opts_dialog,null);
builder.setView(dialogView);

closeBtn = (Button)dialogView.findViewById(R.id.close_btn);

final AlertDialog dialog = builder.create();

closeBtn .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    dialog.dismiss();
}
});

dialog.show();

      

+5


source







All Articles