How to scroll whole view not just listview? - android

In android, I would like to show a UI with fixed text and dynamic list. I added a scroll view, but it only scroll the listview(id is listview_name), not the whole view. How could I fix this issue?
Here is my code
<LinearLayout 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">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="colorBlack"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listview_name"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="colorBlack"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>

You can use ListView inside Scrollview, I have tried following method to achieve this.
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, RelativeLayout.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
Usage in activity : setListViewHeightBasedOnChildren(yourlistview);

Related

Set ListView Height programmatically in Android

I have a ListView in my activity's Layout that exists in a ScrollView like that:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:orientation="vertical"
android:padding="4dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_vertical|center_horizontal"
android:padding="5dp"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:visibility="gone" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/article_title"
android:gravity="center_vertical|center_horizontal"
android:padding="5dp"
android:text="#string/add_to_my_resources"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
</LinearLayout>
<LinearLayout
android:id="#+id/buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:id="#+id/article_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/article_title"
android:gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
<Button
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
</LinearLayout>
<ListView
android:id="#+id/authors_listView"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</ListView>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:divider="#color/light_gray"
android:dividerHeight="0.5dp"
android:groupIndicator="#null"
android:scrollbars="none"
android:textAlignment="center">
</ExpandableListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
And I have a List Adapter names MyListAdapter that populate my listview:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = mcontex.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.my_list, null);
holder = new ViewHolder();
holder.txtViewName = (TextView) convertView.findViewById(R.id.textView1);
holder.txtViewAff = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewName.setGravity(Gravity.LEFT);
holder.txtViewName.setText(name.get(position));
if (aff != null) {
holder.txtViewAff.setGravity(Gravity.LEFT);
holder.txtViewAff.setText(aff.get(position));
}
return convertView;
}
And my_list.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1">
</TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
app:srcCompat="#drawable/ic_draw"/>
</RelativeLayout>
I want to set listview height dynamically to avoid scrolling list view inside the parent layout. So I use this method:
public static void setListViewHeightBasedOnChildren(ListView listView) {
AuthorsListAdapter listAdapter = (AuthorsListAdapter) listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight=0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++)
{
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LinearLayoutCompat.LayoutParams.MATCH_PARENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));
listView.setLayoutParams(params);
listView.requestLayout();
}
This method works correctly in normal but when txtViewAff value is more than 2 or 3 lines, the last item of listview goes behind of the ExpandableListView. I add constant value like 40 to my totalHeight in method setListViewHeightBasedOnChildren:
totalHeight += view.getMeasuredHeight() + 40;
but it adds some blank space for normal items that were true without adding 40.
What can I do to resolve this problem?

Listview opening from bottom in android

I have a layout where I have to add something on top and then at bottom on list view so I don't want to scroll only I want to scroll only list view I want to scroll whole screen so I added this code.
//Remove scrolling from list view so that It scroll whole screen
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ActionBar.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
here is my xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/header"
android:orientation="horizontal"
android:layout_alignLeft="#+id/body"
android:layout_marginBottom="80dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout
android:id="#+id/body"
android:orientation="vertical"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingTop="10dp"
>
<ScrollView
android:layout_width="match_parent"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
android:weightSum="2"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:text="RSHO Doses"
android:layout_weight="1"
android:textColor="#636363"
android:textSize="#dimen/sub_Heading_Size"
android:drawablePadding="10dp"
android:drawableLeft="#drawable/rsho_drawable"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="#dimen/sub_Heading_Size"
android:text="Seizures"
android:drawablePadding="10dp"
android:drawableLeft="#drawable/seizures_drawable"
android:textColor="#636363"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:text="Events"
android:padding="#dimen/activity_horizontal_margin"
android:background="#color/colorHeadingBG"
android:textSize="#dimen/sub_Heading_Size"
android:textColor="#color/colorHeading"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:dividerHeight="2dp"
android:divider="#e5e5e5"
android:layout_alignParentTop="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_horizontal_margin"
android:id="#+id/listView"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
so when I open this application it remove scrolling from list view and add whole list view height in page but it always open from bottom. any solution for this
Try using this setting.
listView.setNestedScrollingEnabled(false);
listView.setHasFixedSize(false);
This way the scroll of ListView will be disabled.

