0% found this document useful (0 votes)
9 views16 pages

8-Dynamic UI

The document discusses modifying an Android activity's onCreate() method to programmatically add views without using a layout resource file. It describes setting the background color of a RelativeLayout and text of a Button view, creating LayoutParams to center the Button vertically and horizontally within the RelativeLayout, and passing the LayoutParams when adding the Button as a child view.

Uploaded by

kim moon light
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

8-Dynamic UI

The document discusses modifying an Android activity's onCreate() method to programmatically add views without using a layout resource file. It describes setting the background color of a RelativeLayout and text of a Button view, creating LayoutParams to center the Button vertically and horizontally within the RelativeLayout, and passing the LayoutParams when adding the Button as a child view.

Uploaded by

kim moon light
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Creating the Example Project in Android

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.

• Last step, need to pass the LayoutParams object through as an


argument when the child view is added to the parent.

or:
Complete code
Output

You might also like