Android - Android UI Components

Up to this point, you have seen that the basic unit of an Android application is an Activity. An Activity displays the user interface of your application, which may contain widgets like buttons, labels, text boxes, etc. Typically, you define your UI using an XML file (for example, the main.xml file located in the res/layout folder), which may look like this:



During runtime, you load the XML UI in the onCreate() event handler in your Activity class, using the setContentView()method of the Activity class:



What happens is that during compilation time, each element in the XML file is compiled into its equivalent Android GUI class, with attributes represented by methods. The Android system then creates the UI of the Activity when it is loaded.
While it is always easier to build your UI using a XML file, there are times where you need to build your UI dynamically during runtime (for example, when writing games). Hence, it is also possible to create your UI entirely using code.
Button using Java code dynamically :
In this section i will tell you how to create a button using java code dynamically


main.xml :
java file :

output:



ViewGroups and Views are covered in next articles.

Comments

Popular posts from this blog

public vs protected vs default access modifiers - Java

Class, Reference and Object