Change ListView Height at Runtime

I am trying to implement a nested ListView at the bottom of my UI but only one item of my array is populating the screen. I've figured out that I can display all of my items if I set my Height attribute (i.e. "175dp" in lieu of "Wrap_Content"). The problem is the list size is not static, so I need to adjust the height of my ListView at runtime.
I've dug around the internets and here on StackOverflow but I can't seem to find a solution.
Thanks!
Code from my fragment:
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(rootView.getContext(),
android.R.layout.simple_list_item_1, list);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rootView.findViewById(R.id.list_linear).getLayoutParams();
LinearLayout manager = (LinearLayout) rootView.findViewById(R.id.list_linear);
lp.height = 45 * values.length;
manager.setLayoutParams(lp);
listView.setAdapter(adapter);
My XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BBDEFB"
android:padding="3dp"
android:id="#+id/movie_description_container">
<ImageView
android:layout_width="match_parent"
android:layout_height="225dp"
android:id="#+id/movie_detail_hero_image"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:gravity="top"
android:src="#drawable/sample_1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="movie title"
android:id="#+id/movie_detail_title"
android:layout_alignBottom="#+id/movie_detail_hero_image"
android:backgroundTint="#color/background_floating_material_dark"
android:textSize="36dp"
android:textColor="#BBDEFB"
android:background="#a3000000"
android:layout_above="#id/movie_detail_hero_image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:paddingLeft="18dp"/>
<ImageView
android:layout_width="175dp"
android:layout_height="215dp"
android:id="#+id/movie_detail_poster"
android:src="#drawable/sample_0"
android:layout_below="#+id/movie_detail_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="12dp"
android:gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="date"
android:textSize="22dp"
android:id="#+id/movie_detail_date"
android:layout_centerVertical="true"
android:layout_below="#id/movie_detail_hero_image"
android:layout_alignRight="#+id/movie_detail_hero_image"
android:layout_marginRight="12dp"
android:layout_marginTop="32dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Rating:"
android:textSize="22dp"
android:id="#+id/movie_detail_rating"
android:layout_below="#+id/movie_detail_date"
android:layout_alignRight="#+id/movie_detail_date"
android:layout_alignEnd="#+id/movie_detail_date"
android:layout_marginTop="6dp" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"
android:numStars="4"
android:nestedScrollingEnabled="true"
android:maxWidth="77dp"
android:layout_below="#+id/movie_detail_rating"
android:layout_alignRight="#+id/movie_detail_rating"
android:layout_alignEnd="#+id/movie_detail_rating"
android:layout_marginTop="3dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Synopsis"
android:id="#+id/movie_detail_synopsis_header"
android:layout_below="#+id/movie_detail_poster"
android:layout_alignLeft="#+id/movie_detail_poster"
android:layout_alignStart="#+id/movie_detail_poster" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="description"
android:id="#+id/movie_detail_synopsis"
android:layout_below="#+id/movie_detail_synopsis_header"
android:layout_alignLeft="#+id/movie_detail_synopsis_header"
android:layout_alignStart="#+id/movie_detail_synopsis_header"
android:layout_margin="6dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton_Favorite"
android:layout_below="#+id/ratingBar"
android:layout_alignRight="#+id/ratingBar"
android:src="#drawable/ic_favorite_white_24dp"
android:maxWidth="45dp"
android:maxHeight="45dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_linear"
android:layout_below="#id/movie_detail_synopsis">
<ListView
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="#+id/list_view2"/>
</LinearLayout>
Actually you can set the height of listview to the size of childers with this method
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}

Listview inside a scrollview in xamarin

