Alex
Alex

Reputation: 44325

How to change items in ListView in android?

I have a main activity that includes a ListView as follows:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_grid"

        android:useDefaultMargins="true"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"

        android:columnCount="4"
        android:orientation="vertical">

        <TextView
            android:text="MainTitle"
            android:textSize="32dip"
            android:layout_columnSpan="4"
            android:gravity="center_horizontal"
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ListView
            android:id="@+id/list"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:padding="10dp"
            android:minHeight="40dp"
            android:textSize="30sp"
            >
        </ListView>
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

which uses the following adapter (ListAdapter.java):

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public ListAdapter(Context context, String[] values) {
        super(context, R.layout.list_layout, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_layout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values[position]);

        // Change icon based on name
        String s = values[position];

        System.out.println(s);

        if (s.equals("WindowsMobile")) {
            imageView.setImageResource(R.drawable.icon4);
        } else if (s.equals("iOS")) {
            imageView.setImageResource(R.drawable.icon2);
        } else if (s.equals("Blackberry")) {
            imageView.setImageResource(R.drawable.icon3);
        } else {
            imageView.setImageResource(R.drawable.icon4);
        }

        return rowView;
    }
}

to simply show an icon and some text. However, the four entries shown are very small in contradiction to examples I have found here and here and here! I have changed the attributes layout_height (wrap_content, fill_parent, ...), layout_width, minHeight and textSize, but none of it seems to change the overall size of each of the ListView entries. They stay very small, and also do not correspond to the preview shown in AndroidStudio.

How can I change the size of the items in the ListView?

Upvotes: 0

Views: 431

Answers (2)

correctmeifimwrong
correctmeifimwrong

Reputation: 502

In the method getView, you inflate the layout where you define each item. There you should change the properties of the items. Also you should inflate this layout just for the first item. Your code should be

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

    if(convertView == null || !convertView.getTag().equals(TAG_NULL)) {
        convertView = inflater.inflate(R.layout.list_layou, parent, false);
    }

and the LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); in the constructor

Upvotes: 0

user4806728
user4806728

Reputation:

In list_layout.xml file you can increment textSize for TextView.

Upvotes: 2

Related Questions