Put view between two Views with strict size - android

Is there any way to put my FrameLayout between 1.1 and 1.3.
I tried to use layout_below and layout_above for it, but it doesn't work together.
1.RelativeLayout
1.1RelativeLayout (strict_size)
1.2FrameLayout (match_parent)
1.3RelativeLayout(strict_size)

I would use a LinearLayout. Assuming you are talking about heights:
<LinearLayout
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp">
</LinearLayout>
When using layout_weight like that, LinearLayout will first lay out views with fixed dimensions, and then assign all the remaining space to the layout_weight="1" view.

Related

How to set height of one layout to the height of another layout in Android?

I have this code. So I want to set height in my relativeLayout by height in scrollView. Can I set height by id of scrollView? It's need, because I have many fragments, but one of them should take height of scrollView (when virtual keyboard is open).
...
<ScrollView
android:id="#+id/scrollViewer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<FrameLayout
android:id="#+id/frameViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/layoutViewer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webViewer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</FrameLayout>
</ScrollView>
...
ScrollViews child (and its childrens also) should have set layout_height="wrap_content" for avoiding multiple re-measurements (way better performance)
if you want to fullfil whole ScrollView even when content have less height then use fillViewport xml attribute

ScrollView goes on top of another layout Android

In the following code I've got two main containers, a RelativeLayout and a ScrollView. I need the RelativeLayout to be on top fixed and below that I need the ScrollView with Images to be scrollable. I've got the following two questions:
Though my ScrollView is scrollable but it goes on top of the RelativeLayout. !important
The view of my images present within the vertical LinearLayout are thumbnail sized. If I have 15-20 images, they take a lot of vertical space. How do I have the images to fit the maximum in a row based on the screen size and then go to the next row?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
<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">
<ImageView>
</ImageView>
<ImageView>
</ImageView>
<ImageView>
</ImageView>
<ImageView>
</ImageView>
</LinearLayout>
</ScrollView>
Follow these in simple way:
You need to use a fixed height for the parent, i.e., <RelativeLayout>.
Set the height of the ScrollView to match_parent.
Make sure you put everything inside a LinearLayout.
Finally you would end up with this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</RelativeLayout>
</LinearLayout>
First, you can set your RelativeLayout's height to a fixed value. Then set your ScrollView's height to match_parent, so it takes up all the available space. Then both of these should be contained in a vertical LinearLayout, to avoid any overlapping.

How to Prevent Scrolling within FrameLayouts and Instead Have the Parent LinearLayout Handle all Scrolling?

I have a scrollable LinearLayout parent with a few FrameLayouts nested inside it as follows:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical"
android:orientation="vertical">
<FrameLayout
android:id="#+id/hb_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/gn_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/yt_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I would like FrameLayouts to take up the space they need, allowing for scrolling of the full view through the LinearLayout.
Issue is: the LinearLayout will only take up the screen, it won't scroll, instead the FrameLayout's will scroll if the content overflows the bottom of the screen.
To be clear, I just want the FrameLayouts to fill whatever space they need, and the LinearLayout can scroll it like one long view. Must I fix the FrameLayout heights to achieve this? If so, is there risk of my layout breaking on different screen sizes/densities (or does DP really work in all cases?).
Thank you immensely!
EDIT: I have confirmed that setting fixed heights in the FrameLayout does exactly what I want this to do: scroll as one. However, why doesn't wrap_content measure the height and then go from there? That is what I expected the case was... I'm not certain how to judge the right heights for each element.
Try adding a scrollview to the layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<FrameLayout
android:id="#+id/hb_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/gn_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/yt_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>

Putting two child layout inside a parent layout with equal weight distribution in Android

I want to use two child layout (one linear layout and one relative layout) inside a parent layout (relative layout) in such a way that both of the child layout will take exactly half of the screen and items inside of each child layout will not cause one child layout to get more width than another one!
It is pretty easy, use parameter layout_weight in children of LinearLayout, something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</RelativeLayout>
<RelativeLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</RelativeLayout>
</LinearLayout>
If I understand correctly from your illustration, the red box is a RelativeLayout, whereas the green boxes are a LinearLayout and a RelativeLayout.
A simple solution would be to center an empty View inside the RelativeLayout and align the two child Views against it:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/v_center" />
<View
android:id="#+id/v_center"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/v_center" />
</RelativeLayout>
A nice little bonus here is that you can provide some spacing between the two by specifying the View's dimensions.
Beware, however, that RelativeLayouts aren't very efficient, and nesting them is an especially bad idea. I suggest using the hierarchy viewer tool to inspect the layout timings to make sure it's relatively fast, and to try to avoid nesting the layouts in this fashion.

Layout Weights do not work inside a ScrollView

I want to assign layout weights to several items within a LinearLayout inside of a ScrollView. However, the ScrollView ignores the LinearLayout weightSum.
My goal is to divide the layout with weights of 2, 1, 1 (for a total sum of 4), but this does not work properly inside of a ScrollView.
How can I solve this layout problem?
main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout android:id="#+id/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2"
android:background="#FFFFFF" />
<LinearLayout android:id="#+id/logo1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:background="#000000" />
<LinearLayout android:id="#+id/logobutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#4B4B4B" />
</LinearLayout>
</ScrollView>
I have faced this problem before. Just use android:fillViewport="true" in your ScrollView and it will fill up the screen.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
This won't work as you have done it. The child view of a ScrollView should be set to wrap_content. If you set it to fill_parent, it will fill the area of the ScrollView and never scroll, because it won't be larger than the ScrollView.
The idea of layout_weight is to fill a specific area proportionately.
You should set all of the child LinearLayouts layout_height to either wrap_content or a specific size (in dp) and the parent LinearLayout layout_height to wrap_content
As said you need to remove the additional
xmlns:android="http://schemas.android.com/apk/res/android"
if it's not the root (first) element of the layout.
just put this in your scroll view:
android:fillViewport="true"
In my experience, fillviewport=true works bit strange.
I solved this problem by wrapping LinearLayout with another Layout like FrameLayout.
I hope this helps.
Add below line in your ScrollView it will be work fine.
Only single child must be there for ScrollView
android:fillViewport="true"

Categories

Resources