Marco Cutecchia
Marco Cutecchia

Reputation: 1555

Items in a Listview don't look like my item layout

I'm building an app for a news website, I have created a ListView and a layout for each item, which has quite a bit of white space, but on the list view all the white space is removed. Here's what I mean: https://i.sstatic.net/VIXH8.jpg This is an item layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="175dp"
    android:gravity="center"
    android:layout_weight="0"
    android:id="@+id/article_layout"
    android:background="@drawable/gradientdemo">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:orientation="vertical"
        android:paddingLeft="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="Titolo dell'articolo"
            android:textSize="@dimen/abc_text_size_headline_material"
            android:layout_gravity="left"
            android:id="@+id/article_title" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:text="3/9/16 - Autore"
            android:layout_gravity="left"
            android:id="@+id/article_info" />
    </LinearLayout>
</LinearLayout>

This is my custom adapter:

public class ArticleAdapter extends ArrayAdapter<Article> {

    public ArticleAdapter(Context context, int textViewResourceId){
        super(context, textViewResourceId);
    }

    public ArticleAdapter(Context context, int resource, List<Article> items ){
        super(context, resource, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View v = convertView;
        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.article_item, null);
        }

        Article article = (Article) getItem(position);

        TextView title = (TextView) v.findViewById(R.id.article_title);
        TextView info  = (TextView) v.findViewById(R.id.article_info);

        if( title != null ) {
            title.setText( article.getTitle() );
        }
        if( info != null ) {
            info.setText( article.getAuthor() );
        }

        return v;
    }
}

Hope it's readable, I'm kind of new to android development.

Upvotes: 1

Views: 93

Answers (4)

Marco Cutecchia
Marco Cutecchia

Reputation: 1555

I fixed this by changing this line in the adapter

v = vi.inflate(R.layout.article_item, null); 

to this one:
v = vi.inflate(R.layout.article_item, parent, false);

Upvotes: 0

thismytime
thismytime

Reputation: 31

Your cell's LineaerLayout:

android:layout_height="0dp"
android:layout_weight="1"

change to:

android:layout_height="wrap_content"

you don't need layout_weight

Upvotes: 1

Diogo Rosa
Diogo Rosa

Reputation: 756

in the linearLayout use wrapcontent, erase layout_weigth and add weigthsum=1

Then in textViews change height to 0dp and add to each one layout_weight=0.5

I think this will work

Upvotes: 0

Loki C.
Loki C.

Reputation: 176

Delete the android:layout_weight="1" and add a correct height, e.g. android:layout_height="60dp":

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:gravity="center|bottom"
    android:orientation="vertical"
    android:paddingLeft="16dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="Titolo dell'articolo"
        android:textSize="@dimen/abc_text_size_headline_material"
        android:layout_gravity="left"
        android:id="@+id/article_title" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="3/9/16 - Autore"
        android:layout_gravity="left"
        android:id="@+id/article_info" />
</LinearLayout>

Upvotes: 0

Related Questions