ListView below scrollview in Android - android

I am trying to put a ListView below a ScrollView in android. I tried putting them inside a LineaLayout like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/marketDetailScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
...
</ScrollView>
<ListView android:id="#android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FFF"/>
</LinearLayout>
and the ListView doesn't get shown. I also tried putting it inside a RelaviteLayout and still nothing. Can I somehow have a ListView under a ScrollView?
Just to add something. I don't want to split my screen so that I have a half with a ScrollView and another half with a ListView. I want the user to scroll down the ScrollView which apparently is bigger than the screen size and then the ListView should start

I would recommend you to put the ScrollView Content as HeaderView in the ListView or do you explicitly want to have two separated scrollable areas on screen?
Example for putting the content of the scroll view in the list view as header (one single scrollable area):
public void onCreate(Bundle s){
setContentView(R.id.yourLayout);
ListView listView = (ListView) findViewById(R.id.listView);
// Set the adapter before the header, because older
// Android version may have problems if not
listView.setAdapter(new YourAdapter());
// Add the header
View header = inflater.inflate(
R.layout.you_layout_that_was_in_scrollview_before, null, false);
listView.addHeaderView(header);
}
The layout of the activity would look like that:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="#+id/listView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#FFF"/>
</LinearLayout>
If you want two scrollable areas, you should work with layout weight:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/marketDetailScrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<!-- ScrollViewContent -->
</ScrollView>
<ListView android:id="#android:id/list"
android:layout_height="0dp"
android:layout_width="match_parent"
android:background="#FFF"
android:layout_weight="1"/>
</LinearLayout>

Fill parent wont work for you,
change it to wrap content, and give layout_weight 1 to scroll view also, then it may work for you

Normally it is not good to put listview in scrollview. but if in case it is required, you need to set the height of listview. Follow the link below to calculate the height of listview for fitting in scrollview.
http://www.jiramot.info/android-listview-in-scrollview-problem
Using this way you can achieve what you want

You have to set the the height of the content of scrollview and its element to 'wrap content'. XML will be like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/marketDetailScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
...(height will be wrap_content for inner element as well)
</ScrollView>
<ListView android:id="#android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FFF"
android:layout_weight="1"/>
</LinearLayout>
This will might help you..

I think this is near to what you are looking for. I give you this code code but i have to tell that putting a ListView in a ScrollView is a bad-practice and you should not use this solution. You should work on something more correct and with no scroll view within scroll view.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="#+id/containerScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/scrollViewContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ScrollView
android:id="#+id/marketDetailScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/scrollViewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- the views contained in your ScrollView goes here -->
</LinearLayout>
</ScrollView>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF" />
</LinearLayout>
</ScrollView>

Related

android two listview one below the other

I have the two listviews listview2 and listview3
what i want is listview3 should be displayed below listview2 but it gets overlapped
How can i get the correct output i.e to be displayed one below the other
and last ListView is navigation drawer
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</RelativeLayout>
<RelativeLayout
android:layout_below="#id/container"
android:id="#+id/container1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listview3"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</RelativeLayout>
<ListView
android:id="#+id/drawerlist"
android:background="#0099CC"
android:cacheColorHint="#00000000"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left" ></ListView>
</android.support.v4.widget.DrawerLayout>
Use LinearLayout with weight attribute.
<LinearLayout
android:id="#+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listview2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"/>
<ListView
android:id="#+id/listview3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5" />
</LinearLayout>
The first RelativeLayout takes the full height of the screen. You'll need to limit the height of the first RelativeLayout otherwise the second RelativeLayout will always be out of the View.
**Its overlapping because:
1. you are setting the height of the list to : match_parent
There is no reference given in relative layout**
Try this:
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
<ListView
android:id="#+id/listview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/listview1"></ListView>
<ListView
android:id="#+id/listview3"
android:layout_below="#id/listview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</RelativeLayout>
You can use layout_weight property to make both listview same height.
For example, you can follow below hierarchy -
<LinearLayout orientation="vertical">
<ListView layout_weight=1></ListView>
<ListView layout_weight=1></ListView>
</LinearLayout>
Nest both ListViews in the same relative layout block, one right after the other. Give them both the same weight
<RelativeLayout
android:id="#+id/container
...
<ListView
android:id="#+id/listview2"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
...
></ListView>
<ListView
android:id="#+id/listview3"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_below="#id/listview2"
...
></ListView>
</RelativeLayout>
I haven't been able to test this, so it may need a few tweaks, but give it a shot.
I would also recommend spending some time formatting your code when you post it because it helps us try to figure out what you're doing.
Taken from https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
To use a DrawerLayout, position your primary content view as the
first child with a width and height of match_parent. Add drawers as
child views after the main content view and set the layout_gravity
appropriately. Drawers commonly use match_parent for height with a
fixed width.
So both your second RelativeLayout and drawerlist are drawers. In any case, it's unclear what exactly you want to achieve, so you have to describe that in more detail. If you want listview3 to come up from the bottom and listview2 to make room for it, you will have to use a different layout or set listview3 to a specific height and animate your first RelativeLayout to exactly that height (using ValueAnimator for bottom margin I'd say).
Edit:
After reading a comment of yours, it doesn't seem like something you want to achieve. In that case, what I describe above is the culprit, i.e. the second RelativeLayout being interpreted as a drawer. Make sure you combine your RelativeLayouts to a single layout (preferrably LinearLayout with weights).

ScrollView throws bottom views out

