user3736518
user3736518

Reputation: 75

How to change the width and height of listviews item in android?

I am trying to build an Application where there is a list-view with many items but I am not being able to change or set the width and height of single items.I have searched everywhere and the answer I got is making the width fill_parent,but its not working for me...

Kindly help.... thanks in advance... here are codes:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dp"
    tools:context=".CustomListViewAndroidExample" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" 
        android:layout_weight="1"/> 

</RelativeLayout>

Upvotes: 0

Views: 2775

Answers (2)

Ryhan
Ryhan

Reputation: 1885

This link shows you how to do it in java with your own custom adapter.

When overriding the getView() in your adapter, your can modify the height before supplying your view to the framework for render. Also, note that you do not have to use a SimpleCursorAdapter, an ArrayAdapter can also be used in the same fashion.

final SimpleCursorAdapter adapter = new SimpleCursorAdapter (context, cursor) {
    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        final View view = super.getView(position, convertView, parent);
        final TextView text = (TextView) view.findViewById(R.id.tvRow);
        final LayoutParams params = text.getLayoutParams();

        if (params != null) {
                params.height = mRowHeight;
        }

        return view;
    }
}

Upvotes: 0

Wilson
Wilson

Reputation: 176

If you want to change the height of list view dynamically, you can use

list.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,    theSizeIWant)); 

or

import android.view.ViewGroup.LayoutParams;
ListView mListView = (ListView)   findViewById(R.id.listviewid);
LayoutParams list = (LayoutParams) mListView.getLayoutParams();
list.height = set the height acc to you;//like int  200
mListView.setLayoutParams(list);

Upvotes: 1

Related Questions