How can I make a dialog appear when I click on an item in a menu?

1

I want to make a dialogue appear when I click on an item in a menu, I have an activity that does the following:

...
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_item_activity, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.eliminar) {
        eliminar();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void eliminar(){

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(android.R.id.content, new FragmentEliminarDialogo());
    ft.commit();


}

}

Content of FragmentEliminarDialogo.java

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceFragment;



@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class FragmentEliminarDialogo extends PreferenceFragment {

public FragmentEliminarDialogo() {
    // Constructor Por Defecto
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.dialog);
}


}

Content of dialog XML

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen     xmlns:android="http://schemas.android.com/apk/res/android">
   <com.my.personal.project.MyDialogPreference
    android:key="dialog"
    android:title="pref_delete_dialog"
    android:dialogMessage="dialog_text"
    android:negativeButtonText="no"
    android:positiveButtonText="popup_button_yes"/>


</PreferenceScreen>

And finally the content of MyDialogPreference.java

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import java.io.File;

public class MyDialogPreference extends DialogPreference {

public MyDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub

}

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    persistBoolean(positiveResult);//Esto devuelve si en el dialogo pulsaron aceptar o cancelar (Booleano)

    if (positiveResult) {

    }
}
}

The problem I have is that I want the dialogue to go directly

    
asked by Pelayo Rodriguez 01.02.2016 в 22:03
source

1 answer

0

Remember that your dialogue should extend from DialogFragment or DialogPreference

public class miFragmentDialog extends DialogFragment {

I notice you have:

public class FragmentEliminarDialogo extends PreferenceFragment {

since you are calling it FragmentEliminarDialogo when calling the method eliminar() :

public void eliminar(){
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(android.R.id.content, new FragmentEliminarDialogo());
    ft.commit();
}

Change your eliminar() function to open an instance of MyDialogPreference() :

   public void eliminar(){
        FragmentManager fm = getFragmentManager();
        MyDialogPreference myDialog = new MyDialogPreference();
        myDialog.show(fm, null);
 }

I found a way to do it directly from the code

 AlertDialog alert = new AlertDialog.Builder(this)
            .setTitle("Borrar")
            .setMessage("¿Seguro que quieres borrar este elemento?")
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            })
            .show();
    
answered by 01.02.2016 / 22:08
source