I need to know how to put two overlapped navigation buttons on ImageView. To make clear, I want to previous and next navigation buttons in ImageView.
Use this layout to wrap your image with two buttons above it on the top left corner.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="#drawable/ic_launcher"
android:layout_alignTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignBottom="true"/>
<Button
android:id="#+id/buttonLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<Button
android:id="#+id/buttonRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#id/bbuttonLeft"/>
</RelativeLayout>
why using ImageView you can use a Layout and set it's background. and then put the buttons as you wish.
if you insist on using and ImageView
<RelativeLayout
android:layout_height="..."
android:layout_width="...">
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<!-- put the buttons in here as you like -->
</RelativeLayout>
Wrap the ImageView into RelativeLayout and use `android:layout_alignTop="#+id/imageView".
Related
Currently my code places a button at the bottom of the screen:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|bottom">
<ImageButton
android:id="#+id/button1"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_centerInParent="true"
android:gravity="center"
android:src="#drawable/main_button"
/>
</LinearLayout>
</RelativeLayout>
However, I wish to align it slightly below the bottom of the screen, so a small part of the button is clipped off (Say, 20%). Ultimately, my end result is that when I tap and drag the button up, it will move up and reveal the entire button, but before that, the bottom part of it is clipped.
There are many questions on how to align a button to the bottom of a screen, but I can't seem to find any that aligns it to below the bottom of the screen.
Is there any way to do so, or am I not using the correct search terms?
EDIT: From the answers, I tried android:translationY="20dp" and it did not work, but I tried android:layout_marginBottom="-20dp" and it worked for me.
You can add android:translationY="Xdp" to your button to move it down by X dp.
Alternatively you can create two LinearLayouts. One to fill the screen and hold other components and another that contains the button. You can then add android:layout_below="#id/layout0" and android:margin_top="Xdp" to control how far below the screen the layout with the button should be.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
</LinearLayout android:id="#+id/layout0"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_below="#id/layout0"
android:margin_top="Xdp"
android:gravity="center|bottom">
<ImageButton
android:id="#+id/button1"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_centerInParent="true"
android:gravity="center"
android:src="#drawable/main_button"/>
</LinearLayout>
</RelativeLayout>
You can translate the button:
android:translationY="20dp"
in your xml, that's moves the view 20 dp's down.
I'm currently trying to position some LinearLayouts (which are then filled with some images) on an absolute position on the screen. Currently the LinearLayouts are alinged to the top-left of the parent RelativeLayout and then positioned using margins. This works properly for all child LinearLayouts EXCEPT the first one in the list. Setting some margins (as for example 25dp for the left margin) does not show any effect in this case. I hope someone has had the same issue and can provide some guidance.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_page"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/ll_first" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="25dp" android:layout_marginTop="20dp"></LinearLayout>
<LinearLayout android:id="#+id/ll_second" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="175dp" android:layout_marginTop="30dp"></LinearLayout>
<LinearLayout android:id="#+id/ll_third" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="40dp" android:layout_marginTop="140dp"></LinearLayout>
</RelativeLayout>
Edit:
Example image http://i.stack.imgur.com/HuYjU.jpg
The RelativeLayout provides some tags for the positioning of Views/ViewGroups relative to another View/ViewGroup.
android:layout_below="id" and android:layout_above="id"
This will put the View below/above another View with the id.
android:layout_alignRightOf="id" and android:layout_alignLeftOf="id"
This will put the View to the right/left of another View with the id.
All tags have the same usability like this, where I use the android:layout_below="" to set a View/ViewGroup below another:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_page"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<--! YOUR LAYOUT AT THE TOP OF YOUR PARENT -->
<LinearLayout
android:id="#+id/ll_first"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"></LinearLayout>
<LinearLayout android:id="#+id/ll_second"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#id/ll_first" ></LinearLayout>
<LinearLayout
android:id="#+id/ll_third"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#id/ll_second"></LinearLayout>
</RelativeLayout>
Now you can use some margins for the position of every View.
I have a ScrollView with some elements.
My problem is that i can't set textview's above ImageView's with dynamical height.
I have 3 ImageView with different height, they can be visible or gone.
Above them i need to put text with background color, this text need to be at the bottom part of the ImageView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/pic"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/third_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="8dp"
android:layout_alignParentBottom="true"
android:layout_above="#id/third_picture"
android:gravity="center_horizontal"
android:background="#android:color/white"
android:text="example text">
</TextView>
I tried a lot, but my View was always broken.
Please help, i need some proffesional advise.
You can do this in textview:
android:layout_alignBaseline="#id/pic"
This will align the baseline of textview with basline of imageview
You can set your picture as a background for a layout (e.g linear layout ) rather than using an ImageView and then put the textview inside it and place it where ever you want.
<LinearLayout
android:background="your picture" >
<!-- align it to the bottom -->
<TextView
/>
</LinearLayout>
I have a layout like this
Button
MapView
Button
I want the buttons to be a fixed size & anchored to the top and bottom of the screen respectively. I want the mapview to be in the middle & fill up the space.
What's the best way to achieve this?
Thanks
Use LinearLayout with vertical orientation and set Mapview's weight as 1
Something like this should do the trick:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<WhateveerMapLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/button"
android:layout_above="#+id/button2"
/>
<Button
android:id="#id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
I am using following code to display button at the bottom of activity.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
>
<Button android:id="#+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
/>
</RelativeLayout>
and listview above it. when i display more data in listview this button pannel is moved down.can any one guide me how can i fix it at the bottom of activity?
any help would be appreciated.
The answer selected as correct is faulty, the button will hide the lower part of the list view. The correct way is to declare the button first and position the list above the button.
<Button android:id="#+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
android:layout_alignParentBottom="true"/>
<ListView
...
android:layout_above="#id/btnGetMoreResults"/>
The android:layout_alignParentBottom attribute has to be declared in an element of the RelativeLayout not in the RelativeLayout himself (unless there is another RelativeLayout as a parent).
You should do something like this, with the ListView inside the RelativeLayout also :
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<ListView ...>
<Button android:id="#+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
android:layout_alignParentBottom="true" />
</RelativeLayout>
If you had, for example, all the scrollable elements in a ScrollView, you should do like the following:
<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"
style="#style/rootElement"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- texts, buttons, images and anything that you want to scroll -->
</ScrollView>
<LinearLayout
android:orientation="vertical"
style="#style/footer"
android:id="#+id/footer"
android:layout_alignParentBottom="true"
>
</LinearLayout>
</RelativeLayout>
Note that if you want the footer to be fixed, then you shouldn't put it in the ScrollView, where the scrollable content will be placed. Make it child of RelativeLayout and set layout_alignParentBottom to true. Maybe you'll need to add a padding at the bottom of the ScrollView in this case (so that the last element do not get hidden by the footer).
The idea is similar for elements other than ScrollView