Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify the Layout params of a child in a ListView in Android?

Tags:

android

Anyone help me to modify the layout params of a child in a ListView in Android.Please give some code snippets if you can.

like image 335
Rajapandian Avatar asked Jun 10 '09 13:06

Rajapandian


People also ask

What are the different attributes of layoutparams in Android?

ViewGroup.LayoutParams has only two attributes: height and width. It’s subclass - ViewGroup.MarginLayoutParams inherits these two attributes and has four of its own: bottomMargin, leftMargin, rightMargin, topMargin.

How to use layoutparams in Eclipse?

In layout-files a root element is usually LinearLayout, so we will use it too. Update the imports CTRL+SHIFT+O. Eclipse will ask which exactly LayoutParams we want to use. We will explore this in more detail. Let’s recall the theory about screens. A screen consists of the ViewGroup and View elements inside it.

Is it possible to add a view to linearlayout?

//keep in mind that LinearLayout is not the only Layout, e.g. you can use ConstrainLayout etc... //after, you need to add yourView to a layout as it won't redraw itself if you just change the layout parameters e.g.:


3 Answers

Here is a long series of blog posts that I found really useful when I started to create my first custom ListView.

NOTE: every word is a separate link ;)

like image 93
JaanusSiim Avatar answered Nov 14 '22 22:11

JaanusSiim


The childs need the LayoutParams from the parent so:

First - The inflater:

convertView = this.inflater.inflate(R.layout.row_main_menu,parent,false);

Look the 2 and 3 parameter.

Second - The LayoutParams:

The ListView LayoutParams not appear in eclipse intellisense! Try this:

ListView.LayoutParams params = new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT,ListView.LayoutParams.WRAP_CONTENT);
convertView.setLayoutParams(params);
like image 25
Dennso Avatar answered Nov 14 '22 21:11

Dennso


Write your own Adapter class and overwrite the getView method. The getView method is called for each position in the list.

You'll have to overwrite some other methods too as given here

You can create custom views and return them from the getView method. Remember to use the older view passed down to the method if its possible for optimum memory usage

like image 28
Prashast Avatar answered Nov 14 '22 21:11

Prashast