How can I change the position of view through code? Like changing its X, Y position. Is it possible?
This example demonstrates How to Dynamically Add Views into View. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.
Use RelativeLayout instead of LinearLayout . There are also many other layouts you can try. Check here for the other type of available layouts. RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID).
For anything below Honeycomb (API Level 11) you'll have to use setLayoutParams(...)
.
If you can limit your support to Honeycomb and up you can use the setX(...)
, setY(...)
, setLeft(...)
, setTop(...)
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With