I have a LinearLayout with a ScrollView inside and a checkbox underneath the scrollview. Whenever i put alot of text in the scrollview, it will expand through the whole screen and kick out the bottom checkbox view. How do i prevent that from happening?
Basically, i want the checkbox to always be right underneath the scrollview. I also want the scrollview to shrink and expand depending on its text. When theres alot of text inside, it should expand to the point where the checkbox is still visible at the bottom of the screen.
It should look like this:
Where the filled ScrollView should have a scroller if the text is too much, rather than kicking out the checkbox from the screen
Help!
My layout looks like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- TextView inside --->
</ScrollView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:gravity="top"
android:orientation="horizontal" >
<!-- CheckBox inside --->
</RelativeLayout>
</LinearLayout>
Dont worry about the syntax, or all the stuff that are inside, I wrote this quick because the code inside is really long and unnecessary!
Try this layout :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1">
<!-- TextView inside --->
</ScrollView>
<!-- CheckBox Here --->
</LinearLayout>
Let's start simple. Try replacing your parent LinearLayout with a RelativeLayout. If this doesn't work, you may have to fix the height of ScrollView to some fixed height using weight, or pixels (which is non-ideal):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- TextView inside --->
</ScrollView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:gravity="top"
android:orientation="horizontal" >
<!-- Stuff inside --->
</RelativeLayout>
</RelativeLayout>
Define a fixed size to your ScrollView. As bigger as the text becomes, the scroll is defined to that size (like layout_height="100dp")
UPDATE
Try something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="200dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- TextView's inside - -->
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- CheckBox's inside - -->
</LinearLayout>
</LinearLayout>

Listview not scrolling

I have a listview that holds expandable cards. Each card is 2 frame views, one for the un-expanded view and one for the expanded view. In the expanded view I have another listview - that doesn't scroll. Can anyone tell me why? Here's some code:
First listview:
<LinearLayout
android:background="#null"
android:id="#+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/groups_listview"
android:layout_width="match_parent"
android:divider="#null"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
2nd listview (Expanded Card Content):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bootstrapbutton="http://schemas.android.com/apk/res-auto"
xmlns:fontawesometext="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_expandablelistitem_card_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/corners_white_transparent"
android:orientation="vertical" >
<ListView
android:id="#+id/friendsInGroupListview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:divider="#null" >
</ListView>
</LinearLayout>
Expandable card:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:padding="16dp" >
<FrameLayout
android:id="#+id/activity_expandablelistitem_card_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</FrameLayout>
<FrameLayout
android:id="#+id/activity_expandablelistitem_card_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp" >
</FrameLayout>
</LinearLayout>
I'd never use wrap_content in the layout_height nor layout_width, it causes performance issues, as you may read here. Also setting a static width/height is not a good idea as it may lead to layout issues in several devices.
Said that, I always define my ListViews like this:
<ListView
android:id="#+id/who_list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical">
</ListView>
Setting your layout_weight within a LinearLayout with just this view inside it, you make yourself sure that ListView will have a good layout rendering and it will enable scrolling when needed.

How to create a LinearLayout with TextView and Image on each row to fill up screen height

I am trying to achieve the below layout in my Android Application.
I achieved this layout by using a custom List Adapter. But the problem is with the empty space in the bottom. Is it possible to make the ListView items be evenly spaced to occupy the entire screen height.
One alternative is to use LinearLayout with WeightSum. But with this approach, how will I be able to place textView and the arrow image in the same row.
Any suggestion to achieve this layout and evenly distribute row height would be appreciated.
Thank you in advance.
Main Activity XML:
<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:background="#color/startscreen_background"
tools:context=".StartScreen" >
<ListView
android:id="#+id/start_lv_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:divider="#color/startscreen_listitem_divider"
android:dividerHeight="0px" >
</ListView>
</RelativeLayout>
List Item Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/startscreen_tv_item"
android:layout_width="match_parent"
android:layout_height="#dimen/startscreen_listitem_height"
android:gravity="center_vertical"
android:paddingLeft="#dimen/startscreen_listitem_paddingleft"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/startscreen_listitem_textcolor"
android:textSize="#dimen/startscreen_listitem_textsize" />
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:background="#color/startscreen_listitem_divider" />
</LinearLayout>
You can use android:layout_height="fill_parent" in your Main Activity XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/startscreen_background"
tools:context=".StartScreen" >
<ListView
android:id="#+id/start_lv_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:divider="#color/startscreen_listitem_divider"
android:dividerHeight="0px" >
</ListView>
</RelativeLayout>
Did you tried giving the layout_height="match_parent" to your ListView ?

add view to vertical linearlayout inside horizontallscrollview within scrollview

I want to programatically add viwes to vertical orientation linearlayout within horizontalscrollview within Scrollview. Basic scheme of my layout is belowe:
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true"
android:fadingEdge="none">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- same header code -->
</LinearLaout>
<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
<LinearLayout
android:id="#+id/list_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"/>
</HorizontalScrollView>
</ScrollView>
Now if i'm adding programatically 5 views to list_view linearlayout, i get only one view, and free sapce below it. If i comment HorizontallScrollView like that:
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true"
android:fadingEdge="none">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- same header code -->
</LinearLaout>
<LinearLayout
android:id="#+id/list_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"/>
</ScrollView>
All views are show correct, but they aren't horizontall scrollable. Please give me same ideas how to solve this problem.
Answer is to change linearlayout's android:layout_width attribute to wrap_content within horizontalScrollview

Categories

Resources