What is onMeasure Custom View in Android?
Last Updated :
06 Dec, 2022
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 in 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 in 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 in 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
<!-- ImageView Code in XML under Linear Layout -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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
// implementation of onMeasure Custom View in java
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// new width you want
int newWid = 60;
// new height you want
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;
// Measure Width custom view
if (wM == MeasureSpec.EXACTLY) {
// Must be of width size
width = wS;
} else if (wM == MeasureSpec.AT_MOST) {
// Can't be bigger than new
// width and width size
width = Math.min(newWid, wS);
} else {
// Be whatever you want
width = newWid;
}
// Measure Height of custom view
if (hM == MeasureSpec.EXACTLY) {
// Must be of height size
height = hS;
} else if (hM == MeasureSpec.AT_MOST) {
// Can't be bigger than new
// height and height size
height = Math.min(newht, hS);
} else {
// Be whatever you want
height = newht;
}
// for making the desired size
setMeasuredDimension(width, height);
}
Similar Reads
How to Create WhatsApp Stories View in Android? Stories are now becoming one of the most seen features in many different apps such as WhatsApp, LinkedIn, Instagram, and many more. In this article, we will take a look at creating a similar type of view in our Android App. What we are going to build in this article? We will be building a simple a
7 min read
Custom ArrayAdapter with ListView in Android In the previous article ArrayAdapter in Android with Example, it's been discussed how the ArrayAdapter works and what are the data sources which can be attached to the ArrayAdapter with ListView. In this article, it's been discussed how to implement custom ArrayAdapter with the ListView. Have a look
7 min read
Custom SimpleAdapter in Android with Example The Adapter acts as a bridge between the UI Component and the Data Source. It converts data from the data sources into view items that can be displayed into the UI Component. In Android, SimpleAdapter is an easy adapter to map static data to views defined in an XML (layout) file. You can specify the
8 min read
Chrome Custom Tabs in Android with Kotlin As developers, we have the option of displaying web content to a user in their browser or via WebViews, but both have their own limitations: starting the browser is a large, non-customizable context transition for users, whereas WebViews don't support all aspects of the web platform. To address this
3 min read
Elastic View in Android In this article, ElasticView is added in android. The ElasticView is a regular CardView, which can flex from user touches. OnClickListener and other various important methods can also be added to the child view of ElasticView. It makes the user interface more attractive thereby enhancing the user ex
2 min read