What is onMeasure Custom View in Android?
In Android, there are many views available already, using them any developer creates a UI of an android application, that a developer wants to create. In-Built Views are the following:
1. TextView
A user interfaces element that displays text to the user.
XML
< TextView
android:id = "@+id/geeksforgeeksText"
android:layout_height = "wrap_content"
android:layout_width = "wrap_content"
android:text = "Welcome to geeksforGeeks!" />
|
2. EditText
A user interfaces element for entering and modifying text.
XML
< EditText
android:id = "@+id/GFGinput"
android:layout_height = "wrap_content"
android:layout_width = "match_parent"
android:inputType = "Text" />
|
3. Button
A user interfaces element the user can tap or click to perform an action.
XML
< Button
android:id = "@+id/geeksforgeeks_Button"
android:layout_height = "wrap_content"
android:layout_width = "wrap_content"
android:text = "Log In To GeeksforGeeks" />
|
4. ImageView
Displays image resources, for example, Bitmap or Drawable resources.
XML
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent" >
< ImageView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src = "@drawable/geeksforgeeksImage"
android:contentDescription = "GeeksforGeeks Image"
/>
</ LinearLayout >
|
There are many more inBuilt views, layouts, and ViewGroups.
Visit it to know more: Android View
Custom Views
If any developer wants to create a UI which have views according to his thought. That created view is called CUSTOM VIEW. Android gives a powerful model for building your UI, based on the fundamental layout classes.
onMeasure():
We want to draw a View, first, we need to know the size of the View, and how the system draws it, and then tell the system, that this process is done in the onMeasure() method.
Syntax:
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
So, basically to decide the size of the view by the developer we use the onMeasure() method. we do overriding onMeasure to get the desired size of the view. When onMeasure is called you get widthMeasureSpec and heightMeasureSpec. This Spec is a way for the parent view to informing you about the requested size and size mode for your view. A size mode is a constraint the parent view sets for its child.
Below is the example of onMeasure overriding Use:
Java
@Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
int newWid = 60 ;
int newht = 50 ;
int wM = MeasureSpec.getMode(widthMeasureSpec);
int wS = MeasureSpec.getSize(widthMeasureSpec);
int hM = MeasureSpec.getMode(heightMeasureSpec);
int hS = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
if (wM == MeasureSpec.EXACTLY) {
width = wS;
} else if (wM == MeasureSpec.AT_MOST) {
width = Math.min(newWid, wS);
} else {
width = newWid;
}
if (hM == MeasureSpec.EXACTLY) {
height = hS;
} else if (hM == MeasureSpec.AT_MOST) {
height = Math.min(newht, hS);
} else {
height = newht;
}
setMeasuredDimension(width, height);
}
|
Last Updated :
06 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...