0

I have linearlayout with 4 imagebutton one after another. In linearlayout with horizontal orientation and provided equal width. Here buttons are aligning properly but the images present in those buttons are not getting aligned properly.

Here is the below code, i have tried and this is how it looks. How do i make the images align properly with correct shape?

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:background="#fff"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">
    <ImageButton
        android:id="@+id/add_fav_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:scaleType="fitXY"
        android:layout_gravity="center"
        android:src="@drawable/ic_whatsapp"
        />

    <ImageButton
        android:id="@+id/id_share_fb_btn"
        android:src="@drawable/ic_whatsapp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"/>
    <ImageButton
        android:id="@+id/id_share_all_btn"
        android:src="@drawable/ic_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"/>
    <ImageButton
        android:id="@+id/id_whatsapp_btn"
        android:src="@drawable/ic_whatsapp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"/>
</LinearLayout>

enter image description here

Updated image, after changing android:layout_height="50dp" to android:layout_height="wrap_content"

enter image description here

Naruto
  • 9,476
  • 37
  • 118
  • 201

2 Answers2

0

Your images looks like squared. you are making them stretch way to fill the entire width but you are making them all with height of 50dp.

Change the height from 50dp to

android:layout_height="wrap_content"

and they will keep a correct height based on the width. If you want them all to be with height of 50dp, you have two choices:

  1. Set the width to 50dp and center them in the parent
  2. Create the "square button"

I don't know if I got the point, if not comment below

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
0

Put all each image button in a relative layout and layout_weight of relativelayout to 1 and layout_width= match_parent

I think this may work ...... or try using imageview instead of imagebutton

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="#fff"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<RelativeLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<ImageButton
    android:id="@+id/add_fav_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="0dp"
    android:minHeight="0dp"
    android:scaleType="fitXY"
    android:layout_gravity="center"
    android:src="@drawable/ic_whatsapp"
    />
  </RelativeLayout>
  <RelativeLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<ImageButton
    android:id="@+id/id_share_fb_btn"
    android:src="@drawable/ic_whatsapp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="0dp"
    android:minHeight="0dp"
    android:layout_gravity="center"
    android:scaleType="fitXY"/>
</RelativeLayout>
<RelativeLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<ImageButton
    android:id="@+id/id_share_all_btn"
    android:src="@drawable/ic_share"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="0dp"
    android:minHeight="0dp"
    android:layout_gravity="center"
    android:scaleType="fitXY"/>
</RelativeLayout>
<RelativeLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<ImageButton
    android:id="@+id/id_whatsapp_btn"
    android:src="@drawable/ic_whatsapp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="0dp"
    android:minHeight="0dp"
    android:layout_gravity="center"
    android:scaleType="fitXY"/>