Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9127989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:24:27+00:00 2026-06-17T07:24:27+00:00

I have a custom layout defined in XML file which has a RelativeLayout root

  • 0

I have a custom layout defined in XML file which has a RelativeLayout root with a bunch of child view.

Now, I defined the following class:

public class MyCustomView extends RelativeLayout {

    public MyCustomView(Context context) {
        super(context);
        init();
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();     
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);    
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater)  getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.my_custom_view, this, true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        Log.d("Widget", "Width spec: " + MeasureSpec.toString(widthMeasureSpec));
        Log.d("Widget", "Height spec: " + MeasureSpec.toString(heightMeasureSpec));

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int chosenWidth = chooseDimension(widthMode, widthSize);
        int chosenHeight = chooseDimension(heightMode, heightSize);

        int chosenDimension = Math.min(chosenWidth, chosenHeight);

        setMeasuredDimension(chosenDimension, chosenDimension);
    }

    private int chooseDimension(int mode, int size) {
        if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {
            return size;
        } else { 
            return getPreferredSize();
        }
    }

    private int getPreferredSize() {
        return 400;
    }
}

As you can see I’m setting the root to MyCustomView instance and the attach flag to true.

What I want to achieve is that when I will add this custom view in another layout’s xml, it will instantiate the MyCustomView class which will have the layout defined in the XML.

I already tries to use <merge> tag, but that way I lose the ability to arrange my child views in the XML as I want.

I also tried to inflate the XML and add it as a view to MyCustomView, but that way I get redundant RelativeLayout.

Last thing, I added the onMeasure() just for completeness.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:24:28+00:00Added an answer on June 17, 2026 at 7:24 am

    the inflation occurs but the child view are not shown

    The RelativeLayout does a bit more(a lot) than what you’ve done in the onMeasure layout(basically the children aren’t measured at all with your code so they don’t have something to show). If you extend a ViewGroup like RelativeLayout you need to let that class to do its callbacks(onMeasure, onLayout) or at least, very carefully replicate the methods and modify it like you want (if you want to see something).

    So, remove the onMeasure method to see the children or explain better why did you override it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a custom view set up in code (no XML layout defined
I have a custom listview defined in my xml layout file. I can add
I have a listView with custom objects defined by the xml-layout below. I want
I have custom component that I can place in my layout file (XML) for
If i have a expandablelistview with custom layout (group.xml and child.xml), the are any
I have implemented a custom view within another activity into a XML layout. The
I have an activity with a listView, which uses a custom row layout defined
I have a custom alertdialog that inflates this custom layout: <?xml version=1.0 encoding=utf-8?> <RelativeLayout
I've created a Custom Component in layout/XML which has 2 TextViews and 2 Buttons.
I have a custom SurfaceView defined in my layout xml. I can draw to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.