83

I would like to create a dialog to display a video title and tags. Below text I would like to add buttons View, Edit and Delete and make these elements same size. Does anyone know how to modify .xml layout file in order to make elements inside LinearView same size?

The current layout file looks like this:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="vertical">

    <LinearLayout 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:orientation="vertical">

          <TextView 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:id="@+id/txtTitle" android:text="[Title]" >
          </TextView>

          <TextView 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" 
              android:id="@+id/txtTags"            
              android:text="[Tags]" >
          </TextView>

    </LinearLayout>

    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal">

        <Button 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:id="@+id/btnPlay" 
           android:text="View">
        </Button>

        <Button 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/btnEdit" 
            android:text="Edit">
        </Button>

        <Button 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/btnDelete" 
            android:text="Delete">
        </Button>

    </LinearLayout>

</LinearLayout>

I would appreciate if anyone could show the solution by modifying the pasted file content.

Thanks!

turbo
  • 1,887
  • 2
  • 21
  • 35
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286

3 Answers3

183

Use android:layout_width="0px" and android:layout_weight="1" on the three Buttons. That says the buttons should take up no more than 0 pixels, but the three of them should split any extra space between them. That should give you the visual effect you wish.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 4
    If you _REALLY_ wanted to you could alternatively use a TableLayout with android:stretchColumns="0,1,2". – Jeremy Logan Jul 25 '09 at 00:50
  • Awesome...so this means, if we put width as 0px, weight will override the settings? Why is it so? – 0xC0DED00D Aug 15 '13 at 04:57
  • This does not work if your Buttons are in a `GridLayout`. In this case they really become `0px` wide. Maybe `GridLayout` doesn't accept `layout_weight`? – Zelphir Kaltstahl Nov 15 '15 at 09:35
  • @Zelphir: `GridLayout` definitely does not use `layout_weight`. That is only used by `LinearLayout`, and perhaps some subclasses of `LinearLayout`. – CommonsWare Nov 15 '15 at 12:14
35

Another way is to make android:layout_width="fill_parent" and android:layout_weight="1" this will also works fine!!!

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Eby
  • 2,769
  • 4
  • 23
  • 30
  • 19
    You were really excited about this answer. :-) Love the enthusiasm! – Oh Danny Boy Sep 21 '11 at 15:40
  • CommonsWare's solution didn't work for me, but this did it! :) I used "match_parent" since I heard you should prefer it over "fill_parent", but both work. – codepleb Aug 14 '14 at 13:12
  • The **LinearLayout** parent of those elements must have also **layout_width="match_parent"**. – Patapoom Aug 07 '19 at 13:37
22

Use LinearLayout with your desired weightSum and create elements with equal layout_weight. Here is an example ...

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="5">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_share_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_search_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_event_note_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_brush_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_menu_white_36dp"/>
</LinearLayout>

So, the weight sum of all elements is 5. Here is the screenshot ...

enter image description here

Note that, Google Material Design icons are used. Hope this is helpful.

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117