Second list view not expanding in linear layout fragemnt - android

My linear layout having two list views, when i add items in second list view, its not expanding. its getting scrolled automatically,
i have used match_parent in my linear layout, even though the second list view (getting scrolled instead of expanding) or the linear layout is not expanding.
Can anyone please help me to expand the list view or the linear layout.
problem in linear layout or the second list view?
but the first list view expanding properly.
fragment_one.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:descendantFocusability="blocksDescendants"
android:padding="10dip" >
<ListView
android:id="#+id/list_nation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#B29090"
android:nestedScrollingEnabled="true">
</ListView>
<ListView
android:id="#+id/list_regional"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#4A9C67"
android:focusable="false"
android:nestedScrollingEnabled="true">
</ListView>
</LinearLayout>

When you add multiple scrolls in a single screen it always shows problems. Because they conflict with each other.
There are two solution for your problem.
If you want to keep your each listview to take half screen then add "weightsum = 100" in your main linear layout and set weight of both listviews to "50".
If you want your first list to scroll till end and at the end of first you want the second one to start scrolling, then calculate the height of your lists on run time and assign the calculated height to your listviews. Check this: Android: How to measure total height of ListView

Related

Making StaggeredGridLayout wrap content

I need to display a staggered grid within a linear layout.
For that I have used a StaggeredGridLayoutManager on a RecyclerView from android.support.v7.widget. The problem is that StaggeredGridLayoutManager doesn't support wrap_content.
There are other questions addressing the issue, but they are concerned with linear layouts, not staggered grids:
Not able to add empty view below Recyclerview
How do I make WRAP_CONTENT work on a RecyclerView
As far as I understand I could derive StaggeredGridLayoutManager and implement onMeasure. Is there a way do to that without recalculating the positions and sizes of the children myself? When looking at the StaggeredGridLayoutManager.java source, I can see that it uses ScrollbarHelper to approximate the size of the scrolling content. Is there a way to reuse that?
The problem is that when RecyclerView is drawn, it calculates all the remaining size to itself before drawing the next elements and don't recalculate after the other elements are drawn, leaving them outside the screen.
There is an easy fix for this problem: The trick is to draw all other elements first, and leave RecyclerView for last. Use a relative layout and put the RecyclerView last on the XML layout file. Since with relative layout you can put each element wherever you want independently of the order on the XML file, you will draw all elements before RecyclerView and this will make it calculate the accurate remaining space and wrap_content will work properly.
Example to add a paginagion bar below the RecyclerView:
<?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:padding="16dp"
tools:context=".MainActivity"
>
<LinearLayout
android:id="#+id/pagination_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"> //HERE YOU ALIGN THIS ELEMENT TO THE BOTTOM OF THE PARENT
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/previous_btn_label"/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/next_btn_label"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/items_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_above="#id/pagination_btns"/> //HERE YOU ALIGN THE RECYCLERVIEW ABOVE THE PAGINATION BAR
</RelativeLayout>
I ended-up using a custom control for this, inspired by:
https://github.com/expilu/AntipodalWall/blob/master/library/src/com/antipodalwall/AntipodalWallLayout.java

Android View to fill center area of Layout

I have this layout:
All views fill the entire space horizontally, and they're inside a LinearLayout oriented vertically. The blue parts have a fixed height (they have the wrap_content property).
The red part is a ListView. How can I make it fill that center space if there are not enough elements in the list and at the same time preventing it to push the last two elements down if it has more elements?
So far it doesn't push down the two views under it (with the layout_weight="1" property), but if it doesn't have enough elements, it shrinks and makes those two elements go up, leaving an ugly white space under them.
This is what happens:
This is what I expect:
Notice that even though the ListView is smaller, the two last views don't go up.
So far I've tried:
Giving all views a weight (ugly display but sort of works).
Giving each view a size (different results on different devices).
Giving the last view the android:gravity="bottom" property, but the view still goes up.
What may work
I've been messing around and I think a RelativeLayout may work, with a property like layout_alignBottom that instead of aligning to the end of the given view, it aligned to the start of it.
SOLUTION
The solution was to use a RelativeLayout and set the list's layout_above and layout_below properties to that of the elements I want to align it to.
<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"
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.stackoverflow.app.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="#string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="#string/hello_world" />
</LinearLayout>
Here's a couple of options that may work for you. Let me know how it goes.
Option 1:
Set the containing vertical orientated linear layout to fill_parent / match_parent (they are the same). Then set the gravity or layout gravity of the bottom 2 views to bottom.
Option 2:
Contain the list view in a linear layout with a fixed height. Set the list view to wrap_content.
EDIT
You could use relative layouts for this, this link here seems to do what you need
http://www.javacodegeeks.com/2013/10/android-fixed-header-and-footer-with-scrollable-content-layout-example.html
How about wrapping your list view inside the a layout and give the layout the fixed height.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="300dp" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>

ListView not getting space to show content on smaller screens

