Search code examples
javaandroidandroid-alertdialog

Custom dialog not showing properly in other devices


So I am developing one android application in which I have created one custom dialog box. but the problem arises that, this dialog box is showing correct in my phone and on emulator also. But it can't get display properly on redmi or other phones. Here is screenshot on my mobile device My device and [![On other device][2]][2]

On other device [2]: https://i.sstatic.net/b3H4J.jpg

Here is my code:- custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF538"
    android:padding="10dp">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Would you like to continue"
        android:textColor="@color/black"
        android:textSize="30dp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/bld"
        android:layout_width="269dp"
        android:layout_height="wrap_content"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/txt_input"
        android:layout_width="382dp"
        android:layout_height="69dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/back"
        android:hint="Why are you sending this request?"
        android:maxLength="100"
        android:textSize="20sp" />
    <!--    In given textview we have used maxlength =100
        because  we have to display small information so we use limited characters.-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="117dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="11dp"
         android:text="Your request will display publicly in blood request section.And if your any request is present in Blood Request will be deleted"
        android:textColor="#FF0303"
         android:textSize="20dp" />

    <Button
        android:id="@+id/btn_okay"
        android:layout_width="134dp"
        android:layout_height="60dp"
        android:layout_weight="0"
        android:layout_marginTop="10dp"
        android:backgroundTint="@color/black"
        android:text="yes"
        android:textColor="#ffffff"
        android:textSize="18sp" />


    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="136dp"
        android:layout_height="60dp"
        android:layout_gravity="right"
        android:layout_marginTop="-58dp"
        android:layout_weight="0"
        android:backgroundTint="@color/black"
        android:gravity="center"
        android:text="no"
        android:textColor="#ffffff"
        android:textSize="18sp" />
</LinearLayout>



donor.java

private void alertDialog() {
        final AlertDialog.Builder alert = new AlertDialog.Builder(Bloodbank.this);
        View mView = getLayoutInflater().inflate(R.layout.custom_dialog2, null);
        Button btn_cancel = (Button) mView.findViewById(R.id.btn_cancel);
        Button btn_okay = (Button) mView.findViewById(R.id.btn_okay);
        TextView bld = (TextView) mView.findViewById(R.id.bld);
        bld.setText("selected blood group is "+item);
        alert.setView(mView);

        final AlertDialog alertDialog = alert.create();
        alertDialog.setCanceledOnTouchOutside(false);
         btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Request dismissed", Toast.LENGTH_SHORT).show();
                alertDialog.dismiss();
            }
        });
        btn_okay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    SQLiteDatabase db = dbHandler.getWritableDatabase();
                    String sql1 = "delete from request where time < date('now','-2 day') AND Username = '" + MainActivity.getValue() + "'";
                    db.execSQL(sql1);
                    String sql = "select Name,contactNo from request where Username = '" + MainActivity.getValue() + "'";
                    Cursor cursor = db.rawQuery(sql, null);
                    if (cursor.moveToFirst()) {
                        do {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    runOnUiThread(new Runnable() {
                                        @RequiresApi(api = Build.VERSION_CODES.O)
                                        @Override
                                        public void run() {
                                            Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                                            vibrator.vibrate(VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE));
                                            Toast.makeText(getApplicationContext(), "your request is pending. Please delete that first.", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            });
                        }

                        while (cursor.moveToNext());
                    }
                    
                        catch (Exception e) {
                            runOnUiThread(new Runnable() {
                                @RequiresApi(api = Build.VERSION_CODES.O)
                                @Override
                                public void run() {
                                    Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                                    vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));

                                    Toast.makeText(getApplicationContext(), "Please select blood group first", Toast.LENGTH_SHORT).show();

                                }
                            });
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                alertDialog.dismiss();
            }

        });
        alertDialog.show();
        alertDialog.getWindow().setLayout(700, 833); //Controlling width and height.

    }

Solution

  • Try to change these lines

            alertDialog.show();
            alertDialog.getWindow().setLayout(700, 833); //Controlling width and height.
    

    With these line

            int width = (int) (getResources().getDisplayMetrics().widthPixels * 0.90);
            int height = (int) (getResources().getDisplayMetrics().heightPixels * 0.60);
            if (alertDialog.getWindow() != null) {
                alertDialog.getWindow().setLayout(width, height);
            }
            alertDialog.show();