android listview item height

AndroidListviewRowAndroid ArrayadapterSimplecursoradapter

Android Problem Overview


Why when i use SimpleCursorAdapter for ListView i have items height in ListView like this - listview image

(My code based on this)

But when using arrays Listview items have big height

listview big

(I learn listview based on this)

Row layout for item listview is

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

So My question is why there is a difference in row heights when using ArrayAdapter and SimpleCursorAdapter?

Android Solutions


Solution 1 - Android

  android:textAppearance="?android:attr/textAppearanceLarge" 

seemed no effect.

  android:minHeight="?android:attr/listPreferredItemHeight" 

changed the height for me

Solution 2 - Android

The trick for me was not setting the height -- but instead setting the minHeight. This must be applied to the root view of whatever layout your custom adapter is using to render each row.

Solution 3 - Android

You need to use padding on the list item layout so space is added on the edges of the item (just increasing the font size won't do that).

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:padding="8dp" />

Solution 4 - Android

I did something like that :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  
    View view = super.getView(position, convertView, parent);

    TextView textView = (TextView) view.findViewById(android.R.id.text1);
    textView.setHeight(30);
    textView.setMinimumHeight(30);

    /*YOUR CHOICE OF COLOR*/
    textView.setTextColor(Color.BLACK);
  
    return view;
}

You must put the both fields textView.setHeight(30); textView.setMinimumHeight(30); or it won't change anything. For me it worked & i had the same problem.

Solution 5 - Android

The height of list view items are adjusted based on its contents. In first image, no content. so height is very minimum. In second image, height is increased based on the size of the text. Because, you specified android:layout_height="wrap_content".

Solution 6 - Android

Here is my solutions; It is my getView() in BaseAdapter subclass:

public View getView(int position, View convertView, ViewGroup parent)
{
    if(convertView==null)
    {
        convertView=inflater.inflate(R.layout.list_view, parent,false);
        System.out.println("In");
    }
    convertView.setMinimumHeight(100);
    return convertView;
}

Here i have set ListItem's minimum height to 100;

Solution 7 - Android

This is my solution(There is a nested LinearLayout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

        <TextView
            android:id="@+id/item"
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:background="@drawable/box_arrow_top_bg"
            android:gravity="center"
            android:text="全部收支"
            android:textColor="#666"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionolegiView Question on Stackoverflow
Solution 1 - AndroidBambooView Answer on Stackoverflow
Solution 2 - AndroidEvanView Answer on Stackoverflow
Solution 3 - AndroidafollestadView Answer on Stackoverflow
Solution 4 - AndroidomnisiusView Answer on Stackoverflow
Solution 5 - AndroidsprView Answer on Stackoverflow
Solution 6 - AndroidRakin AfserView Answer on Stackoverflow
Solution 7 - AndroidPraise SongView Answer on Stackoverflow