Siva Natarajan
Siva Natarajan

Reputation: 1429

Aligning listview items using code android

I have a listview.

The list items must be populated using code and in the following pattern

The odd numbered list items must be placed aligned with the left side of the screen.

The even numbered list items must be place aligned with the right side of the screen.

The width must depend on the size of the content.

These task must be performed using code.

  <LinearLayout android:id="@+id/list_item"
        android:paddingLeft="5dip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >
        <TextView android:id="@+id/text_view"
            android:autoLink="all"
            android:paddingTop="6dip"
            android:paddingBottom="3dip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:linksClickable="false"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#ff000000"
            android:textSize="18sp" />
</LinearLayout>

note:i use cursor adapter to bind the data with listview

Upvotes: 0

Views: 646

Answers (1)

woodshy
woodshy

Reputation: 4111

You must see Google I/O 2010 - The world of ListView to understand how ListView works.

For your situation in getView(...) method of your list adapter you could add something like this:

    if(position%2==0){
        //even
        convertView.setGravity(Gravity.RIGHT);
    } else{
        //odd
        convertView.setGravity(Gravity.LEFT);
    }

In order to set width dependency on content change all "layout_width properties" to "wrap_content" in your xml above.

Also it would be useful to read ListView Tips & Tricks post series.

Hope I've got the question correctly.

Upvotes: 1

Related Questions