0

How can I give all elements fixed width inside LinearLayout?Imagine I have a list of buttons and I would to center them all and also give them same width based on the element with the biggest width, is there any way how to make the layout manager to do this dynamically (I mean don't repeat the code for every button)?My code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center">
    <Button android:id="@+id/mainStageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="menuClickListener"
        android:text="@string/mainStage"/>          
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="menuClickListener"
        android:text="@string/amphiteatre"/>    
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="menuClickListener"
        android:text="@string/aula" />
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="menuClickListener"
        android:text="@string/mensa"/>  
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="menuClickListener"
        android:text="@string/sportsSchedule"/> 

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • Sounds good but I was rather asking if there is a way how to set this inside a LayoutManager tag (you know, DRY principle :)) – Petr Mensik Aug 13 '14 at 20:21
  • I'll be removing my comments since they wouldn't help. I'll be adding another soon. – Vikram Aug 13 '14 at 20:24
  • Thanks, I have also edited my question, maybe I didn't make myself clear enough :) – Petr Mensik Aug 13 '14 at 20:27
  • 1
    I believe your current setup cannot be modified to produce the desired result. Here's what I suggest: Your parent container will be a `FrameLayout` - {width:match, height:match}. Inside the `FrameLayout`, place a `LinearLayout` - {width:wrap, height:**, orientation:vertical, **layout_gravity:center_horizontal**}. Place your `Buttons` in this `LinearLayout`, but change their `width` attribute to `match_parent` and remove the `layout_gravity` attributes. – Vikram Aug 13 '14 at 20:59
  • Nice, that really worked! Put your comment into the answer so I can upvote you properly :) – Petr Mensik Aug 16 '14 at 17:04

3 Answers3

2

Have you consider using a listView? You could achieve the results that you are looking for with a listView with a much cleaner code-base:

List View

nerdy900
  • 338
  • 2
  • 8
1

You could use a for-loop and loop trough all the Layout's children via view. getChildAt(index) :)

TehCoder
  • 209
  • 1
  • 8
0

The most reliable method:

  1. Write a custom layout manager

  2. Or even easier, pick an existing layout manager that does what you need

In this case TableLayout is a subclass of LinearLayout that already has the right properties.

From the link:

The width of a column is defined by the row with the widest cell in that column

By just replacing the linear layout with this:

<TableLayout 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" >

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is rather long" >
        </Button>
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium" >
        </Button>
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Short" >
        </Button>
    </TableRow>
</TableLayout>

The results are like this image:

Table layout

x-code
  • 2,940
  • 1
  • 18
  • 19