0

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?

thhVictor
  • 338
  • 6
  • 25

1 Answers1

1

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.

Community
  • 1
  • 1
Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
  • thanks for the solution. they are great. I managed to make it work with your help and i have just a question. If i needed some flexibility on the listview to wrap its width (not match_parent), do i still need a custom adapter? I only want the user to be able to click on the text but not the empty spaces left and right of the text. – thhVictor Oct 10 '13 at 03:14
  • I have not tried but I think that in that case you should go for custom adapter and in getView you should add onclicklistner for your textview. Custom adapter are flexible and you can change them according to your requirement any time. Today, any how if you will implement your listview according to your requirement and later you need to change or add some views then again you will implement everything again. So if you feels that there can be changes further then you should use custom adapters. – Bharat Sharma Oct 10 '13 at 12:07