Change listview Row Item height in android programmatically

How to set listview row height dynamically using layoutparams and view.

In this tutorial we are simply creating a listview and with the help of array adapter we are displaying string array into list view. But using getView() method we are accessing the list views and after that with the use of layout params we are setting up its height. So here is the complete step by step tutorial for Change listview Row Item height in android programmatically.

android-project-download-code-button

How to Change listview Row Item height in android programmatically.

Code for MainActivity.java file.

 package com.changelistviewrowitemheight_android_examples.com;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

public class MainActivity extends Activity {

 ListView listview;
 ArrayAdapter<String> arrayadapter;
 LayoutParams layoutparams;
 String[] Stringlist = new String[] {
 "ANDROID",
 "PHP",
 "BLOGGER",
 "WORDPRESS",
 "DRUPAL"
 };
 List<String> Item_List;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 listview = (ListView) findViewById(R.id.listView1);

 Item_List = new ArrayList<String>(Arrays.asList(Stringlist));

 arrayadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, Item_List){
 @Override
 public View getView(int position, View convertView, ViewGroup parent){
 
 View view = super.getView(position,convertView,parent);

 layoutparams = view.getLayoutParams();

 //Define your height here.
 layoutparams.height = 70;
 
 view.setLayoutParams(layoutparams);

 return view;
 }
 };

 listview.setAdapter(arrayadapter);
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.changelistviewrowitemheight_android_examples.com.MainActivity" >

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" >
 </ListView>

</RelativeLayout>

Screenshot:

Change listview Row Item height in android programmatically

Click here to download Change listview Row Item height in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *