-1

I am trying to load a custom view of my dialog in my activity. I want to remove the white space to the right of the dialog.

My activity

public class MyActivity extends AppCompatActivity {


  @Override
  protected void onCreate(final Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.my_activity);
     showClippingCoupon();
  }
}

My dialog code

void showClippingCoupon()
{
    LayoutInflater layoutInflater = getLayoutInflater();
    View toastView = layoutInflater.inflate(R.layout.clipping_coupon_toast, null);

    AlertDialog.Builder builder = new AlertDialog.Builder(this).setView(toastView);
    AlertDialog dialog = builder.create();
    dialog.setCanceledOnTouchOutside(true);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    TextView couponCodeText = (TextView) toastView.findViewById(R.id.coupon_code_text);

    couponCodeText.setText("Y34UIDEFDK");
    ImageView closeButtonImageView = (ImageView) toastView.findViewById(R.id.coupon_close);
    if (closeButtonImageView != null)
    {
      closeButtonImageView.setOnClickListener(new OnClickListener()
      {
        @Override
        public void onClick(View v)
        {
          dialog.dismiss();
        }
      });
    }

    dialog.show();
}

My activity layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout">
</FrameLayout>

I am trying to load a custom dialog view in my activity. This is how it is supposed to look like.

enter image description here

But this is what I get

enter image description here

This is what it looks in a preview.

enter image description here

Here is the layout of the dialog.

        <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/coupon_text_color_v2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="wrap_content"
        android:id="@+id/coupon_clip_success_icon"
        android:src="@drawable/ic_check_circle_green"
        app:layout_constraintStart_toStartOf="parent"
        android:contentDescription="@android:string/ok"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_margin="16dp"
        android:layout_toStartOf="@+id/coupon_body"
        android:layout_height="wrap_content" />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:id="@+id/coupon_body"
        android:focusable="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@+id/coupon_clip_success_icon"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:id="@+id/coupon_copied_text"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="48dp"
            android:text="@string/coupon_code_copied"
            android:textAppearance="@style/Small"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/coupon_code_text"
            android:layout_below="@+id/coupon_copied_text"
            android:layout_marginStart="16dp"
            android:layout_marginBottom="16dp"
            android:text="Test"
            android:textAppearance="@style/Small.Bold"
            />

    </RelativeLayout>
    <ImageView
        android:id="@+id/coupon_close"
        android:src="@drawable/ic_clear"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_constraintStart_toEndOf="@+id/coupon_body"
        android:layout_marginEnd="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_gravity="top|right"
        android:contentDescription="@string/close" />

</androidx.constraintlayout.widget.ConstraintLayout>

What should I be doing to remove the white space at the dialog's right?

Kartik
  • 2,541
  • 2
  • 37
  • 59
  • I would start by constraining everything with at least 3 constraints. Also not seeing why you need a relative layout inside your constraint layout. Also I would try to force wrap content for the dialog, like: `dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, (ViewGroup.LayoutParams.WRAP_CONTENT);` – cylon May 05 '19 at 07:28
  • Can you elaborate? – Kartik May 05 '19 at 07:30
  • First point is probably won't help, as I see that in preview it looks OK, and there is no warning from the editor related to the constraints, right? Secondly, getting rid of relative layout would be just an improvement, and has nothing to do with your problem. Have you tried to force the dialog to wrap content with the code above? – cylon May 05 '19 at 07:42
  • There was no change. – Kartik May 05 '19 at 07:47
  • Simply setting `wrap_content` to your frame layout might not work. Try these solutions to change dialog view's width to `WRAP_CONTENT` https://stackoverflow.com/a/17724940/6168272. – Ranjan May 05 '19 at 07:52

1 Answers1

0
ConstraintLayout :- width : match_parent and replace your Imageview 
 <ImageView
        android:id="@+id/coupon_close"
        android:src="@drawable/ic_clear"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginEnd="8dp"
        android:contentDescription="@string/close" />