1

Can someone please help me on how I can add OK information dialog box in my fragment class? I have tried this but it is not working, it works in activity class but not in fragments. I must be missing something or doing is completely wrong, can someone help me out?

Edited: A method, something along the lines of;

public void rideInfor(ImageButton button, findViewById buttonID, String title, String message){


     ImageButton button = (ImageButton)rootView.findViewById(R.id.buttonID);
     button.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
                    builder1.setTitle(title);
                    builder1.setMessage(message);
                    // builder1.setIcon(R.drawable.ic_launcher);
                    builder1.setNeutralButton("Yes", new DialogInterface.OnClickListener()
                    {

                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            dialog.cancel();
                        }
                    });

                    builder1.show();

                }
            }); 
Android
  • 171
  • 1
  • 4
  • 16
  • which error you are getting ? – rupesh Nov 30 '14 at 14:48
  • I am not getting any error in logcat or console but the all of this code isn't working in fragment for example, it tells me to keep on adding } and keep on deleting them and also I had to remove this line new AlertDialog.Builder(this) because it was giving this error "The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined" – Android Nov 30 '14 at 14:51

1 Answers1

0

Try below code and let me know:

EDITED CODE:

           });  
                    alertDailog.show();
    }
        });

Method:

         private void showAlertDialog(String msg, String title){
           AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    getActivity(), AlertDialog.THEME_HOLO_LIGHT);
            alertDialogBuilder.setTitle(title);
            alertDialogBuilder
                    .setMessage(msg);

    // Set the icon at the top left you want
    alertDialogBuilder.setIcon(R.drawable.icon);

     // true if cancellable want to true
            alertDialogBuilder.setCancelable(false);
            AlertDialog alertDailog = alertDialogBuilder.create();
    alertDialogBuilder.setNeutralButton("Yes", new DialogInterface.OnClickListener()
    {
                @Override
                public void onClick(View v) {
            // Ok button
            });


                alertDailog.show();
     }

Full Code:

ImageButton camBt = (ImageButton)view.findViewById(R.id.button1);
camBt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getActivity(), AlertDialog.THEME_HOLO_LIGHT);
                alertDialogBuilder.setTitle("Title");
                alertDialogBuilder
                        .setMessage("Your Message");

        // Set the icon at the top left you want
        alertDialogBuilder.setIcon(R.drawable.icon);

         // true if cancellable want to true
                alertDialogBuilder.setCancelable(false);
                AlertDialog alertDailog = alertDialogBuilder.create();
        alertDialogBuilder.setNeutralButton("Yes", new DialogInterface.OnClickListener()
        {
                    @Override
                    public void onClick(View v) {
                // Ok button
                });


                    alertDailog.show();
            } 
        });// Close onCLickListener of Image Button
rupesh
  • 2,865
  • 4
  • 24
  • 50
  • Hi it works but the dialog box pops up when I launch the app, I want it to open when I click on my button. Also would be possible to add a ok button so the user can can close the dialog box down. Thanks again. – Android Nov 30 '14 at 15:08
  • @Android put inside your onClick event of the button. – rupesh Nov 30 '14 at 15:12
  • @Android put inside this: `camBt.setOnClickListener(new OnClickListener() { public void onClick(View arg0) {` – rupesh Nov 30 '14 at 15:13
  • rup35h, I have edited my post, please have a look. I am getting an error it is asking me to add }): and these stuff but when I add them it tells me to delete them. – Android Nov 30 '14 at 15:34
  • it works, thanks. Can I asked 1 more question if you don't mind – Android Nov 30 '14 at 15:40
  • Sorry, I won't be able to upvote because of low rep. it says i need 15 at least – Android Nov 30 '14 at 15:47
  • rup, can we take this to discussion please? – Android Nov 30 '14 at 15:48
  • Its ok. Ask me what you were asking – rupesh Nov 30 '14 at 15:48
  • But you don't have enough credit to join chat i think – rupesh Nov 30 '14 at 15:49
  • how much reputation do I need? – Android Nov 30 '14 at 15:57
  • oh k, my question is, is it possible to add pinch zoom on a image that I have in my fragment class? and also you see the dialog box method that we created eariler, how can I put that in a method with parameters such as the findViewById as one of the parameters – Android Nov 30 '14 at 16:00
  • Yes have a look on these links: [link 1](http://stackoverflow.com/questions/10630373/android-image-view-pinch-zooming) [link2](http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/) – rupesh Nov 30 '14 at 16:03
  • rup, can you help me with putting the dialog method that we created in a another method with findViewById as the parameter please. – Android Nov 30 '14 at 16:29