I want to move a button depending on where the widgets are on the screen
Asked
Active
Viewed 2,138 times
1
-
1What 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 Answers
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