0

I have a linear layout in xml with vertical orientation.

LinearLayout linearLayoutRoot = (LinearLayout)findViewById(R.id.listViewCompareLinear);

I used com.devsmart.android.ui.HorizontalListView library for horizontal ListView scroll. I need to call a no. of the horizontal scroll view programatically and add it to linear layout above (since it is dynamic, i cannot use through xml).

I did the following:

for (int k = 0; k < selectBike.size(); k++) {
    listView = new HorizontalListView(this, null);
    linearLayoutRoot.addView(listView);
}

But what happen is it displays only first HorizontalListView since i think it is set to match parent by default. I set the background color to the HorizontalListView and whole device screen is covered with that color. So setting it to wrap_content might solve the problem...

How to set the width to wrap content programmatically?

whole xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/horizontalListViewRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
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="com.bajaj.pulsar.pulsar.CompareListView">


    //it contains listViews dynamically
    <LinearLayout
        android:id="@+id/listViewCompareLinear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

    </LinearLayout>


<Button
    android:id="@+id/compare"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:paddingLeft="100dp"
    android:paddingRight="100dp"
    android:text="Compare" />


    </RelativeLayout>
beck
  • 2,967
  • 3
  • 16
  • 27

1 Answers1

0

use LayoutParams.

 listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100));
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
  • try with constant height in it – KDeogharkar Jun 15 '16 at 11:45
  • if i set height, i can see other listviews as well. listView.setLayoutParams(new LinearLayout.LayoutParams(200, 200)); But i want to wrap the content of listView – beck Jun 15 '16 at 11:48
  • 1
    so now you have to set height appropriate to all devices . width can be match parent as I have changed in my answer. – KDeogharkar Jun 15 '16 at 11:49
  • ok thanks anyway... i wonder why wrap content doesnt work here. But setting the constant height may have problems in different devices. – beck Jun 15 '16 at 11:52
  • do you set any background image to your horizontal list view? – KDeogharkar Jun 15 '16 at 11:54
  • yes i used adapters and it has checkbox with bg image with height and width equal to (device width / 5) – beck Jun 15 '16 at 11:56
  • I dont think it is causing any issue . I mean background to Listview. – KDeogharkar Jun 15 '16 at 11:57
  • nope but the list view is under relativelayout in xml which has bg image – beck Jun 15 '16 at 11:59
  • i have updated the whole xml in above question. I have a linearLayout which contains list view dynamically and a button in the bottom of the parent – beck Jun 15 '16 at 12:02
  • 1
    http://stackoverflow.com/questions/11295080/android-wrap-content-is-not-working-with-listview check this – KDeogharkar Jun 15 '16 at 12:07