Android - position layout at bottom of screen always in the foreground - android

My xml file is like following:
<LinearLayout>
<ScrollView>
<LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
First LinearLayout has android:layout_height="match_parent", all others android:layout_height="wrap_content". How to create a layout at bottom of screen always in the foreground with an imageview?

This is best done with a relative layout. Relative layout elements stack on top of each other unless you position them relative to each other. For instance, if you had two image views and did position them, the second image view would be placed on top of the first one.
Here is an example to get you started:
<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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:contentDescription="#string/YOUR_CONTENT_DESC"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher"/>
</RelativeLayout>

you could set the gravity of the linear layout to bottom.
android:layout_gravity="bottom"
When your creating your XML document order does matter. So for example:
<ImageView/>
<LinearLayout/>
The LinearLayout will be placed in front of the ImageView.

Try this way,hope this will help you to solve your problem with another one alternative.
<FrameLayout 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="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:adjustViewBounds="true"
android:layout_gravity="bottom|center_horizontal"/>
</FrameLayout>

Related

Is it possible to make two layouts share one ImageView?

I mean how can i locate a ImageView in two layouts? i have 2 relative layouts one is up, one is down. The up one has a ImageView but i want half of this ImageView located in down layout. How can i do that?
EDIT::
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/activity_horizontal_margin"
android:background="#d2aff4">
<Space
android:id="#+id/center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true" />
<RelativeLayout
android:id="#+id/bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/center"
android:background="#87c96d" />
<RelativeLayout
android:id="#+id/top"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
In this scenario the "top" layout needs to be created at last to be displayed on top of the rest of the views. If you need it to have a background colour, you need to apply the colour to the main container.
Here is the result:
I agree with #Booger answer's but If your parent layout is RelativeLayout then add this below property in you ImageView.
android:layout_centerInParent="true"
Or you can use below code to create that kind of layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/gray_font">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorAccent">
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"
android:layout_centerInParent="true"/>
</RelativeLayout>
Take a look at this library https://github.com/umano/AndroidSlidingUpPanel
if you want something easy you can put those layouts in to a parent relative layout and then you can add a image view in parent relative layout as last child and position it how you need
I think you should wrap both your RelativeLayout in another Parent layout, and in that layout, you will place your ImageView (which will span both the other layouts.
Something like this pseudo-code:
<LinearLayout>
<RelativeLayout1>
<RelativeLayout2>
<ImageView gravity=center> //Order counts, this needs to be after the RelativeLayouts to show on top of them
</LinearLayout>

Stretch and fill middle Layout Android

I have a Main Relative Layout(height,width=fill parent) that has 3 layouts inside it:
1st: Frame Layout(width=fill_parent/height=wrap_content)
2nd: Linear Layout(width=fill_parent/height=wrap_content/layoutbelow 1st)
3rd: Relative Layout(height=wrap_content/width=fill parent/layout_alignParentBottom="true"/ layoutbelow 2nd)
I want the 3rd layout to be at the bottom of the screen and 1st at the top of the screen and 2nd to fill the space in between. How do I do this? I followed this link:Android LinearLayout fill-the-middle and tried setting layout_height="0px" and layout_weight="1" but it did not work. Can anyone please help me with this?
Thanks
IIRC RelativeLayout doesn't support layout_weight.
So you need something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_wight="1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Try this :
1st: Frame Layout(width=fill_parent/height=wrap_content)
2nd: Linear Layout(width=fill_parent/height=wrap_content/layoutbelow 1st/layoutabove 3rd)
3rd: Relative Layout(height=wrap_content/width=fill parent/layout_alignParentBottom="true")
For Example :
<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">
<Button
android:id="#+id/btnButton1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="1"/>
<Button
android:id="#+id/btnButton2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="2"
android:layout_below="#+id/btnButton1"
android:layout_above="#+id/btnButton3"/>
<Button
android:id="#+id/btnButton3"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="3"
android:layout_alignParentBottom="true"/>
</RelativeLayout>

layout with top bar, bottom bar & remaining space content view

I'm getting crazy with the android views. I want a layout with a bar on top (A) and one bar one the bottom (C) of my app with height="wrap_content". The full remaining space in the middle (B) should be the content area with another Layout or TextView or whatever. But i can't get this to work. I tried a lot with the layouts, but when i do android:layout_height="match_parent" to B, C disappears. Any ideas? Thanks in advance
You can use android:layout_weight property to achieve that, but your parent container has to be a LinearLayout, in my example I'm using just LinearLayout as children of the parent view but you can use another type of View:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_a"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout
android:id="#+id/layout_b"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:id="#+id/layout_c"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
If you're curious and wanna know how it works... here is the explanation, good luck :)
Put the contents into a RelativeLayout with alignParentTop and alignParentBottom attributes for content A and C and also below and above related attribute to content B, as follows:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- create content A and C before -->
<LinearLayout
android:id="#+id/A"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" > </LinearLayout>
<LinearLayout
android:id="#+id/C"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" > </LinearLayout>
<!-- create content B regarding the previous ids -->
<LinearLayout
android:id="#+id/B"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/A"
android:layout_above="#id/C"
android:orientation="vertical" > </LinearLayout>
</RelativeLayout>
You can try to change the content B's height with wrap_content instead of match_parent if this doesn't work. Let me know if this helps.
Use weight:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/A"
android:layout_width="match_parent"
android:layout_height="50dp">
<!-- Your view here or replace FrameLayout -->
</FrameLayout>
<FrameLayout
android:id="#+id/B"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
<!-- Your view here or replace FrameLayout -->
</FrameLayout>
<FrameLayout
android:id="#+id/C"
android:layout_width="match_parent"
android:layout_height="50dp">
<!-- Your view here or replace FrameLayout -->
</FrameLayout>
</LinearLayout>
Setting height to 0 is good for performance reasons, since weight will change it anyway.
<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/top_bar"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<TextView
android:id="#+id/middle_area"
android:layout_weight="14"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<TextView
android:id="#+id/bottom_bar"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
android_layout_weight in combination with LinearLayout can do the trick for you. Try the above code and adjust the android_layout_weight according to your need.
In my example the top bar will take 1/16 th of the screen the bottom bar would take 1/16th of the screen and the middle_area would take 14/16 th of the screen. You can change the numbers to your custom needs.

Android ScrollView and buttons at bottom of the screen

I want to implement this: A ScrollView that contains many elements (ImageViews, TextViews, EditTexts etc) and then after the ScrollView some buttons (which are custom ImageViews) that appear always exactly at the bottom of the screen.
If I use the android:fillViewport="true" attribute, then if the elements of the ScrollView are too big to fit in the screen size the buttons get invisible . If I use the android:Weight=1 attribute then the ScrollView gets only 50% of the Screen when the screen is big and it can fit (I want the buttons to take a small percentage, about 10%). If I set the android:Weight to bigger values then the buttons appear very small.
Please help! Maybe it is something simple that I overlooked but I’ve been banging my head for hours!
Just created and tested it. Looks like you want.
<?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">
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Button2"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/buttons">
<!--Scrollable content here-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test text"
android:textSize="40dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
<?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="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hallo Welt"
/>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go next page"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
This worked for me. Give the scroll view a weight of 1. Put all the other widgets following the scroll view in a layout. The scroll view will grow enough to not block the rest.
Widgets in scroll view and rest at bottom
scrollview cannot fit the screen because you put it on a linear layout, so linear layout fit in the screen,
just try to make scrollview as root elemen on xml layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- Here you can put some XML stuff and BOOM! your screen fit to scrollview -->
</LinearLayout>
</ScrollView>
If you do not want to use RelativeLayout, it is better to use LinearLayout. This method is better in my opinion.
Just set the layout_weight to one

Scale background image in LinearLayout

I'm having the following problem;
I have a LinearLayout with in it a ScrollView, in the ScrollView is some custom view. Now I want to put an image in the background of the LinearLayout and keep the image in its original size. But when I set the background property of the LinearLayout it stretches to fit the full screen. How can I make it so that the image keeps its original size?
My xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/some_drawable">
<ScrollView
android:id="#+id/scrollview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:fillViewport="true">
<some.custom.layout
android:id="#+id/mainLayout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</ScrollView>
</LinearLayout>
Code example for Egor's answer:
<FrameLayout
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/background"
/>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</FrameLayout>
Try using FrameLayout with an ImageView and LinearLayout inside. For example, try changing the alpha of the image and move it to foreground in your FrameLayout, thus the LinearLayout stays on background. Hope this helps!
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/layoutLogin"
android:layout_alignLeft="#id/layoutLogin"
android:layout_alignRight="#id/layoutLogin"
android:layout_alignTop="#+id/layoutLogin"
android:adjustViewBounds="true"
android:background="#mipmap/ic_photo_bg"
android:scaleType="fitXY" />
<LinearLayout
android:id="#+id/layoutLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
Alternatively to ColdForged's suggestion, you could move from LinearLayout to RelativeLayout and use ImageView to show the image instead of changing background to it. The views in RelativeLayout can interfere unlike in LinearLayout.
You might take a look at the InsetDrawable. Either that or it would likely require you to subclass your layout to draw the way you wish as the background element doesn't have the ability you need.

Categories

Resources