0

I am trying to make a custom AlertDialog. The issue is the extra space in the dialog (white rectangle at the pic). How to remove it?

dialog here is my code :

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:background="@drawable/round_alert_like_ui">

         <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/like_icon"
            android:layout_marginStart="10dp"
            android:layout_gravity="center"
            android:id="@+id/likeIconId"
            android:fitsSystemWindows="true"
            android:padding="5dp" />

         <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/wowButtonId"
                android:layout_marginStart="5dp"
                android:src="@drawable/love_icon"
                android:background="@drawable/round_button_for_round_menu_like_button"
                android:layout_gravity="center" />

         <ImageButton
                android:layout_width="40dp"
                android:layout_height="35dp"
                android:id="@+id/blehButtonId"
                android:layout_marginStart="5dp"
                android:src="@drawable/bleh"
                android:background="@drawable/round_button_for_round_menu_like_button"
                android:layout_gravity="center" />

         <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/dislikeButtonId"
            android:layout_marginStart="5dp"
            android:src="@drawable/dislike_icon"
            android:background="@drawable/round_button_for_round_menu_like_button"
            android:layout_gravity="center" />
    </LinearLayout>
</LinearLayout>

in activity:

final AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AlertDialog alertDialog = builder.create();
View view1 = LayoutInflater.from(context).inflate(R.layout.layout_for_long_like_button_option, null);
Alexey
  • 4,384
  • 1
  • 26
  • 34
im07
  • 386
  • 2
  • 12

4 Answers4

0

Just update the layout_width of your parent LinearLayout and the immediate child LinearLayout to match_parent instead of wrap_content. See the update code below:

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp"
            android:background="@drawable/round_alert_like_ui">

             <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/like_icon"
                android:layout_marginStart="10dp"
                android:layout_gravity="center"
                android:id="@+id/likeIconId"
                android:fitsSystemWindows="true"
                android:padding="5dp"/>

             <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/wowButtonId"
                    android:layout_marginStart="5dp"
                    android:src="@drawable/love_icon"
                    android:background="@drawable/round_button_for_round_menu_like_button"
                    android:layout_gravity="center"/>

             <ImageButton
                    android:layout_width="40dp"
                    android:layout_height="35dp"
                    android:id="@+id/blehButtonId"
                    android:layout_marginStart="5dp"
                    android:src="@drawable/bleh"
                    android:background="@drawable/round_button_for_round_menu_like_button"
                    android:layout_gravity="center"/>

             <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/dislikeButtonId"
                android:layout_marginStart="5dp"
                android:src="@drawable/dislike_icon"
                android:background="@drawable/round_button_for_round_menu_like_button"
                android:layout_gravity="center"/>
        </LinearLayout>
    </LinearLayout>

Update:

Try setting transparent background to your alert dialog after you called the alertDialog.show().

alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
sourav.bh
  • 467
  • 1
  • 7
  • 20
  • i want to remove the white space( there will just be icon like facebook like button popup), rather than extending the layout to the alertdialog size. – im07 Mar 27 '19 at 15:46
  • @Imranrana07 got your point, I have updated my answer above, try it please – sourav.bh Mar 27 '19 at 16:26
  • its just making background transparent. i am trying to make like facebook like button. if you can please help me – im07 Mar 27 '19 at 16:33
  • but your question was about removing that extra white background, please be specific about what you want and that's the only way we can help you – sourav.bh Mar 27 '19 at 16:36
  • sorry, its a simple mistake, but i told you i want like facebook like button which popup just above the button. – im07 Mar 27 '19 at 16:53
  • @Imranrana07, I appreciate that you realise your problem. But you should understand that SO is not something where you can just ask I want this, after that someone will do that for you. You need to try for it yourself first, then ask help on specific problem. Like you did in the question, no one can give your desired solution based on the code you posted. There is no indication which button you are mentioning or from where you are using this layout. I would suggest you to post a new question with your attempts or edit this question enough so that someone can help you. – sourav.bh Mar 27 '19 at 18:15
  • sourav, bro u r from bangladesh, i also from bd.if you dont mind may i be added with you on fb? – im07 Mar 28 '19 at 04:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190806/discussion-between-sourav-bh-and-imranrana07). – sourav.bh Mar 28 '19 at 05:06
0

As an option, if you would create custom dialog, you can do like this:

  @Override
  public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null) {
      int width = ViewGroup.LayoutParams.WRAP_CONTENT;
      int height = ViewGroup.LayoutParams.WRAP_CONTENT;
      dialog.getWindow().setLayout(width, height);
    }
  }
Jurij Pitulja
  • 5,546
  • 4
  • 19
  • 25
0

Try this layout and customize. There is no need to nested linear layout. It causes performance problems.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:layout_height="48dp"
              android:background="@color/colorPrimary"
              android:padding="4dp">

    <ImageButton
            android:id="@+id/likeIconId"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:background="@android:color/black"
            android:src="@android:drawable/ic_delete"/>

    <Space
            android:layout_width="8dp"
            android:layout_height="0dp"/>

    <ImageButton
            android:id="@+id/wowButtonId"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:background="@android:color/black"
            android:src="@android:drawable/ic_delete"/>

    <Space
            android:layout_width="8dp"
            android:layout_height="0dp"/>

    <ImageButton
            android:id="@+id/blehButtonId"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@android:color/black"
            android:adjustViewBounds="true"
            android:src="@android:drawable/ic_delete"/>

    <Space
            android:layout_width="8dp"
            android:layout_height="0dp"/>

    <ImageButton
            android:id="@+id/dislikeButtonId"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@android:color/black"
            android:adjustViewBounds="true"
            android:src="@android:drawable/ic_delete"/>
</LinearLayout>

And in activity or fragment you can call like this. There is no need to use alert dialog.

private fun showDialog(context: Context){
        val dialog = Dialog(context)
        dialog.setContentView(R.layout.layout_for_long_like_button_option)
        dialog.show()
    }
toffor
  • 1,219
  • 1
  • 12
  • 21
0

thank you guys for your kind cooperation. i have solved my problem by myself. i just added a layout to the position i want and solved all. thank you again

im07
  • 386
  • 2
  • 12