android – Custom AlertDialog.Builder positive and negative buttons not showing-ThrowExceptions

Exception or error:

Hello I followed every tutorial in Stack Overflow and YouTube anyways once I got to display my RecyclerView then I needed to accomplish other things with no problem at all. Anyways my only problem is that the positive button and the negative buttons disappeared. I assume that the problem might be that the things I have done in my XML is overlapping with the buttons in the view but I only have 3 items in the RecyclerView, so this is my big doubt. I made the dialog in an onLongClick then I added a RecyclerView method to get the position and the data present in the row that’s why I cant call the alertDialog.setPositiveButton() in another place. This is quite a challenge would be appreciated to get some help in this.

This is my XML

<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <TextView
        android:id="@+id/text_adm_asigna_asesor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:fontFamily="@font/poppins_bold"
        android:text="Asigna un asesor"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="30dp"
        android:visibility="visible"
        android:layout_marginRight="30dp" />

        <LinearLayout
            android:id="@+id/linear_dialog_testDrive"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:visibility="gone"
           >

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/circle_imagen_seleccion_alert"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:visibility="gone"
                android:src="@drawable/lifeon_logo"
                android:layout_marginLeft="40dp">

            </de.hdodenhof.circleimageview.CircleImageView>

            <TextView
                android:id="@+id/alert_cambia_test_drive_nombre"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/poppins_bold"
                android:textColor="@android:color/black"
                android:text="@string/nombre"
                android:visibility="gone"
                android:layout_marginLeft="30dp">

            </TextView>

        </LinearLayout>

        <Button
            android:id="@+id/cambiar_seleccion_alert_test_drive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:background="@drawable/button_shape"
            android:text="@string/cambiar_seleccion"
            android:layout_gravity="center_horizontal"
            android:visibility="gone"
            >

        </Button>

    </LinearLayout>
    </LinearLayout>

</androidx.core.widget.NestedScrollView>

