Ad

Custom Alertdialog Width Is Bigger Than The Shown Width In Layout Xml

I am trying to create custom alertdialog. Here is my alertdialog custom layout xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:orientation="horizontal">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Processing" />
</LinearLayout>
</RelativeLayout>

and the code from activity where i call custom alertdialog

   LayoutInflater inflater = MainActivity.this.getLayoutInflater();
   View dialogView = inflater.inflate(R.layout.layoutalertprogress, null);
   AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
   alertDialogBuilder.setView(dialogView);
   alertDialog = alertDialogBuilder.create();
    if(!alertDialog.isShowing())
      {
           alertDialog.show();
      }

problem is even in layout file linearlayout width is wrapped to its content like this layout demo

the real width of alertdialog looks like this in runtime enter image description here so the real width of alertdialog is wider (wider than the layout shown above). It seems that linearlayout width is not wrapped to its contents so there is big unused space from its edges to its child elements. how to make this linearlayout width to wrap its contents so the unused space from its edges can be removed? Thanks

Ad

Answer

Try this

set background color to child LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Processing" />
    </LinearLayout>
</RelativeLayout>

and then add this line in your java code

 alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

after this

AlertDialog alertDialog = alertDialogBuilder.create();
Ad
source: stackoverflow.com
Ad