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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:51:32+00:00 2026-06-10T02:51:32+00:00

I have a ViewAnimator that contains two child views. I’d like for the ViewAnimator

  • 0

I have a ViewAnimator that contains two child views. I’d like for the ViewAnimator to be the size of the first child, regardless of which child is currently active. Is this possible with a bit of configuration or do I have to implement my own subclass and override onMeasure, etc?

  • 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-10T02:51:34+00:00Added an answer on June 10, 2026 at 2:51 am

    I ended up solving this by extending ViewAnimator, overriding onMeasure and setting the dimensions of the layout to the dimensions of the first child. Here’s what I came up with. It’s basically a small modification on the FrameLayout.onMeasure method.

    /**
     * Overrides the default {@link android.widget.FrameLayout#onMeasure(int, int)}
     * behavior to allow a main child to be defined which determines the size of the
     * layout. By default, the max size of all children is used as the layout size.
     */
    public class SizePinnedViewAnimator extends ViewAnimator {
        private final int mainChildIndex;
    
        @SuppressWarnings("UnusedDeclaration")
        public SizePinnedViewAnimator(final Context context) {
            super(context);
            mainChildIndex = 0;
        }
    
        @SuppressWarnings("UnusedDeclaration")
        public SizePinnedViewAnimator(final Context context, final AttributeSet attrs) {
            super(context, attrs);
    
            TypedArray a = context.obtainStyledAttributes(
                    attrs,
                    R.styleable.SizePinnedViewAnimator);
            mainChildIndex = a.getInt(R.styleable.SizePinnedViewAnimator_mainChild, 0);
            a.recycle();
        }
    
        /**
         * {@inheritDoc}
         *
         * Copied from {@link android.widget.FrameLayout#onMeasure(int, int)}
         */
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            final int count = getChildCount();
    
            View mainChild = getChildAt(mainChildIndex);
            if(mainChild == null) {
                throw new IllegalStateException("No child at index " + mainChildIndex);
            }
    
            measureChildWithMargins(mainChild, widthMeasureSpec, 0, heightMeasureSpec, 0);
    
            int maxHeight = mainChild.getMeasuredHeight();
            int maxWidth = mainChild.getMeasuredWidth();
    
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
    
            // Find rightmost and bottommost child
            for (int i = 0; i < count; i++) {
                if(i == mainChildIndex) continue;
    
                final View child = getChildAt(i);
                if (getConsiderGoneChildrenWhenMeasuring() || child.getVisibility() != GONE) {
                    measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
                }
            }
    
            // Don't have access to the foreground padding numbers, but we're not
            // using a foreground drawable anyway, so ignore it.
    
            // Account for padding too
            maxWidth += getPaddingLeft() + getPaddingRight();
            maxHeight += getPaddingTop() + getPaddingBottom();
    
            // Check against our minimum height and width
            maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
            maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
    
            setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
                    resolveSize(maxHeight, heightMeasureSpec));
        }
    }
    

    And the attribute defined in res/values/attrs.xml:

    <declare-styleable name="SizePinnedViewAnimator">
        <attr name="mainChild" format="integer" />
    </declare-styleable>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I'm trying to create an animation to transition between two views, both of which
Have you ever tried working with a XAML file which contains thousand tons of
have written this little class, which generates a UUID every time an object of
Have deployed numerous report parts which reference the same view however one of them
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I have a widget that periodically updates itself (hourly) to display top result of
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have a look at one of my websites: moskah.com The problem is that it
Have a simple contact us XPage created. Have server side validation in place that

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.