coder
coder

Reputation: 13250

Display dialog in fragment android

I am using http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ tutorial for displaying silder menu and working pretty well.

Now I would like to know for one particular listview item I need to display dialog with yes or no buttons. So I'm new to this. Can anyone help me out with this?

This is what my slider menu looks like and as you can see If I click the third item I need to display dialog as shown

enter image description here

Upvotes: 4

Views: 1121

Answers (1)

Jossy Paul
Jossy Paul

Reputation: 1287

If you are following this tutorial, then change your displayView(int position) method in MainActivity to this:

    private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new FindPeopleFragment();
        break;
    case 2:
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                MainActivity.this);

            // set title
            alertDialogBuilder.setTitle("Alert");

            // set dialog message
            alertDialogBuilder
                .setMessage("Pelase select your choice")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                        //do whatever you want to do when user clicks ok

                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
        break;
    case 3:
        fragment = new CommunityFragment();
        break;
    case 4:
        fragment = new PagesFragment();
        break;
    case 5:
        fragment = new WhatsHotFragment();
        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

Upvotes: 2

Related Questions