How to fix ListView inside ScrollView? - android

I need to put a ListView inside ScrollBar. I know there is other question about this theme but the answers are very complex and bad.
The ListView doesn't show all its elements, only the first one.
The main structure of my relevant code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
....MORE ELEMENTS....
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
</ScrollView>
</RelativeLayout>

Put your ListView in a separate LinearLayout parent or you can add it in a separate .xml file according to your choice and add
android:fillViewport="true"
in your scrollview, like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!....MORE ELEMENTS....>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</ScrollView>

Related

How to Set a Button at a fixed position even when respective Activity has ScrollView

Is there a way to use some non scrollable view when the activity has ScrollView component?
I want that my button behaves similar to the button in the question below, I want it fixed on the bottom of the UI too:
how to set a button at a fixed location on the bottom of UI?
Here's my layout code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent ">
<FrameLayout
android:id="#+id/map_frameview"
android:layout_width="match_parent"
android:layout_height="200dp">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp">
</LinearLayout>
</LinearLayout>
</ScrollView>
Thanks in advance!
The link you have attached in your question shows how you can do it. You can use RelativeLayout for this. The idea is, you need to tell a button to stay on bottom of the screen & tell the scrollview to stay above the Button along with it's scrollable content like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnMy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/btnMy">
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/btnMy"
android:orientation="vertical">
<FrameLayout
android:id="#+id/map_frameview"
android:layout_width="match_parent"
android:layout_height="200dp">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp">
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>

ListView not utilizing full space in FrameLayout

I have an activity layout with 2 FrameLayout:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.sioned.multivoc.AddWordActivity"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<include layout="#layout/toolbar_layout"/>
<ScrollView
android:id="#+id/scLeft"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/paneLeft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/paneLeftTop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/paneLeftBottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/paneLeftTop"/>
</RelativeLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
The 2nd FrameLayout paneLeftBottom will at some point contain the following fragment:
<RelativeLayout
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"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/lyTop"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...some static fields...
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/lyTop">
<ListView
android:id="#+id/lvResult"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"/>
</RelativeLayout>
In this configuration the ListView is only shown with a height of one of it's rows instead several rows utilizing the available space. The ListView works fine but only one row at a time is displayed.
The same ListView fragment works fine when I put it into a 2-pane landscape layout. How can I make the ListView use the available space ?
Edit:
Found a partial solution by switching to a LinearLayout within the ScrollView. Only problem left is, that the bottom Framelayout overlays a bit of the top FrameLayout when I load the said fragment into it. But that is acceptable in this case as it is only temporary to select some data.
<ScrollView
android:id="#+id/scLeft"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:id="#+id/paneLeft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/paneLeftTop"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/paneLeftBottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</ScrollView>
This right here
android:layout_width="match_parent"
android:layout_height="match_parent"
Should be
android:layout_width="match_parent"
android:layout_height="wrap_content"

Android: How to Display included layout and listview in one view together?

I would like to ask how can i display included layout and listview positioned under the included in onde view.
Layout:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.TransactionsActivity">
<include
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentTop="true"
layout="#layout/preloader" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list"
/>
</android.support.v4.widget.SwipeRefreshLayout>
In the preview is displayed only the included layout, but lisview below is not displayed.
Many thanks for any advice.
Since SwipeRefreshLayout accept only one child. Wrap it inside LinearLayout or RelativeLayout.
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.TransactionsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="#layout/preloader"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentTop="true" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Best Of Luck.

the RecyclerView I am injecting does not appear?

Here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="10dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/content_main">
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"></include>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v7.widget.RecyclerView
android:id="#+id/threadRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
</LinearLayout>
The main reason I did this design was to try to display the
<android.support.v7.widget.RecyclerView
android:id="#+id/threadRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
properly. However, when it is not being displayed at all. Before adding the two relativelayouts, I have one relative layout and the recyclerview did display, but it match_parent for every item of the list and was not able to fix it. How can I display my view properly?
The problem probably has to do with the layout_height="wrap_content" on your RecyclerView. You need to use setAutoMeasureEnabled(true) for wrap_content to work properly with a RecyclerView.
Here's an example:
RecyclerView threadRecyclerView = (RecyclerView)findViewById(R.id.threadRecyclerView);
threadRecyclerView.getLayoutManager().setAutoMeasureEnabled(true);
And your your XML should look something like this:
<android.support.v7.widget.RecyclerView
android:id="#+id/threadRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager"/>
Hope that helps. :)
Note that setAutoMeasureEnabled only works properly for Android Support Library +23.2

Android layout_above does not work on API 21

I have a RelativeLayout that contains 3 children (LinearLayout) :
one at the top.
one at the bottom.
and one between the 2 last views.
The code i use for it is :
<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/white"
tools:context="fr.caudoux.onlinegames.activities.GameMainFragment">
<LinearLayout
android:orientation="vertical"
android:background="#ff44f0c3"
android:layout_alignParentTop="true"
android:id="#+id/game_main_actionbar"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
<LinearLayout
android:background="#F01234"
android:orientation="vertical"
android:id="#+id/game_main_top_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/game_main_actionbar"
android:layout_above="#+id/game_main_chat_container">
</LinearLayout>
<LinearLayout
android:background="#ff2c3ff0"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:id="#id/game_main_chat_container"
android:layout_marginBottom="70dp"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
</RelativeLayout>
Since the api 21 the attribut 'android:layout_above' is not working : the second LinearLayout take all the space below the first one, and the third LinearLayout is over the second.
On api lower than 21 it works : the second part take only the space between the first and the third Linearlayout.
Does anyone known why it is not working and hos to do ?
Thanks
Change the ordering to have your top and bottom view declared before the view that is in between the two:
<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/white"
tools:context="fr.caudoux.onlinegames.activities.GameMainFragment">
<LinearLayout
android:orientation="vertical"
android:background="#ff44f0c3"
android:layout_alignParentTop="true"
android:id="#+id/game_main_actionbar"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
<LinearLayout
android:background="#ff2c3ff0"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:id="#id/game_main_chat_container"
android:layout_marginBottom="70dp"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
<LinearLayout
android:background="#F01234"
android:orientation="vertical"
android:id="#+id/game_main_top_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/game_main_actionbar"
android:layout_above="#+id/game_main_chat_container">
</LinearLayout>
</RelativeLayout>
As mentioned, this same layout is easier to do as a LinearLayout, which specializes in stacked elements:
<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:background="#color/white"
android:orientation="vertical"
tools:context="fr.caudoux.onlinegames.activities.GameMainFragment">
<LinearLayout
android:orientation="vertical"
android:background="#ff44f0c3"
android:id="#+id/game_main_actionbar"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
<LinearLayout
android:background="#F01234"
android:orientation="vertical"
android:id="#+id/game_main_top_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:background="#ff2c3ff0"
android:orientation="vertical"
android:id="#id/game_main_chat_container"
android:layout_marginBottom="70dp"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
</LinearLayout>
Note how only the middle element (the element that needs to fill the remaining space) has a layout_weight on it. This ensures that all remaining space is allocated to that view while the other views are of fixed height.
You cannot have both above and below set in one view. Use below on second view and then below again on last view.

Categories

Resources