I have a RelativeLayout at the top of my app. Below the RelativeLayout I have a ViewPager.
To explain this in a way that will make sense, imagine the screen's height is 700 pixels. The RelativeLayout is about 200 pixels high.
I want the RelativeLayout to be position absolutely at the top of the app such that the ViewPager is behind it. I then want to add a 200 pixel paddingTop to the ViewPager so that it appears under the RelativeLayout.
Here is the layout I have now (which is obviously not working):
<LinearLayout
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/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
I load a ListView with some data under the header:
<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">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
How can I do this?
Try changing the linearlayout to a framelayout. This will allow the views to be placed on top of each other.
Then put the code for the pageviewer above the relativelayout. This will make the pageviewer appear to be behind the relativelayout as it is rendered first, and the relativelayout rendered on top of it. Android will render views in the order in which they are declared.
Lastly, add a top margin/padding to the pageviewer to make it seem positioned below the relativelayout.
Here's what you can do:
Use RelativeLayout instead of LinearLayout as the root.
Move the ViewPager(pager) view to the top of child RelativeLayout(header)
<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.support.v4.view.ViewPager
android:id="#+id/pager"
android:paddingTop="200px"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</RelativeLayout>
Related
I have a very simple question that for some reason im not being to able to implement.
I have a RecyclerView which is being filled only once upon its activity creation. I want to add another view below it.
If the RecyclerView doesnt fill the entire screen length (usually in portrait mode) the view should be positioned directly below it.
If the RecyclerView exceeds the screen length (usually in landscape mode) the view should still be positioned below it but should also be docked to the screen bottom.
Thats it.
I've tried position them in RelativeLayout, LinearLayout or CoordinatorLayout. With match_parent, wrap_content (with or without setAutoMeasureEnabled). With app:layout_anchor and app:layout_anchorGravity but nothing is working.
Appreciate any help.
This is an example of one of my tries:
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/features_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchor="#id/features_list"
app:layout_anchorGravity="bottom" />
</android.support.design.widget.CoordinatorLayout>
Try this:
Note: I'm using a ScrollView since you said the height of your RecyclerView can exceed the screen size.
<?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">
<!-- Required for the content to not exceed the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:measureAllChildren="true">
<!-- Content -->
</ScrollView>
<!-- View that needs to be under the ScrollView or
clipped to the button of the screen based on the ScrollView height -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I have a question that i couldn't Find Any Answer For it.
How can I Make An Layout Scrolling And Make Another Layout that doesn't Scroll?
On the other hand i want to make a layout Including Some buttons that scrolls if necessary and i want to put Two buttons at the bottom of the screen and i don't want them to disappear when scrolling.
<?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:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.myname.myapp.MainActivity">
// a bunch of Buttons and TextViews
</RelativeLayout>
</scrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
// two Moveless Buttons
</RelativeLayout>
But it doesn't Work.
Your ScrollView has width and height that matches the parent. This means it takes up the entire space available leaving nothing for the RelativeLayout. You probably want to wrap what you already have inside a LinearLayout and then use layout_weight attribute to divide the space up.
I'm not sure I understand what You want to do. But try this and give feedback
( code example not tested in Android Studio)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.myname.myapp.MainActivity">
// a bunch of Buttons an TextViews
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp (for example)>"
android:layout_alignParentBottom="true">
// two Moveless Buttons
</RelativeLayout>
</RelativeLayout>
Make your root a vertical linearlayout with a scrollview, to hold the scrollable views, and a RelativeLayout, to hold your buttons.
You can take advantage of the LinearLayout's weighting ability by applying a weight of 1 to the Scrollview so it takes up as much room it can while the button-holding-RelativeLayout will be the size of your biggest button.
Here is an example:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.myname.myapp.MainActivity">
// a bunch of Buttons an TextViews
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
// two Moveless Buttons
</RelativeLayout>
</LinearLayout>
HTHS!
I am trying to use this code but its not working when i put layout weight.If i remove layout weight and enter height for image view,it works fine.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<ImageView
android:id="#+id/informative_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.7"
android:scaleType="fitXY"></ImageView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_option_menu"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6.3" />
</LinearLayout>
</ScrollView>
Actually you don't need scrollview. Remove it. And if you are using recyclerview inside scrollview then you need to specify height in dp of list to make it work properly.
I'm having weird issues with ScrollView that contains a relativeLayout with topMargin
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:fillViewport="true"
android:focusableInTouchMode="true">
<RelativeLayout
android:id="#+id/cp_editor_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:layout_marginTop="270dp"
>
...
This code example does not work. the scrolling stops after around 20px. If i'm removing the margin_top attribute then the scrolling works as expected.
Thanks for your help
I do not understand the issue with the topMargin stopping the scrolling. However, to achieve the desired margin and maintain the scrolling functionality i can think of two solutions:
1) add an extraneous View that has the same height as the margin you want, and put it above your Relative Layout (cp_editor_layout). It would look like this:
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:fillViewport="true"
android:focusableInTouchMode="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="270dp" />
<RelativeLayout
android:id="#+id/cp_editor_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white" >
</RelativeLayout>
</LinearLayout>
2) give the margin top margin to your scroll view since that space is unnecessary to have in the scroll anyway. If your going for some type of overscroll you need to subclass the ScrollView.
Hope this helps you somehow :)
I have lot of items on the screen and I need to use the scrollbar so the user can scroll down. However, the scroll is either not visible or it's not working. How is it possible to add a scrollbar to a LinearLayout?
Wrap the linear layout with a <ScrollView>
See here for an example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
</LinearLayout>
Note: fill_parent is deprecated and renamed to match_parent in API Level 8
and higher.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
You need to wrap your linear layout with a scroll view
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
This can be done using the tag <ScrollView>. For ScrollView, one thing you have to remind that, ScrollView must have a single child.
If you want your full layout to be scrollable then add <ScrollView> at the top. Check the example given below.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
But if you want some part of your layout to be scrollable then add <ScrollView> within that part. Check the example given below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
</LinearLayout>
Here is how I did it by trial and error.
ScrollView - (the outer wrapper).
LinearLayout (child-1).
LinearLayout (child-1a).
LinearLayout (child-1b).
Since ScrollView can have only one child, that child is a linear layout. Then all the other layout types occur in the first linear layout. I haven't tried to include a relative layout yet, but they drive me nuts so I will wait until my sanity returns.
you need to use the following attribute and enclose it within the linear layout
<LinearLayout ...>
<scrollView ...>
</scrollView>
</LinearLayout>
You need to place ScrollView as the first child of Layout file and now put your linearlayout inside it. Now, android will decide on the basis of content and device size available whether to show a scrollable or not.
Make sure linearlayout has no sibling because ScrollView can not have more than one child.
<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=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<---------Content Here --------------->
</LinearLayout>
</ScrollView>
</LinearLayout>
Whenever you wanted to make a layout scrollable, you can use <ScrollView> With a layout or component in it.
You can add an atrribute in linearLayout : android:scrollbars="vertical"