1

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.

RBT
  • 24,161
  • 21
  • 159
  • 240
Boris Katic
  • 11
  • 1
  • 1
  • 3
  • Related post - [Changing position of a button](https://stackoverflow.com/q/5646929/465053) – RBT Dec 30 '19 at 04:51

4 Answers4

5

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 .

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
2

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

Garg
  • 2,731
  • 2
  • 36
  • 47
1

Use Linear Layout as parent Layout and following line in button

android:layout_gravity="bottom|right"
Anil
  • 1,087
  • 1
  • 11
  • 24
-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

  • 4
    Please don't post images of code, https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557 – quant Nov 17 '18 at 15:24