Android floating buttons over a view - android

I was trying to have a floating button on my view, I googled and found this link which pretty well sums it up.
http://www.jondev.net/articles/Floating_Views_in_Android_(Buttons)
While this is true for one button, but what if i want to have two floating buttons one at "top left" and another at "bottom right".
I thought of having a relative layout which has two buttons with lay out gravities different. Will this work. I tried it and miserably failed. Has any one else tried to do this? Is there any way of doing this, if so please let me know.

You can achive that with a RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:text="Button" />
</RelativeLayout>
Note that the last added Widget is on top.

You can do it with RelativeLayout although you can also achieve that using FrameLayout (like in the example in your the link).
In FrameLayoutSet a proper gravity to the the buttons (Gravity.BOTTOM|Gravity.RIGHT, or via XML...), and in RelativeLayout set the reuiqred rules to the Buttons:
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
etc.

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:text="Button" />
</RelativeLayout>

Related

How do I align the right/left side of a button in android to the center of the screen?

I was thinking it would be something like android:layout_startOf"centerHoriztonal" or something like that but I have searched and couldn't find anything. If it helps I am trying to make a soundboard app where all the buttons can be the same size across multiple device screens.
try this xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:text="Here is button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
/>
<Button
android:id="#+id/button2"
android:text="Here is button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
You can wrap the button(s) inside a horizontal LinearLayout and use layout_weight to divide the screen in half and then layout_alignParentLeft/layout_alignParentRight to align the buttons inside a RelativeLayout that takes up exactly half the screen width.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button Text"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
</LinearLayout>
Another way to do it is to create a dummy view object that is centered horizontally in the parent view, and align your buttons to the left or right of it:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/dummyview"
android:layout_width="0px"
android:layout_height="0px"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/dummyview"
android:text="Button Text"/>
</RelativeLayout>
Are you sure you want the same size of buttons in all devices? People try to avoid it and make make view elements flexible.
Anyways if you want to set same size just directly write in layout
android:layout_width = "#dimen/valueForButton"; //some absolute value
For starting at same point, just add all buttons inside LinearLayout and make some operations there, such as
android:layout_gravity = "center_horizontal";
Your layout must be something like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="#dimen/valueForButton"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="#dimen/valueForButton"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="#dimen/valueForButton"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

button:gravity="right" is not working

i am desinging a xml
<?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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:background="#CCCCCC"
android:text="Cancel" />
<Button
android:id="#+id/btnadd"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:gravity="right"
android:background="#3399FF"
android:text="Add" />
</LinearLayout>
</LinearLayout>
for button "Add" android:gravity=right is not working.i have give a layout:margin,then only its moving right.can anyone help me out.
It's a little unclear what you want, but do notice the following:
gravity affects the contents of the widget i.e. your button text.
layout_gravity affects positioning of the widget in the parent layout
So if you want the button to be on right, change gravity to layout_gravity.
Use RelativeLayout instead of LinearLayout.
and use android:layout_alignParentRight="true" for btnadd.
EDIT :
Code :
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:background="#CCCCCC"
android:text="Cancel" />
<Button
android:id="#+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:background="#3399FF"
android:text="Add" />
</RelativeLayout>
Change the LinearLayout to be as RelativeLayout
and instead of android:gravity="right" use android:layout_alignParentRight="true"
It is because you use LinearLayout (orientation horizontal)
Try to use relative layout
you are using linear layout in linear layout your layout depends on orientation like you are using vertical:
use relative layout instead to solve your problem,and give margins you want
also to align your button right use:
android:layout_alignParentRight="true"
your problem would definetly be solved with this.
it was because of you are using android:orientation="horizontal"in Linear Layout
use this android:orientation="vertical"
or use this i modified your code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#CCCCCC"
android:text="Cancel" />
<Button
android:id="#+id/btnadd"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#3399FF"
android:text="Add" />
</RelativeLayout>
If you want to achieve something like this:
You can do that either with RelativeLayout or LinearLayout.
For LinearLayout you explicitely add space between the buttons. This is because the android:layout_gravitypositions the view only within the space, that LinearLayout has given. And this space is not the LinearLayout itself, but the sum of all views, that were layed out linearly (as the name says).
Here's my way of putting the two buttons:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<ImageView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#android:color/transparent" />
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
I always prefer LinearLayout over RelativeLayout as I experienced LinearLayout as more robust, especially on Android's 2.x versions.
BTW: The difference between android:layout_gravity and android:gravity:
The layout_gravity tells the parent layout of a view to position the whole view within the bounds that the layout defines. In case of LinearLayout this is the space that was allocated by linearly putting the views one after the other.
The gravity tells the view to position its foreground within the available space. You might have seen, that your Add-Button has shown the text on the right side.

Use in android:layout_weight buttons Android

