Boris Katic
Boris Katic

Reputation: 11

Android Studio button positioning

I'm trying to set button position to the bottom right corner of the screen. I've tried with this:

button.setX(maxX);
button.setY(maxY);

but the button is invisible because it's off the screen.

EDIT: To clarify. I need to find a way to keep my button WITHIN layout when I set its position to maxX and maxY. To prevent it from going out of bounds. So even if I set its position to something like:

button.setX(maxX - 10);
button.setY(maxY - 10);

it wouldn't stick half out of the screen.

Upvotes: 1

Views: 67951

Answers (4)

Crazy drinks
Crazy drinks

Reputation: 1

The Simple Code of Button to move its position is given below. You can actually change the dp size of the layout yourself. This is Very Simple Code To Move The direction of the button

Image Shows The Proof

Upvotes: -1

Garg
Garg

Reputation: 2731

Simply try this:

<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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="20dp"
    android:layout_marginRight="20dp"
    android:text="Bottom Right Button" />

Hope this work for you

Upvotes: 2

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75778

Please check this SO Answer Changing position of a button.

Alternative Way, You can add this in your button(RelativeLayout)

android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"

Sample Demo

<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" 
>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#AF3800"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"

     />

android:layout_alignParentBottom If true, makes the bottom edge of this view match the bottom edge of the parent. Accommodates bottom margin.

android:layout_alignParentRight If true, makes the right edge of this view match the right edge of the parent. Accommodates right margin.

Both are boolean value, either "true" or "false".

I hope it will helps you .

Upvotes: 5

Anil
Anil

Reputation: 1087

Use Linear Layout as parent Layout and following line in button

android:layout_gravity="bottom|right"

Upvotes: 1

Related Questions