This is the java code

      recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {

        }

        @Override
        public void onLongClick(View view, int position) {
            asesores.clear();

            ParseObject itemTestDrive = listadoTestDrive.get(position);

            testDriveObjectId = itemTestDrive.getObjectId();

            LayoutInflater inflater = getLayoutInflater();
            View convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_agreements, null);
            alertDialog.setView(convertView);


            RecyclerView list = convertView.findViewById(R.id.list);
            TextView txt = convertView.findViewById(R.id.text_adm_asigna_asesor);
            CircleImageView ima=convertView.findViewById(R.id.circle_imagen_seleccion_alert);
            TextView txtNombreSeleccion=convertView.findViewById(R.id.alert_cambia_test_drive_nombre);
            Button botonCambiarSeleccion=convertView.findViewById(R.id.cambiar_seleccion_alert_test_drive);
            LinearLayout linearLayout=convertView.findViewById(R.id.linear_dialog_testDrive);


            botonCambiarSeleccion.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.setVisibility(View.VISIBLE);
                    ima.setVisibility(View.GONE);
                    txtNombreSeleccion.setVisibility(View.GONE);
                    botonCambiarSeleccion.setVisibility(View.GONE);
                    linearLayout.setVisibility(View.GONE);
                }
            });

            ParseQuery<ParseObject> query1 = ParseQuery.getQuery("CitaTestDrive");
            query1.include("ComercialAsignado");
            query1.getInBackground(testDriveObjectId, new GetCallback<ParseObject>() {
                @Override
                public void done(ParseObject object, ParseException e) {
                    if (object.getParseObject("ComercialAsignado") != null) {
                        txt.setText(getActivity().getResources().getString(R.string.Advertencia_asignacion_a_alguien));

                    }


                }
            });



            ParseUser user = ParseUser.getCurrentUser();

            ParseObject sucursal = user.getParseObject("PerteneceSucursal");
            String sucurObjectId = sucursal.getObjectId();

            ParseQuery<ParseUser> query = ParseUser.getQuery();
            ParseObject suc = ParseObject.createWithoutData("Sucursal", sucurObjectId);
            query.whereEqualTo("PerteneceSucursal", suc);
            query.include("PerteneceSucursal");
            query.whereEqualTo("Rol","Asesor");
            query.findInBackground(new FindCallback<ParseUser>() {
                @Override
                public void done(List<ParseUser> objects, ParseException e) {
                    if (e == null) {
                        for (ParseUser obj : objects) {
                            asesores.add(obj);
                        }
                        list.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                        list.setHasFixedSize(true);
                        ListAsesorAdminAsignaTestDrive adapter = new ListAsesorAdminAsignaTestDrive(getApplicationContext(), asesores);
                        list.setAdapter(adapter);
                    }
                }
            });
            list.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), list, new RecyclerTouchListener.ClickListener() {
                @Override
                public void onClick(View view, int position) {
                    ParseUser item = asesores.get(position);

                    list.setVisibility(view.GONE);
                    ima.setVisibility(view.VISIBLE);
                    txtNombreSeleccion.setVisibility(view.VISIBLE);
                    botonCambiarSeleccion.setVisibility(View.VISIBLE);
                    linearLayout.setVisibility(View.VISIBLE);


                    ParseFile parseFile = item.getParseFile("Avatar");
                    if (parseFile != null) {
                        parseFile.getDataInBackground(new GetDataCallback() {
                            @Override
                            public void done(byte[] data, ParseException e) {
                                if (e == null) {
                                    Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                                    if (bmp != null) {
                                        ima.setImageBitmap(bmp);
                                    }
                                }
                            }
                        });
                    }

                    String asesorObjectId = item.getObjectId();
                    String asesorNombre = item.getString("Nombre");
                    txtNombreSeleccion.setText(asesorNombre);


                    alertDialog.setPositiveButton(getApplicationContext().getResources().getString(R.string.guardar), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ParseQuery<ParseObject> query = ParseQuery.getQuery("CitaTestDrive");

                            query.getInBackground(testDriveObjectId, new GetCallback<ParseObject>() {
                                @Override
                                public void done(ParseObject object, ParseException e) {

                                    object.put("ComercialAsignado", ParseObject.createWithoutData("_User", asesorObjectId));
                                    object.put("PendienteAsignacionAdmin", "Asignado");
                                    object.saveInBackground(new SaveCallback() {
                                        @Override
                                        public void done(ParseException e) {
                                            if (e == null) {
                                                Toast.makeText(getApplicationContext(), "Test drive asignado a:" + asesorNombre, Toast.LENGTH_LONG).show();
                                            } else {
                                                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();

                                            }
                                        }
                                    });

                                }

                            });

                            adapter.notifyDataSetChanged();

                        }
                    });

                    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });


                }

                @Override
                public void onLongClick(View view, int position) {

                }
            }));


            list.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            list.setHasFixedSize(true);

            ListAsesorAdminAsignaTestDrive adapter = new ListAsesorAdminAsignaTestDrive(getApplicationContext(), asesores);
            list.setAdapter(adapter);


            alertDialog.show();


        }
    }));

Appreciate if you don’t answer if you don’t know the correct answer or way to solve this.

Update: Android is gone crazy!!!.When I make the first onLongClick the alert dialog still doesn’t work since I don’t have a way to go back I click in the outside of the alert dialog and it disappears. Then I make another long Click and it works!!! this is very weird why does this happen????? nothing have changed.

How to solve:

I. Declare custom drawable.xml for dialog background.

android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
    <corners android:radius="2dp" />
    <solid android:color="@color/indigo" />
</shape>

II. Declare custom styles in your styles.xml file.

<!--buttons color-->
<item name="colorAccent">@color/pink</item>
<!--title and message color-->
<item name="android:textColorPrimary">@android:color/white</item>
<!--dialog background-->
<item name="android:windowBackground">@drawable/drawable</item>

III. Create your dialog and use style as parameter in AlertDialog.Builder.

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyDialogTheme);

AlertDialog dialog = builder.create();

dialog.show();

###

use code like this,

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setMessage(message);
    builder.setCancelable(false);
    builder.setPositiveButton(
            "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();

                }
            });

    builder.setNegativeButton(
            "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();

              }
            });

    AlertDialog alert = builder.create();
    alert.setOnShowListener(arg0 -> {
        alert.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.RED));
        alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.RED));
    });
    alert.show();

Leave a Reply

Your email address will not be published. Required fields are marked *