Recipe Customizing a Button

This recipe customizes a button using a class called myButton. It extends the Button widget so that the component inherits most of the Button features.To customize a widget, the most important methods are onMeasure() and onDraw().

The onMeasure() method determines the size requirements for a widget. It takes two parameters: the width and height measure specification. Customized widgets should calculate the width and height based on the contents inside the widget, and then call setMeasuredDimension() with these values. If this is not done, an illegalStateException is thrown by measure().

The onDraw() method allows customized drawing on the widget. Drawing is handled by walking down the tree and rendering view by view. All parents are drawn before the children get drawn. If a background drawable is set for a view, then the view draws that before calling back to its onDraw() method.

Inside the myButton class, eight member methods and two constructors are implemented. The member functions are

■ setText()—Set the text that is drawn on the button.

■ measureWidth()—Measure the width of the button widget.

■ measureHeight()—Measure the height of the button widget.

■ onDraw()—Draw the graphics on the button widget.

■ onMeasure()—Measure and set the boundary of the button widget.

The methods setText(), setTextSize(), and setTextColor() change the text attributes. Every time the text is changed, the invalidate() method needs to be called to force the view to redraw the button widget and reflect the change. The method requestLayout() is called in the setText() and setTextSize() methods but not in the setTextColor() method.This is because the layout is only needed when the boundary of the widget changes, which is not the case with text color change.

Inside onMeasure(), the setMeasuredDimension() method is called with measureWidth() and measureHeight(). It is an important step for customizing the View.

The methods measureWidth() and measureHeight() are called with the size of the parent view and need to return the proper width and height values of the custom view based on the requested mode of measurement. If the exactly mode of measurement is specified, then the method needs to return the value given from parent View. If the AT_MOST mode is specified, then the method can return the smaller of the two values— content size and parent view size—to ensure the content is sized properly. Otherwise, the method calculates the width and height based on the content inside the widget. In this recipe, the content size is based on the text size.

The method drawArcs() is a straightforward function that draws arcs on the button. This is called by onDraw() as the text is drawn.Animation of the arcs also takes place here. Every time the arc is drawn, its length is incremented a little and the gradient is rotated making a nice animation.

The class for the custom button is shown in Listing 11.1. A constructor method is required, and here, two MyButton() methods are shown depending on arguments. Each initializes the label view with the custom attributes.The android.graphics.* libraries are similar in format to Java for graphics manipulations, such as Matrix and Paint.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment