- What are the steps to adding a widget dynamically to a linear layout?
Ans: (a) Create a new widget by calling the constructor like this:
Button button = new Button(this); // this is the context.
(b) Configure the widget, for example:
button.setText("Push Me");
(c) Add the widget to the layout:
layout.addView(button);
- Look up methods from the Canvas class in the Android Developer class docmentation,
found at
developer.android.com.
Select More >> Develop >> Android Platform (in Libraries)
The package name is
android.graphics. The Canvas class contains methods for drawing
shapes. Here are some of its methods to check out:
drawCircle drawOval drawLine drawPath
drawPoint drawPoints drawRect
- Try out the TouchEvent1 Example.
- Write an Android app that draws a circle in a MyView widget when a touch up event occurs.
Hint: combine the Drawing0 and TouchEvent1 examples.
- Repeatedly draw circles with touch up events. Keep track of the locations of the
circles in Point objects stored in an arraylist. Here is
the UML diagram for the Point class:
+---------------------------+
| Point |
+---------------------------+
| - x: int |
| - y: int |
+---------------------------+
| + Point(x : int, y : int) |
| + getX( ) : int |
| + getY( ) : int |
| + toString( ) : String |
+---------------------------+
- To the app in Exercise 5, add a reset button to erase all circles and start over.
Ans:
activity_main.xml
Point.java
MyView.java
ActivityMain.java
- In Exercise 6, instead of drawing circles, draw a polyline (connected line segments).
- Research and test this widget: SeekBar.
Ans: Here are the layout and activity files:
activity_main.xml
MainActivity.java
- Look at the DrawStar Example.