I am a windows developer. I don't have much knowledge in android. So my scenario is like this: I have a scrollview which has its child as a linearlayout. The Linear layout has 3 listviews. I want to show all the items in all of the 3 listviews. The scrolling will be handled by the top level scrollview.
Will this work ?
<ScrollView
android:scrollbars="vertical"
android:scrollbarStyle="insideOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView1"
android:fadeScrollbars="true">
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout2">
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/linear_currentSubEmpty"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:background="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/currentListViewLayout"
android:layout_below="#+id/currentSubsView">
<com.fortysevendeg.swipelistview.SwipeListView xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:id="#+id/currentSubList"
android:listSelector="#00000000"
android:layout_width="match_parent"
android:layout_height="match_parent"
swipe:swipeFrontView="#+id/front"
android:background="#android:color/transparent"
swipe:swipeBackView="#+id/back"
swipe:swipeDrawableChecked="#drawable/choice_selected"
swipe:swipeDrawableUnchecked="#drawable/choice_unselected"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeMode="left"
swipe:swipeActionLeft="reveal"
swipe:swipeOpenOnLongPress="false"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/pastViewLayout"
android:layout_height="match_parent">
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/linear_pastSubEmpty"
android:layout_height="wrap_content"
android:layout_below="#+id/pastBottomView">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pastSubList"
android:divider="#ffffffff"
android:dividerHeight="1dp"
android:layout_weight="1" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/suspViewLayout"
android:layout_height="match_parent">
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/linear_suspendedSubEmpty"
android:layout_height="wrap_content"
android:layout_below="#+id/suspBottomView">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/suspSubList"
android:divider="#ffffffff"
android:dividerHeight="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
I got to know that there is no direct way to achieve this, I followed the following method.
public static void SetListViewHeightBasedOnChildren(ListView listView) {
IListAdapter listAdapter = listView.Adapter;
if (listAdapter == null) {
return;
}
int totalHeight = listView.PaddingTop + listView.PaddingBottom;
for (int i = 0; i < listAdapter.Count; i++) {
View listItem = listAdapter.GetView(i, null, listView);
if (listItem is ViewGroup) {
listItem.LayoutParameters = new global::Android.Views.ViewGroup.LayoutParams (global::Android.Views.ViewGroup.LayoutParams.WrapContent, global::Android.Views.ViewGroup.LayoutParams.WrapContent);
}
listItem.Measure(0, 0);
totalHeight += listItem.MeasuredHeight;
}
global::Android.Views.ViewGroup.LayoutParams parameters = listView.LayoutParameters;
parameters.Height = totalHeight + (listView.DividerHeight * (listAdapter.Count - 1));
listView.LayoutParameters = parameters;
}

Extra space While adding Grid View to ScrollView

I am using GridView which is inside scrollview(i know bad practice but requirements are like this only). The problem is their is unwanted extra vertical space coming at the end of GridView. I am also setting the height of GridView dynamically by this code.
public static void setHeightDynamically(GridView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight;
listView.setLayoutParams(params);
listView.requestLayout();
}
My xml is like this
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/header_section"
layout="#layout/header_screen" />
<ScrollView
android:id="#+id/parent_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:layout_alignWithParentIfMissing="false"
android:layout_below="#+id/header_section">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="200dp" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
></ListView>
<TextView
android:id="#+id/more"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#android:color/darker_gray"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="14sp"
android:visibility="gone" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textColor="#android:color/black"
android:textSize="14sp" />
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/txt"
android:scaleType="fitXY"
android:layout_margin="5dp"
/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txt"
android:layout_margin="5dp"
android:layout_marginLeft="3dp"
android:layout_toRightOf="#+id/icon"
android:textColor="#android:color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/demo_demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/icon"
android:textColor="#android:color/black"
android:textSize="12sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
<GridView
android:id="#+id/items_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
<include layout="#layout/nav_drawer" />
Make increment according to the number of columns in your grid view , here i am assuming number of coulmns are 2 so just Make an increment of +2 in your loop
public static void setHeightDynamically(GridView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i = i+2) {
view = listAdapter.getView(i, view, listView);
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight;
listView.setLayoutParams(params);
listView.requestLayout();
}

Categories

Resources