8-Dynamic UI
8-Dynamic UI
Studio
Adding Views to an Activity
• The onCreate() method is currently designed to use a resource layout
file for the user interface. Begin, therefore, by deleting this line from
the method:
• The next modification to the onCreate() method is to write some Java
code to add a RelativeLayout object with a single Button view child to
the activity.
Setting View Properties
• Set the background of the RelativeLayout view to be blue and the
Button view to display text that reads “Press Me”.
Adding Layout Parameters and Rules
• In order to instruct the layout view to place the button in a different
location, in this case centered both horizontally and vertically, it will
be necessary to create a LayoutParams object and initialize it with the
appropriate values.
• The next step is to add some additional rules to the parameters to
instruct the layout parent to center the button vertically and
horizontally.
or:
Complete code
Output