How to calculate total row heights of listView in android?

Developing apps for mobile devices come fun when you get rid of those problems which are not so easy at all. Your apps development are getting stuck but you are passionate guys who love to write code for android devices. Few days ago I got same problem that was so painful to me as well as to my clients. As I am not good writer but share it to you and helping other programmers, I like to solve this issue through hands on code sample. Along this discussion I will help you to calculate total row heights of android listview

What was my problems: 

My problem was to add comments under VideoView where A scrollview has covered videoview and commentsview. Actually commentsview was a listview. I had to calculate the total heights of listview rows. Mostly it calculated the accurate height of the listview but when comments content is too much then it cannot provide you the actual height. 

Way to calculate total row heights of listView in android: 

To solve this problem, I am trying to write two xml file and three class. Here are my all xml files and classes. 

MainXML file: 


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/scrollView1"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:text="Hi I" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:text="I am Sharif" />

        <ListView
            android:id="@+id/myListView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:dividerHeight="10dp" >
        </ListView>
    </LinearLayout>

</ScrollView>

Comments XML: 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingLeft="25dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/txtCommentText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="nice introduction"
        android:textColor="#171717"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/txtCommenterName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtCommentText"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="5dp"
        android:text="" />

</RelativeLayout>

The Data Class: 
package com.example.listviewheigh;

public class CommentsViewData {
private String data;
   private String user;
   private String name;
   public CommentsViewData(String data,String user,String name){
       this.data = data;
       this.user = user;
       this.name = name;
   }
   public String getData(){
       return data;
   }
   public void setData(String data){
       this.data = data;
   }
   public  String getUser(){
       return user;
   }
   public void setUser(String user){
       this.user = user;
   }
   public String getName(){
       return name;
   }
   public void setName(String name){
      this.name = name;
   }
}
The Adapter Class: 
package com.example.listviewheigh;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CommentsAdapter extends ArrayAdapter<CommentsViewData> {
Context context;
CommentsViewData comment;
List<CommentsViewData> comments;
private String currentUser = "";

public CommentsAdapter(Context context, int resource,
List<CommentsViewData> items) {
super(context, resource, items);
this.context = context;
this.comments = 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.commentsdetailslayout, null);
}

comment = comments.get(position);// items.get(position);

if (comment != null) {
TextView CommentTextView = (TextView) v
.findViewById(R.id.txtCommentText);
TextView commenterView = (TextView) v
.findViewById(R.id.txtCommenterName);

if (CommentTextView != null) {
CommentTextView.setText(comment.getData());
}
if (commenterView != null) {
commenterView.setText(comment.getName());
}

}
return v;

}
}

class Helper {
public static void getListViewSize(ListView myListView, Context context) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
// do nothing return null
return;
}
// set listAdapter in loop for getting final size
int totalHeight = 0;
Log.d("numberofrow", "" + myListAdapter.getCount());
for (int size = 0; size < myListAdapter.getCount(); size++) {

View listItem = myListAdapter.getView(size, null, myListView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
/* listItem.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));*/

WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidth = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated

int listViewWidth = screenWidth - 10 - 10;
int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth,
MeasureSpec.AT_MOST);
listItem.measure(widthSpec, 0);

totalHeight += listItem.getMeasuredHeight();
}
// setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight
+ (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
myListView.requestLayout();
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
}

The Main Class: 
package com.example.listviewheigh;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
private ArrayList<CommentsViewData> commentsViewDatas;
private CommentsAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView commentsListView = (ListView) findViewById(R.id.myListView);

commentsViewDatas = new ArrayList<CommentsViewData>();

commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (1) ", " 1 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif with rani" +
"and I take this shord" +
"with araweasfasfas" +
"asdfasfasfasfasf" +
"asdfasdfasfasfas" +
"asdfasdfasdfasfasfasfasdfsadasdfa" +
"sadfasfasdfasfasfasdfdgfghfghfgjfhfgrptkdfhdf (2) ", " 2 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (3) ", " 3 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (4) ", " 4 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif with rani" +
"and I take this shord" +
"with araweasfasfas" +
"asdfasfasfasfasf" +
"asdfasdfasfasfas (5) ", " 5 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (6) ", " 6 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (7) ", " 7 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (8) ", " 8 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (9) ", " 9 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (10) ", " 10 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (11) ", " 11 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (12) ", " 12 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (13) ", " 13 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (14) ", " 14 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (15) ", " 15 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (16) ", " 16 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (17) ", " 17 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (18) ", " 18 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (19) ", " 19 ",
"Toko toko"));
commentsViewDatas.add(new CommentsViewData("Hello I am Sharif (20) ", " 20 ",
"Toko toko"));
adapter = new CommentsAdapter(MainActivity.this,
R.layout.commentsdetailslayout, commentsViewDatas);
commentsListView
.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
commentsListView.setAdapter(adapter);
Helper.getListViewSize(commentsListView, MainActivity.this);
}
}


Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP