Android ScrollView doesnt scroll - android

In the code below, the total weight in the parent linear layout is less than the total weight of the its children.So I expect the scroll to work.The first children covers the entire screen.The other child is below the screen, but no scroll.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fillViewport="true"
tools:context=".ActivityHome">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical"
android:weightSum="4">
<!--First children-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="13"
android:orientation="vertical">
</LinearLayout>
<!--Second children-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</ScrollView>
I just have to work with layout_height.How can I do that?

The Problem here is android:weightSum="4" . This is clearly opposite What ScrollView is build for . Remove Weight from layout If View goes out of the screen then it will automatically scroll . Try this for instance :-
<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"
android:fillViewport="true"
tools:context=".ActivityHome">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical"
>
<!--First children-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#android:color/black"
android:orientation="vertical">
</LinearLayout>
<!--Second children-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#android:color/holo_red_light"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</ScrollView>
See This Thread if you want to use android:fillViewport.

Related

ScrollView not working in a fragment

i want to make layout scrollable with scrollview but its not working at all.tried to change scrollview with NestedScrollView but the problem still same. this layout used in fragment. put LinearLayout inside the scrollview as a view. can anyone help me to correct my code if i am doing wrong ? below is my xml code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#android:color/white"
android:orientation="vertical"
tools:context=".ui.jobCalendar.JobCalendarFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.applandeo.materialcalendarview.CalendarView
android:id="#+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:headerColor="#color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_calendar"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
<TextView
android:padding="10dp"
style="#style/TextContent.Black"
android:layout_marginBottom="8dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="Schedule"
android:textColor="#color/white"
android:textSize="16sp" />
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/listJob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
tools:listitem="#layout/job_row">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
thanks in advance
Do this
1) Change root layout to RelativeLayout
2) Set height of ScrollView to wrap_content
3) Change height of ScrollView child which is LinearLayout to
match_parent
4)add this line to Host activity of that fragment
<activity
android:name="Activity_name"
android:windowSoftInputMode="stateHidden|adjustResize" />
Finally the xml looks like:-
<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:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:fillViewport="true"
android:orientation="vertical"
tools:context=".ui.jobCalendar.JobCalendarFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:fillViewport="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.applandeo.materialcalendarview.CalendarView
android:id="#+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:headerColor="#color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_calendar"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
<TextView
style="#style/TextContent.Black"
android:layout_marginBottom="8dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:padding="10dp"
android:text="Schedule"
android:textColor="#color/white"
android:textSize="16sp" />
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/listJob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
tools:listitem="#layout/job_row">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I think you need to get rid of those settings inside the ScrollView attribute.
Basically short it down to
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
Put the rest of the settings inside a RelativeLayout that is included inside the ScrollView like this:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
======== put all the rest of your settings from scrollvide here =====>
<CONTENT> add here the rest of your code </CONTENT>
</RelativeLayout>
</Scrollview>

android: ScrollView parent can't resolve the weight

I have a weird issue. As you can see in my code below, for hor_LnI have set the android:layout_weight to 50, therefore it should fits 50% of height of the screen to fill button's height also, however it does not.
Although if I change the ScrollView to LinearLayout as parent layout, it seems original and correct.
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:id="#+id/vertical_Ln"
android:layout_width="match_parent"
android:orientation="vertical"
android:weightSum="100"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/hor_Ln"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:weightSum="10"
android:layout_weight="50">
<Button
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Here is the output:
What is the problem?
You have to include property android:fillViewport="true" to make your ScrollView full screen.
In xml set android:fillViewport="true"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/vertical_Ln"
android:layout_width="match_parent"
android:orientation="vertical"
android:weightSum="100"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/hor_Ln"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:weightSum="10"
android:layout_weight="50">
<Button
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</ScrollView>

How to display the listview at the bottom of my activity ? There are 3 linear layouts above the listview

i am dividing my android screen into four vertical parts and i want to display the listview at the bottom of my screen, but whenever i placed it at bottom it wont aligned to the bottom. Even after i stretched it to the bottom it takes full screen. how to do that ?
You can display listview at the bottom by the following desing.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--First Part-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--Second Part-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--Third Part-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--Fourth Part-->
<ListView
android:id="#+id/xListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
this may help you
<?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:weightSum="4"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
//first view....
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
//second view....
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
//third view....
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
//List view goes here....
</LinearLayout>
If the listview which you want at bottom always need to be seen then you should use rlativelayout. It will help you to place it properly at bottom by using this attribute android:layout_alignParentBottom="true" in your listview.
Linear layout with vertical orientation will be more helpful instead of
relative 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="wrap_content"
android:weightSum="4"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<ListView
android:id="#+id/mListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

ListView takes up all space in LinearLayout

I have this LinearLayout containing a ListView and a LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<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">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:hint="Hej"/>
</LinearLayout>
</LinearLayout>
The ListView takes up the entire layout and the LinearLayout with EditText gets added below the bottom edge of the screen and isn't visible. How can I fix this? I tried with layout_weight but didn't work.
That is because you are using fill_parent, and it will do exactly that.
You could try something along the lines of this... It will cause the ListView to expand to fill the space.
<?xml version="1.0" encoding="utf-8"?>
<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">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Hej"/>
</LinearLayout>
</LinearLayout>
Another alternative is to use a RelativeLayout. So long as the height of the ListView is not wrap_content, it should be OK.
<?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:orientation="vertical">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_above="#+id/footer"
android:layout_height="match_parent"/>
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Hej"/>
</LinearLayout>
</RelativeLayout>

Add a linear layout to the view after a ExpandableListView without being covered by the ExpandableListView when populated?

What happens is when I populate my list view the linear layout below the ExpandableListView will disappear. I want the "#+id/bottomActionBarAppointments" layout to be visible always on the screen. I do not want to set a static height to the ExpandableListView.
Thanks in advance for any help.Following is my layout xml:
<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"
tools:context=".AppointmentsActivity" >
<LinearLayout
android:id="#+id/calendar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<ExpandableListView
android:id="#+id/expandableListViewAppointments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="#null" >
</ExpandableListView>
<LinearLayout
android:id="#+id/bottomActionBarAppointments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
Found the answer after trying few changes.
I used layout_weight property to allow each linear layout to share a specific portion of the screen.
Following is my layout XML:
<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"
tools:context=".AppointmentsActivity" >
<LinearLayout
android:id="#+id/calendar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/green"
android:layout_weight="0.1" >
</LinearLayout>
<LinearLayout
android:id="#+id/listViewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8">
<ExpandableListView
android:id="#+id/expandableListViewAppointments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="#null" >
</ExpandableListView>
</LinearLayout>
<LinearLayout
android:id="#+id/bottomActionBarAppointments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/green"
android:layout_weight="0.1">
</LinearLayout>
</LinearLayout>

Categories

Resources