thhVictor
thhVictor

Reputation: 338

Listview dynamically allocate width

I want to allow my ListView to be wrapped to its content on its width and making it aligning to the parent centre.

This is what a normal ListView is giving me

This is almost what i want to achieve, only that i want the ListView to wrap by the longest test in the ListView. Currently this is achieved by declaring the dp which is something I would like to avoid if possible

Some codes for first image:

<ListView
            android:id="@+id/lst_test"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="wrap_content" >
        </ListView>

Some codes for second image:

<ListView
     android:id="@+id/lst_test"
     android:layout_width="200dp"
     android:layout_height="wrap_content" >
</ListView>

Do I have to use custom adapter for this or I can stick with the current one? If I would need to use custom adapter, how do i go about doing this?

Upvotes: 0

Views: 694

Answers (1)

Bharat Sharma
Bharat Sharma

Reputation: 3966

You don't need to create custom adapter. Here is an example which can help you.

public class MainActivity extends Activity {

    HashMap <String, CheckBox> options = new HashMap<String, CheckBox>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView list_view = (ListView) findViewById(R.id.listView1);

        ArrayList<String> str = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            str.add("Example " + i);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_layout, R.id.textView1, str);
        list_view.setAdapter(adapter);
    }
}

Above I have created an activity in which i am getting listview and setting data using ArrayAdapter.

I have created two layouts :

First layout: Contains listview. Second layout: Contains textview which need to display.

Main logic to align data at center of Listview is you need to set gravity of textview as center.

You can manage second layout according to your needs.

Here is my first XML layout:

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

Here is my second layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="TextView" />

</RelativeLayout>

If you are still facing any problem then you can ask. If you need more flexibility then you can create custom adapter by inheriting ArrayAdapter class.

Here is an example of custom adapter: How do I link a checkbox for every contact in populated listview?

Hope it will help you.

Upvotes: 1

Related Questions