So I am developing a screen where there are some images and buttons on top and Below that is a list view which shows a list of some activity.
The design is something like this :-
Now on smaller screen the ListView height becomes very small as the screen space is taken up by the above icons and images.
So how can i increase the height of the Linearlayout or ListView so that user can scroll to the see the rest of the ListView.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
.... Other Layouts .....
<ListView
android:id="#+id/listArea"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingLeft="#dimen/list_padding"
android:paddingRight="#dimen/list_padding" />
</LinearLayout>
Edit: Tried using the top view as a header to the List but since I want an EmptyView too, this is creating a problem as it replaces the whole header + listview
From what I read about that issue, you should specify the Views on top as header of the list, and the'll scroll properly.
Afaik this only works if the list is non-empty, as the empty view replaces the whole list including headers.
You can use the weightSum and layout_weight attributes to control how much of the parent's available space a child view will take up. To use these, a parent layout, for example, your LinearLayout, gets the android:weightSum attribute. Each child layout gets the android:layout_weight attribute, where the sum of all child weights is the weightSum of the parent. In addition, each child should have their layout_height or layout_width set to 0dp, whichever is going to be decided by the weight.
Here's an example based on your diagram. Let's say you want the two top views to take up 1/4 of the screen each, and the ListView to take up the bottom half. Add android:weightSum="4" to your LinearLayout, android:layout_weight="1" to the two child layouts that you represent with ...Other Layouts..., and android:layout_weight="2" to the ListView. Code might look something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="4">
<ImageView
android:layout_weight="1"
android:layout_height="0dp"
...some other attributes.../>
<LinearLayout
android:layout_weight="1"
android:layout_height="0dp"
android:orientation="horizontal"
...some other attributes...>
...some children of the LinearLayout...
</LinearLayout>
<ListView
android:id="#+id/listArea"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingLeft="#dimen/list_padding"
android:paddingRight="#dimen/list_padding"
android:layout_weight="2"/>
</LinearLayout>

Force an ExpandableListView to move other items down

My layout is a ScrollView with some TextView and other controls and also an ExpandableListView.
What I want to do is When Expanding the ExpandableListView the controls which are below it move down and again upon collapsing, all the controls move up and only Group of Expendables become Visible.
The problem is when using wrap_content for expandView's Height the expanding of it shows nothing and just the indicator (little arrow) shows that it's expanded, and when explicitly use some numbers eg. 200dp, the lowest items of expandView not shown. (Because of using two Scrolling widget together).
Here's my simplified layout.
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="HardcodedText" >
<LinearLayout>
//some controls
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ExpandableListView
android:id="#+id/lstDrugs"
android:layout_width="match_parent"
//HERE is the point that wrap_content only shows the groups header
android:layout_height=**"wrap_content"**
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:divider="#drawable/dividergradient"
android:dividerHeight="2dp" >
</ExpandableListView>
<Button
android:id="#+id/btnAddDrug"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/lstDrugs"
android:text="Add" />
</RelativeLayout>
</LinearLayout>
I've just found my answer in this thread.
The problem is with using List inside ScrollView. To handle it you should use a layout other than the ScrollView (eg. LinearLayout) and add other widgets as Header and Footer of the list. This would enable the scrolling of the view and also expanding of the List.

linearlayout doesn't show all childs

I have problem in this layout that : it doesn't show any child after the listView
and the listview is filling this sight ; it scrolls but didn't show the any other child
I have tried to user ScrollView but it make issues with expandlist
this is layout xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/select_bg"
android:orientation="vertical" >
<TextView
android:id="#+id/Header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/header_bg"
android:gravity="center"
android:paddingRight="50dip"
android:paddingTop="5dip"
android:text="#string/select"
android:textColor="#60240a"
android:textSize="18dip"
android:textStyle="bold" >
</TextView>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:divider="#drawable/line_divider"
android:dividerHeight="3dip"
android:listSelector="#android:color/transparent" >
</ListView>
<ImageView
android:layout_width="fill_parent"
android:layout_height="3dip"
android:src="#drawable/line_divider" />
<ExpandableListView
android:id="#+id/expandableList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:childDivider="#drawable/line_divider"
android:divider="#drawable/line_divider"
android:dividerHeight="2dip"
android:listSelector="#android:color/transparent"
android:textColor="#60240a" >
</ExpandableListView>
</LinearLayout>
When you are using ListView in the layout you have to add this property android:layout_weight for the listview. Without this property being set, you cannot get all views.
Since your layout is linear any views that exceeds the limit of the screen are not displayed in the screen. Yes scrollview makes issues with list or expandable listview as the scrollview itself is scrollable it will consume the scrolls of listview and expandablelist. To resolve this you have to re order or change your layouts to make your rest of the views visible
Give the first ListView some constant height. Like
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="300dip"
....
/>
LinearLayout will not scroll when its content increases. And you cannot use a ScrollView as parent for ListView.
Set all layout_height - attributes from direct childs of your root layout to fill_parent.
Than give every of these layouts a attribute layout_weight.
The value of layout_weight is an integer according to how many space they've been granted in the view.
For example two views: one with weight 2 and one with weight 1: The weight 1 is twice the size of weight 2.
Just play a little with the weight attribute to find out for yourself.
You have given height of listview as wrap_content so it will occupy the all the space in screen so u give the height of listview fixed and same thing applied to expandable list also give fix height to that also.
Hope u get it All The best

Categories

Resources