How to set the ListView Rows Height
Asked Answered
R

5

40

The image below shows the height of the rows are not same. I want the rows to have the same height.

enter image description here

In my program I'm getting the data from the XML service which I parsed and displays the result on the list view. But the height of the ListView rows is not the same.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:background="@drawable/list_selector"
  android:padding="3dip"
  android:layout_width="match_parent"
  android:gravity="center"
  android:layout_height="10dp">

    <TextView android:layout_height="wrap_content" android:text="ITEM"
        android:layout_width="wrap_content" android:id="@+id/txtItem"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="15dip"
        android:textStyle="bold"
        android:textColor="#040404"></TextView>
    
    <TextView android:layout_height="wrap_content" android:text="MANUFACTURER"
        android:layout_width="wrap_content" android:id="@+id/txtItemTwo"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14dip"
        android:textColor="#FF7F50"
        
        ></TextView>
    

</LinearLayout>

Setting the height

android:layout_height="10dp"

but still not same in the ListView.

This is my custom adapter for getView :-

 public class MyEventAdapter extends BaseAdapter {
   
     ArrayList < String > listTitle;
     ArrayList < String > listFullText;

     Activity activity;

     public MyEventAdapter(Activity activity, ArrayList < String > listTitle, ArrayList < String > listFullText) {
         super();
         this.listTitle = listTitle;
         this.listFullText = listFullText;

         this.activity = activity;
     }

     public int getCount() {
         // TODO Auto-generated method stub
         return listTitle.size();
     }

     public Object getItem(int position) {
         // TODO Auto-generated method stub
         return null;
     }

     public long getItemId(int position) {
         // TODO Auto-generated method stub
         return 0;
     }

     private class ViewHolder {
         TextView txtViewTitle;
         TextView txtViewTitleTwo;

     }

     public View getView(int position, View view, ViewGroup parent) {
         // TODO Auto-generated method stub

         ViewHolder title;
         LayoutInflater inflater = activity.getLayoutInflater();

         if (view == null) {
             view = inflater.inflate(R.layout.lview_row, null);
             title = new ViewHolder();

             title.txtViewTitle = (TextView) view.findViewById(R.id.txtItem);
             title.txtViewTitleTwo = (TextView) view.findViewById(R.id.txtItemTwo);

             view.setTag(title);
         } else {
             title = (ViewHolder) view.getTag();
         }

         title.txtViewTitle.setText(listTitle.get(position));
         title.txtViewTitleTwo.setText(listFullText.get(position));

         return view;
     }
 }

Anyone please help me the proper way to get the desired result.

Runofthemine answered 30/4, 2012 at 7:5 Comment(5)
If you are using custom adapter then post your code of getView method from custom adapter.Studley
Your implementation should work, you must be doing something else somewhere :).Victory
hi @BharatSharma i just post my Custom Adapter Code please have a look thanks...Runofthemine
run lint on your code, tools.android.com/tips/lintTingly
use view.setMinimumHeight(minHeight); to set height of your view inside your getView() method of your MyEventAdapter classPneumothorax
S
138

the problem is that you are mis-using the inflator, don't give null as the root node

if you pass null as the root node, all the size parameters you set in the xml fail to work

the correct code is

view = inflater.inflate(R.layout.lview_row, parent, false);

http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

Strephonn answered 22/6, 2013 at 16:19 Comment(2)
even though this makes some sense, this doesn't work in every case. Better answer with more introspection needed, as the provided link doesn't dig deep enough into the real issueStruggle
some items are hiding please helpAquarium
P
1

override method getView(int position, View convertView, ViewGroup parent) in your MyEventAdapter class and use view.setMinimumHeight(minHeight); to set minimum height of view.

set minimum height of view with in method `getView(int position, View convertView, ViewGroup parent)' as following:

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder title;
    LayoutInflater inflater = activity.getLayoutInflater();

    if (view == null) {
        view = inflater.inflate(R.layout.lview_row, null);
        view.setMinimumHeight(minHeight); //set minimum height of view here
        title = new ViewHolder();

        title.txtViewTitle = (TextView) view.findViewById(R.id.txtItem);
        title.txtViewTitleTwo = (TextView) view.findViewById(R.id.txtItemTwo);

        view.setTag(title);
    } else {
        title = (ViewHolder) view.getTag();
    }

    title.txtViewTitle.setText(listTitle.get(position));
    title.txtViewTitleTwo.setText(listFullText.get(position));

    return view;    
}
Pneumothorax answered 30/4, 2012 at 7:57 Comment(0)
S
1
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:background="@drawable/list_selector"
  android:padding="3dip"
  android:layout_width="match_parent"
  android:gravity="center"
  android:layout_height="match_parent">

    <TextView android:layout_height="wrap_content" android:text="ITEM"
        android:layout_width="wrap_content" android:id="@+id/txtItem"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="#040404"></TextView>

    <TextView android:layout_height="wrap_content" android:text="MANUFACTURER"
        android:layout_width="wrap_content" android:id="@+id/txtItemTwo"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14sp"
        android:textColor="#FF7F50"

        ></TextView>


</LinearLayout>

If you are having more than one line in your text then set your text as single line so that it will not come in second line and change your height of each row.

Studley answered 30/4, 2012 at 8:37 Comment(0)
A
0

If you are having more than one line in your text then set your text as single line so that it will not come in second line and change your height of each row.

Archway answered 22/8, 2015 at 14:44 Comment(0)
J
-2

First of all you should use sp to specify the size of a text not dip (dp), second i think you have some contradictory dimensions you have 2 text fields of 15 + 14 dp in a layout of ONLY 10 dp. Try to increase your layout height to 40 dp and change your textview's dimensions to use sp

Jenjena answered 30/4, 2012 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.