I have a GridView and com.daimajia.slider.library.SliderLayout inside a LinearLayout. And this LinearLayout is a child of a ScrollView. I have given weight for GridView and SliderLayout. The problem is that SliderLayout is not expanding
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:focusableInTouchMode="true"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_main_black"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9.5"
android:minHeight="335dp" >
<com.daimajia.slider.library.SliderLayout
android:id="#+id/advertiseSlider"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_gravity="bottom"
android:gravity="center"
custom:selected_color="#FFFFFF"
custom:selected_height="8dp"
custom:selected_padding_left="3dp"
custom:selected_padding_right="3dp"
custom:selected_width="8dp"
custom:shape="oval"
custom:unselected_color="#555555"
custom:unselected_height="4dp"
custom:unselected_padding_left="3dp"
custom:unselected_padding_right="3dp"
custom:unselected_width="4dp" />
</FrameLayout>
<ScrollableGridView
android:id="#+id/categorylist"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="5"
android:background="#color/app_main_black"
android:drawSelectorOnTop="true"
android:horizontalSpacing="2dp"
android:numColumns="2"
android:stretchMode="columnWidth" />
</LinearLayout>
</ScrollView>
try this.It calculates grid view height based on children
public void setGridViewHeightBasedOnChildren(GridView gridView, int columns) {
ListAdapter listAdapter = gridView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int items = listAdapter.getCount();
int rows = 0;
View listItem = listAdapter.getView(0, null, gridView);
listItem.measure(0, 0);
totalHeight = listItem.getMeasuredHeight();
float x = 1;
if( items > columns ){
x = items/columns;
rows = (int) (x + 1);
totalHeight *= rows;
}
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight;
gridView.setLayoutParams(params);
}
After you have called setAdapter on your gridview, just call
setGridViewHeightBasedOnChildren( <yourGridView> , <no of grid view columns> )
android:layout_weight is used when you have declared a android:weightSum on your LinearLayout
android:weightSum = "14.5"
Related
I have to generate a dynamic listview in android. The listview is put inside a scrollview because I have other content too to display above the listview. Something like this :
Down part is the listview which is dynamically added. This all comes fine on a portrait mode on Samsung note5 but when I change the screen orientation to landscape the listview does not scroll. This could be because I have given wrap-content for the listview.
Please check my layout code:
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/rootLayout">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#color/gray25"
android:id="#+id/scrollView"
android:layout_above="#id/footer">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainLayout"
android:background="#color/light_blue"
android:paddingBottom="#dimen/_10sdp">
<!--Top header-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:id="#+id/topheader"
android:background="#drawable/dashboard_button"
android:layout_marginLeft="#dimen/_5sdp"
android:layout_marginRight="#dimen/_5sdp"
android:layout_marginTop="#dimen/_5sdp"
android:padding="#dimen/_5sdp">
</LinearLayout>
<!--middle portion-->
<ListView
android:id="#+id/RFIDList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transcriptMode="alwaysScroll"
android:layout_marginTop="#dimen/_10sdp"
android:background="#drawable/dashboard_button"
android:layout_marginLeft="#dimen/_5sdp"
android:layout_marginRight="#dimen/_5sdp"
android:minHeight="#dimen/_50sdp" />
</LinearLayout>
</ScrollView>
<!-- Footer aligned to bottom -->
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"
android:layout_centerInParent="true"
android:layout_marginLeft="#dimen/_15sdp">
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I cannot give fixed height to the listview because its items are generated dynamically. I tried to find the solution and also tried changing my listview to recyclerview but did not work. but Any help would be appreciated.
If you use RecyclerView use NestedScrollView instead of ScrollView and set
recyclerView.setNestedScrollingEnabled(false); after setting layout manager
for ListView see my answer over here
Use below method after listView.setAdapter(yourAdapter);
as
setlistViewHeight(listView,context);
Your problem will be solved.
public static void setlistViewHeight(ListView listView, Context context) {
ListAdapter myListAdapter = listView.getAdapter();
if (myListAdapter == null) {
return;
}
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, listView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
#SuppressWarnings("deprecation")
int screenWidth = display.getWidth();
int listViewWidth = screenWidth - 65;
int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth,
View.MeasureSpec.AT_MOST);
listItem.measure(widthSpec, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (myListAdapter.getCount()));
listView.setLayoutParams(params);
listView.requestLayout();
}
this is my layout for the same. my issue is, the list view in it is not increase the height with item count.
when I used wrap_content a given height like 210 pixel its shows the data... when I set height as wrap_content, it is showing only one item.
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/rl_createPro_option"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:visibility="gone"
android:layout_marginBottom="5dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please provide option-wise price:"/>
<ListView
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:id="#+id/rv_createPro"
android:layout_height="wrap_content" />
<RadioGroup
android:layout_width="match_parent"
android:id="#+id/rg_createPro"
android:layout_marginTop="5dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Option highlight??"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="yes"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="no"/>
</RadioGroup>
</LinearLayout>
please help me!!
This is because of your linearlayout with vertical orientation its keeping space for the lower views like radioGroups .Set height of listview dynamically use
public static void setListViewHeightBasedOnChildren(final ListView listView) {
listView.post(new Runnable() {
#Override
public void run() {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int listWidth = listView.getMeasuredWidth();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(
View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += listItem.getMeasuredHeight();
Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
}
Log.d("totalHeight " + totalHeight, "********");
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
listView.setLayoutParams(params);
listView.requestLayout();
}
});
}
I wanted to make my ListViews the size of the actual content. For example if there's 20 items I want it to display the 20 items without making the list itself scrollable like it is now.
It looks like this atm: The lists are very small and they are scrollable.
https://i.gyazo.com/eb8902a17936f54a2271b51abe228607.png
<ScrollView 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"
tools:context="com.example.tiagosilva.amob_android.ArchiveFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Tube Data"
android:textColor="#color/AMOB_yellow"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tubeData_list">
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Tool Data"
android:textColor="#color/AMOB_yellow"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolData_list">
</ListView>
<Button
android:id="#+id/btn_clear_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_buttons"
android:text="Clear all"
android:textColor="#color/AMOB_gray"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"/>
</LinearLayout>
</ScrollView>
Try to use this, might be you gotta solution for it :-
Utility.setListViewHeightBasedOnChildren(yourlistview);
add this method :-
public static class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
It will not Scroll but u can get the size of the actual content as you want.
I have a ListView inside a ScrollView.
To enable the scrolling in both ListView and ScrollView I implemented this method which I got from some other question on StackOverFlow.:-
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, ViewGroup.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);
listView.requestLayout();
}
The problem is that because the ListViewHeight is already to its fullest the OnScroll Method on the ListView doesnt get called. I want to execute some code when the user scrolls ( some Asynchronous loading of images ). Is there any workaround ?
My xml file for layout :-
<ScrollView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:background="#ffffffff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="info"
android:id="#+id/njnjj"/>
<ListView
android:id="#+id/gjghjgh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:visibility="visible"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
I understand that it is not at all a good practice to have a ListView inside ScrollView, but I need a workaround.
I'm trying to make an activity with a static header and a scrollable dynamic list. Here is the Layout:
<GridLayout
android:id="#+id/GridLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ListView
android:id="#+id/listex"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
I tried to use the height=0dp & weight=1 solution but the result is that when I add something to the list, it does not show any element.
If I use height= match_parent & weight=1 the elements are shown but is not scrollable
Please help, I tried a lot of combinations gettign no results.
Thanks.
I have been using this list view and it works fine.
Maybe you don't have enough items in you list view, for it to scroll?
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="290dp"
android:layout_weight="0.50" >
</ListView>
You can try using below codes :
<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"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="static header" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
This is my solution.
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:fadeScrollbars="true"
android:layout_weight="1" />
set ListView weight to 1. or You can do like this.
just after u set ur adapter call this method and set adapternotifydatasetchange() after dat.
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, ViewGroup.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);
listView.requestLayout();
}
your list in the GridLayout remove the parent of list view that is GridLayout it will scroll perfectly.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ListView
android:id="#+id/listex"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
The vertically scrolling GridLayout should not contain another vertically scrolling widget (ListView).You shouldn't put a ListView inside a ScrollView because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent that is its ScrollView
Set to the linearLayout weightSum = 1