3

I wanted to use listview in a fragment in Navigationdrawer implemented using actionbarsherlock lib. The problem is the listView height is not getting scaled to match_parent . It is getting scaled upto one item of listview. I have searched a lot, tried similar questions and also tried changing parent layout to Relative_layout, Linear_layout and Frame_layout. I am not getting what I am doing wrong. How can I scale my listView to acquire the rest of the size of the layout.

this is how it looks in editor

this is how it looks in editor

this is how it looks in app. The size of listView is scaled to an item. For more items I have to scroll withing that space.

this is how it looks in app

My main fragment layout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/body_background_green"
android:orientation="vertical"
android:padding="5dp" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="30dp"
    android:text="@string/farmerlist"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/farmercode" />

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/farmercode"
        android:inputType="number" >

        <requestFocus />
    </AutoCompleteTextView>
</LinearLayout>
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/transparent"
        android:dividerHeight="2dp" >
    </ListView>

</LinearLayout>

this is the item layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="@+id/ll1"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Code : " />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="code"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name : " />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Type : " />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="type"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

</LinearLayout>

This is my activity layout

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:background="@color/body_background_green"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="6dp"
    android:paddingRight="6dp"
    android:scrollbarStyle="outsideOverlay">
     <FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    </FrameLayout>

</ScrollView>
<!-- android:layout_gravity="left" tells DrawerLayout to treat
     this as a sliding drawer on the left side. The drawer is
     given a fixed width in dp and extends the full height of
     the container. A solid background is used for contrast
     with the content view. -->
<ListView android:id="@+id/left_drawer"
          android:layout_width="250dp"
          android:layout_height="match_parent"
          android:layout_gravity="left"
          android:background="@android:color/white"/>
</android.support.v4.widget.DrawerLayout>

This is my Sherlockfragment

public class Startcollection extends SherlockFragment {
AutoCompleteTextView auto;
ListView lv;
Fadapter adapter;
ArrayList<String> list;
ArrayAdapter<String> autoadapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.startcollectionfrag,
            container, false);
            auto=(AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView1);
    lv=(ListView) rootView.findViewById(R.id.listView1);
    list=new ArrayList<String>();
    for(int i=0;i<Constant.vec_prod.size();i++)
    {
        list.add(Constant.vec_prod.get(i).getCode()
                +" "+Constant.vec_prod.get(i).getFirstname());
    }
    adapter=new      Fadapter(getActivity(),R.layout.listitem,Constant.vec_prod);
    auto.setThreshold(1);
    autoadapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,list);
    auto.setAdapter(autoadapter);
    lv.setAdapter(adapter);
    return rootView;
}

public class Fadapter extends ArrayAdapter<ProducerModel>
{
    Activity context;
    Vector<PModel> vecprod;
    TextView code,name,type;
    LinearLayout ll;
    public Fadapter(Activity context, int textViewResourceId, Vector<PModel> vecprod) {
        super(context, textViewResourceId,vecprod);
        this.context=context;
        this.vecprod=vecprod;
    }

    public View getView(int position, View view, final ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.listitem, null);
        code=(TextView) rowView.findViewById(R.id.textView2);
        name=(TextView) rowView.findViewById(R.id.textView4);
        type=(TextView) rowView.findViewById(R.id.textView6);
        ll=(LinearLayout) rowView.findViewById(R.id.ll1);
        code.setText(vecprod.get(position).getCode()+"");
        name.setText(vecprod.get(position).getFirstname());
        type.setText(vecprod.get(position).getTypes());
        return rowView;
    }

}
}
Vikas Mane
  • 789
  • 1
  • 6
  • 14

2 Answers2

7

Use LinearLayout weight for your ListView and have a try:

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" 
    android:background="@drawable/transparent"
    android:dividerHeight="2dp" >
</ListView>

Update: after you post your Activity layout, the problem is obvious. It's because your put a ListView into a ScrollView. I think your ScrollView is of no use, you can remove it and change the layout_height of your frame_container to match_parent and have a try.

Refer to the following post to see why you can't put a ListView into a ScrollView:

Android list view inside a scroll view

How can I put a ListView into a ScrollView without it collapsing?

Community
  • 1
  • 1
Lei Guo
  • 2,550
  • 1
  • 17
  • 22
1

Change your main layout's parent Linearlayout to Relative layout

Pavya
  • 6,015
  • 4
  • 29
  • 42
  • and also tell me on which device you are running your code? Because some motorola devices does not show full listview. please run on other device and check before editing code – Pavya Jan 22 '15 at 08:34
  • I just tried Relative_layout...its still not working..I have tested this on Micromax canvas A1 and Xiaomi redmi Note... – Vikas Mane Jan 22 '15 at 08:57
  • @ Victor what was the problem? – Pavya Jan 22 '15 at 09:22
  • 1
    it is not about `RelativeLayout` or `LinearLayout` there is a `ScrollView` that causing the problem it is very bad using a `Scrollable` view inside another. – Kaushik Jan 22 '15 at 09:28