1

I want to move a button depending on where the widgets are on the screen

  • 1
    What you want to do is described here http://stackoverflow.com/questions/5646929/changing-position-of-a-button. But you may want to consider other options like a dynamic relative layout. – bbill Jul 02 '15 at 21:03

2 Answers2

0

You can move a button by using the

button.setY(Float)
button.setX(Float)

For example, on your Activity, you can do something like this. (This is done using Kotlin).

import android.os.Bundle
import android.widget.Button


class MainActivity : AppCompatActivity() {
    private lateinit var button: Button // Call it up here

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_post)

        button = findViewById(R.id.ButtonsId) // Choose which button from your xml file. 

    }

    //Whichever your action function is {
            button.setX(225F) // This will move the button horizontally. 
            button.setY(225F) // This will move the button vertically once the action is performed.
        }
    }
}
Adan Vivero
  • 422
  • 12
  • 36
-1

Use translationX and translationY - View.setTranslationX(float)

Dimitar Genov
  • 2,056
  • 13
  • 11