I try to put buttons on screen. I want that this screens be split in same space on whole screen. I read a lot of the answers about use in android:layout_weight but i didn't get success.
code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/p_data_button"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#drawable/p_selector"
/>
<Button
android:id="#+id/p1_data_button"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#drawable/p1_selector"
/>
<Button
android:id="#+id/p2_data_button"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#drawable/p2_selector"
/>
<Button
android:id="#+id/p3_data_button"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#drawable/p3_selector"/>
</LinearLayout>
EDIT:
This background work with selector. it is affected?
I add Answer.
Here's my code which split screen textviews in 5 parts on screen.. in your code you havent provided android:weightSum="5" property..
<LinearLayout
android:id="#+id/l"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<TextView
android:id="#+id/txtUHID"
style="#style/pateint_context_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="UHID: " />
<TextView
android:id="#+id/txtBedID"
style="#style/pateint_context_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Bed ID: "
android:visibility="invisible" />
<TextView
android:id="#+id/txtDOB"
style="#style/pateint_context_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="DOB: " />
<TextView
android:id="#+id/txtSex"
style="#style/pateint_context_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Sex: " />
<TextView
android:id="#+id/txtAge"
style="#style/pateint_context_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Age: " />
</LinearLayout>
Use android:layout_height="match_parent" if you are using weights and you want for the elements to cover the entire space.
As the Button are of size "wrap_content". They aren't showing.
Just place some text android:text="test" in the all button. It would work. Else use image in background selector.
Your xml code is absolutely fine, and it should display what you want it to display.
There must be something wrong with the drawable background you have added to all the buttons. I suggest that you remove the drawable and apply some basic color like
android:background="#color/black"
to make sure that it's problem with the drawable. Good luck!
I found the problem . You all right. The code work fine. In the old version of this application they do function that in some case put all content in relative layout. I understood that when content is in relative layout its need to be put on linear layout. but it's Ddon't working..
I convinced after i replace this function that this was what cause to this problem.

how to do this for all emulators

<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="365dp"
android:layout_margin="10dp"
android:background="#drawable/round_border" >
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/contactimg"
style="#style/CodeFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#drawable/round_border_imageview"
android:maxHeight="75dp"
android:maxWidth="75dp"
android:padding="2dp"
android:src="#drawable/icon"
android:textSize="15dp" >
</ImageButton>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/relativeLayout2">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="20dp"
android:weightSum="1"
android:background="#drawable/round_border">
<TextView
android:id="#+id/TextView03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Textttt"
android:padding="10dp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="1dp"
android:layout_below="#+id/relativeLayout1"
>
<Button
android:id="#+id/replaymassegebutton"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:background="#drawable/button_indicator"
android:text="Reply"
android:textColor="#color/gray2" >
</Button>
</RelativeLayout>
In HVGA emulator it'is worded fine. but in another emulator it is not. As you can see I put height=365dp,(I have to change height but I don't know how, 365dp works only in HVGA mode) what should I do here so it work for all emulator. I tried wrap_content and fill_parrent but when I set this paramteters the buttons gone (which are in another layout). I also tried this way android:layout_weight="1" android:layout_height="0dp". but no result here. Could you give me another solution
Better have parent layout as relative.
there you can put buttons always at bottom- by setting android:layout_alignParentBottom="true" in your property of button [or buttons manager in case there are more than one buttons]...
For the remaining screen you may have to use LinearLayout there, use it as an immidiate child and arrange your original view there.... make it android:layout_alignParentTop="true" and put your buttons view below it... by adding this android:layout_below= to button view.. this will make your linear layout to acquire all remaining space that has not taken by buttons.... add scroll view as immediate child of linear layout in case of smaller screens...
and you have least worries about multiple screens....
Enjoy

How to place a button at a position little above the bottom in android

I need to place a button at the bottom of screen, leaving some space below it.
In other words I am using these two attributes for my button -
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
but the margin attribute wont work when I use alignParentBottom and the button sticks at the bottom only. Please tell me how to place it a little above the bottom .
Here's the xml
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#drawable/board"
android:layout_marginLeft="10dp"
android:layout_marginRight="60dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Type of Lesion"
android:textColor="#8B0A50"
android:layout_marginTop="40dp"
android:id="#+id/disease_listheader_textview"
android:drawableBottom="#drawable/underline"/>
<ListView
android:id="#+id/disease_listview"
android:layout_below="#id/disease_listheader_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#drawable/divider"
android:layout_marginLeft="40dp"
android:layout_marginBottom="50dp"
android:cacheColorHint="#00000000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/disease_search_button"
android:layout_centerHorizontal="true"
android:background="#drawable/disease_search_btn"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"/>
</RelativeLayout>
just make change in relative layout height attribute value to fillparent like
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
.................../>
hope this help you
Can you try this?
Replace the Button tag with the one mentioned below. I am trying to wrap the button in another dummy layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:paddingBottom="20dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/disease_search_button"
android:background="#drawable/disease_search_btn"
android:layout_marginBottom="20dp"/>
</LinearLayout>
This runs just the way you wanted
<RelativeLayout android:id="#+id/relativeLayout1" android:layout_width="match_parent" android:layout_height="fill_parent">
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="20dip"></Button>
</RelativeLayout>

Categories

Resources