Skip to content
Related Articles
Open in App
Not now

Related Articles

What is onMeasure Custom View in Android?

Improve Article
Save Article
Like Article
  • Difficulty Level : Expert
  • Last Updated : 06 Dec, 2022
Improve Article
Save Article
Like Article

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.


<!-- TextView in XML  -->
  android:text="Welcome to geeksforGeeks!" />

2. EditText

A user interfaces element for entering and modifying text.


<!-- EditText in XML -->

3. Button

A user interfaces element the user can tap or click to perform an action.


<!-- button in XML  -->
  android:text="Log In To GeeksforGeeks" />

4. ImageView

Displays image resources, for example, Bitmap or Drawable resources.  


<!-- ImageView Code in XML under Linear Layout   -->
         android:contentDescription="GeeksforGeeks Image"

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. 


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. 


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:


// implementation of onMeasure Custom View in java
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);

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!