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 8131487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:06:23+00:00 2026-06-06T09:06:23+00:00

I have to build a more complex custom view in Android. The final layout

  • 0

I have to build a more complex custom view in Android. The final layout should look like this:

<RelativeLayout>
  <SomeView />
  <SomeOtherView />
  <!-- maybe more layout stuff here later -->
  <LinearLayout>
    <!-- the children -->
  </LinearLayout>
</RelativeLayout>

However, in the XML files I just want do define this (without defining SomeView, SomeOtherView etc.):

<MyCustomView>
  <!-- the children -->
</MyCustomView>

Is this possible in Android, and if yes: What would be the cleanest way to do it?
The possible solutions that came to my mind were ‘override the addView() methods’ and ‘remove all views and add them again later’, but I am unsure which way to go…

Thanks a lot in advance for your help! 🙂

  • 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-06T09:06:24+00:00Added an answer on June 6, 2026 at 9:06 am

    It’s absolutely possible, and encouraged, to create custom container views. This is what Android would call a compound control. So:

    public class MyCustomView extends RelativeLayout {
        private LinearLayout mContentView;
    
        public MyCustomView(Context context) {
            this(context, null);
        }
    
        public MyCustomView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            //Inflate and attach your child XML
            LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
            //Get a reference to the layout where you want children to be placed
            mContentView = (LinearLayout) findViewById(R.id.content);
    
            //Do any more custom init you would like to access children and do setup
        }
    
        @Override
        public void addView(View child, int index, ViewGroup.LayoutParams params) {
            if(mContentView == null){
                super.addView(child, index, params);
            } else {
                //Forward these calls to the content view
                mContentView.addView(child, index, params);
            }
        }
    }
    

    You can override as many versions of addView() as you feel are necessary, but in the end they all call back to the version I placed in the sample. Overriding just this method will have the framework pass all children found inside its XML tag to a specific child container.

    And then modify the XML as such:

    res/layout/custom_layout.xml

    <merge>
      <SomeView />
      <SomeOtherView />
      <!-- maybe more layout stuff here later -->
      <LinearLayout
          android:id="@+id/content" />
    </merge>
    

    The reason for using <merge> is to simplify the hierarchy. All the child views will get attached to your custom class, which is a RelativeLayout. If you don’t use <merge>, you end up with a RelativeLayout attached to another RelativeLayout attached to all the children, which can cause issues.


    Kotlin version:

    
        private fun expand(view: View) {
            val parentWidth = (view.parent as View).width
            val matchParentMeasureSpec = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY)
            val wrapContentMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
    
            view.measure(matchParentMeasureSpec, wrapContentMeasureSpec)
            val targetHeight = view.measuredHeight
            view.isVisible = true
            val animation: Animation = getExpandAnimation(view, targetHeight)
    
            view.startAnimation(animation)
        }
    
        private fun getExpandAnimation(
            view: View,
            targetHeight: Int
        ): Animation = object : Animation() {
            override fun applyTransformation(
                interpolatedTime: Float,
                transformation: Transformation
            ) {
                view.layoutParams.height =
                    if (interpolatedTime == 1f) {
                        LayoutParams.WRAP_CONTENT
                    } else {
                        (targetHeight * interpolatedTime).toInt()
                    }
    
                view.requestLayout()
            }
    
            override fun willChangeBounds(): Boolean {
                return true
            }
        }.apply {
            duration = getDuration(targetHeight, view)
        }
    
        private fun collapse(view: View) {
            val initialHeight = view.measuredHeight
            val animation: Animation = getCollapseAnimation(view, initialHeight)
    
            view.startAnimation(animation)
        }
    
        private fun getCollapseAnimation(
            view: View,
            initialHeight: Int
        ): Animation = object : Animation() {
            override fun applyTransformation(
                interpolatedTime: Float,
                transformation: Transformation
            ) {
                if (interpolatedTime == 1f) {
                    view.isVisible = false
                } else {
                    view.layoutParams.height =
                        initialHeight - (initialHeight * interpolatedTime).toInt()
                    view.requestLayout()
                }
            }
    
            override fun willChangeBounds(): Boolean = true
    
        }.apply {
            duration = getDuration(initialHeight, view)
        }
    
        /**
         * Speed = 1dp/ms
         */
        private fun getDuration(initialHeight: Int, view: View) =
            (initialHeight / view.context.resources.displayMetrics.density).toLong()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one is a weird one. I have a more complex application that I
I have build a c# class library verification.dll using OpenCVSharp. This references OpenCvSharp.dll in
I have build my very first application to Android and I want now to
Similar to this question: JQuery: Build HTML in 'memory' rather than DOM I have
I have a simple GridView ; something like this for a regular Item or
I have a node tree built out of Node objects. They are more complex
I have built CMS on cakephp framework. I can delete just one or more
I have more than 3000 members on a site built using wordpress. I am
I have build C# program that work with RAPI (communication to PPC or WinCE)
I have build a webapplication using ASP.NET MVC and JQuery. On my local machine

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.