Home > Back-end >  how do i add a pop up buble on a fragment?
how do i add a pop up buble on a fragment?

Time:04-13

hello i have a issue i can't put a pop up on a fragment in android studio

here's the code i try to use , the thing i try is to add a pop up to my fragment that when i click on a button a pop up show and show some text in it tho i tried to add a working code in the fargemnt it just dont work

public class fragment2 extends Fragment {
   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       return inflater.inflate(R.layout.fragment2_layout,container,false);


   }
   private Activity ControllerCompat;
   android.widget.Button Bouton1;



   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.fragment2_layout);
       Bouton1 = (android.widget.Button) findViewById(R.id.button1);




       Bouton1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               AlertDialog.Builder alert = new AlertDialog.Builder(fragment2.this);
               alert.setTitle("voici les boutons");

               alert.setPositiveButton("yes   ", new DialogInterface.OnClickListener() {

                   public void onClick(DialogInterface dialog, int whichButton) {

                   }
               });

               alert.setNegativeButton("no", new DialogInterface.OnClickListener() {

                   public void onClick(DialogInterface dialog, int whichButton) {

                   }
               });

               alert.show();
           }
       });
   }
}

CodePudding user response:

Bouton1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
                alertDialogBuilder.setTitle("Title");
                alertDialogBuilder.setMessage("Message");
                alertDialogBuilder.setCancelable(true);
                alertDialogBuilder.setPositiveButton("yes", (dialog, id) ->
                        {

                        }
                );
                alertDialogBuilder.setNegativeButton("no", (dialog, id) -> dialog.cancel());
                AlertDialog myAlertDialog = alertDialogBuilder.create();
                myAlertDialog.show();
            }
        });*emphasized text*

CodePudding user response:

like this? if yes then i have an error on the buuton1 saying that he cant resolve symbol "bouton 1"

`public class fragment2 extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2_layout, container, false);

    buton1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
            alertDialogBuilder.setTitle("Title");
            alertDialogBuilder.setMessage("Message");
            alertDialogBuilder.setCancelable(true);
            alertDialogBuilder.setPositiveButton("yes", (dialog, id) ->
                    {

                    }
            );
            alertDialogBuilder.setNegativeButton("no", (dialog, id) -> dialog.cancel());
            AlertDialog myAlertDialog = alertDialogBuilder.create();
            myAlertDialog.show();
        }
    });


}

} `

  • Related