Specifying the UI with XML and keeping the layout separate from the application code makes for easily maintainable projects and allows us to specify different layouts based on locale, orientation, pixel density, docking status, and other configuration parameters.
Despite the tidiness of this approach there are occasions when we need to manipulate a layout or another view group at runtime from within a Java method. All XML code is interpreted by Java and we have access to ViewGroup parameters through various LayoutParams classes.
Here we will set up a simple layout with XML and use a LinearLayout.LayoutParams object to change the margins of a View during run time.
Start a new Android project and navigate to the main.xml
file in the res/layout
folder.