Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView with Multiple Columns aligned properly

Tags:

android

My Tablet app shows a ListView with multiple TextViews in each line item (using custom cursor adapter). I have around 6-7 columns, What is the best way to give equal spaces to all columns?

Right now I tried:

  • Same weight on every TextView. But this is never properly aligned.
  • Used max width along with 1st, still no luck, one can easily overflow and push the next one to right.

    <TextView
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:text="column 1"
        android:textSize="22sp" />
    
like image 731
Taranfx Avatar asked Aug 03 '12 16:08

Taranfx


2 Answers

Weights are fine, but you have to set android:layout_width to 0dp every time you use a layout weight, else your views may not be scaled properly.

Also, don't mix fixed widths with layout weights. If you choose to use weights, use them everywhere in your row layout.

like image 94
Dalmas Avatar answered Oct 05 '22 23:10

Dalmas


It's not good practice to use fixed widths and heights on Android, let Android do the layout. You can use a linearLayout with weights in order to properly aline items in different rows. use "android:width=0dp" and use weight to dimension the various fields. An alternative approach would be to use a TableLayout, but that's kind of tricky. It does work, however.

like image 25
Christine Avatar answered Oct 06 '22 01:10

Christine