-6

Now the width of each row is changing depending on the size of the text. But I would like a fixed size for each column like excel. What should be changed? It shouldn't be match_parent since the parent has very large width..

<?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"
              android:orientation="horizontal">

    <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:textAppearance="?attr/textAppearanceListItem" />

    <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:textAppearance="?attr/textAppearanceListItem" />

    <TextView
        android:id="@+id/short_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout>

EDIT

Current edition it shows something like

Chris chris@gmail.com R
Apostole apostole@gmail.com -

But what I want is

Chris    chris@gmail.com    R
Apostole apostole@gmail.com -

I tried @Priyank Prajapati 's version but it still gives the former layout

Student
  • 29
  • 5
  • for all the textviews change `android:layout_width=match_parent` and add `android:weight=1` – Logic Aug 24 '16 at 07:02
  • 1
    change `layout_width="0dp"` and add `layout_weight="1"`; – Masked Man Aug 24 '16 at 07:06
  • And if there are multiple rows use [TableLayout](https://developer.android.com/guide/topics/ui/layout/grid.html) with [weights](https://developer.android.com/guide/topics/ui/layout/linear.html#Weight) to make design more uniform and flexible – Rehan Aug 24 '16 at 07:07
  • Also refer to: http://stackoverflow.com/questions/9408174/how-to-give-fixed-width-and-height-for-button-in-android + http://stackoverflow.com/questions/2025282/what-is-the-difference-between-px-dp-dip-and-sp-on-android – Fr333du Aug 24 '16 at 07:11
  • @logic For weights to work, the weighted dimension **must** be **0dp**, not `match_parent`. – Phantômaxx Aug 24 '16 at 07:54

2 Answers2

1

Do something like this :

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

    <TextView
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:textAppearance="?attr/textAppearanceListItem" />

    <TextView
            android:id="@+id/email"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:textAppearance="?attr/textAppearanceListItem" />

    <TextView
        android:id="@+id/short_name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout>
  • 1
    please check my edited answer if you want to change gravity then use android:gravity="center" or android:gravity="right" try this if any query then let me know. – Priyank Prajapati Aug 24 '16 at 08:36
1

Change parent android:layout_width="wrap_content" to android:layout_width="match_parent"

Note: By using android:layout_weight="1" with with android:layout_width="0dp" for each will unifrom it for all rows

enter image description here

Full code:

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

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:layout_weight="2"
        android:text="Chris"
        android:textAppearance="?attr/textAppearanceListItem" />

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:layout_weight="8"
        android:text="chris@gmail.com"
        android:textAppearance="?attr/textAppearanceListItem" />

    <TextView
        android:id="@+id/short_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:layout_weight="1"
        android:text="R"
        android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout>
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41