Search code examples
javaandroidandroid-fragmentsandroid-alertdialog

AlertDialog "onClick" method not working properly


I currently have an alert dialog that is being displayed on a fragment. The dialog displays to the user with no issues, but the "setPositiveButton" and "setNegativeButton" are not responding to the user interaction correctly, the Log.i after the on "setPositiveButton" is pressed is the only thing that is actually working. I want it to display a Toast message and travel to a new fragment after the possitive button is has been pressed.

    private FragmentRequestCodeBinding binding;

//TAG
private static final String TAG = "requestCodeFragment";

//Values
private String firstLetter = "B";
private int secondLetter = 0;
private String thirdLetter = "0";
private String fourthLetter = "0";
private String fifthLetter = "0";
private String dtcCode = firstLetter + secondLetter + thirdLetter + fourthLetter + fifthLetter;

//Buttons
private Button request;

//Views
View root;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    binding = FragmentRequestCodeBinding.inflate(inflater, container, false);
    root = binding.getRoot();

    request = root.findViewById(R.id.requestButton);

    // Inflate the layout for this fragment
    return root;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final NavController navController = Navigation.findNavController(view);


    request.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new AlertDialog.Builder(getContext())
                    .setIcon(R.drawable.ic_baseline_priority_high_24)
                    .setTitle("Request for " + dtcCode)
                    .setMessage("Are you sure you want to request the following code " +
                            "for this car model?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(getContext(), "Ticket created successfully!",
                                    Toast.LENGTH_SHORT).show();
                            navController.navigate(R.id.action_requestCodeFragment_to_navigation_profile);
                            
                            Log.i(TAG, "onClick: Pressed");
                        }
                    })
                    .setNegativeButton("No", null)
                    .setCancelable(true)
                    .show();
        }
    });

}

Solution

  • Sorry to bother guys, the code is all good. I started using a real device instead of the android emulator and everything is working just fine.