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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:53:36+00:00 2026-06-03T06:53:36+00:00

I have currently implemented a custom ViewGroup class, that attempts to position items (buttons

  • 0

I have currently implemented a custom ViewGroup class, that attempts to position items (buttons with drawables) evenly and diagonally across the screen.

Unfortunately if the items are too big they tend to overlap to make sure everything fits on the screen.

I am hoping that there’s an easier way to accomplish this with already existing layouts (API8+), or if there is something simple I can change in my ViewGroup class to make the child views smaller to avoid the overlapping. (Such as making each child view the exact size needed to evenly layout without overlap)

Here is my onMeasure class (not doing anything special. letting children measure themselfs)

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

        measureChildren(widthMeasureSpec, heightMeasureSpec);

         int width = MeasureSpec.getSize(widthMeasureSpec);
         int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width, height);
    }

EDIT: If I change onMeasure to manually measure the children to a certain width/height they get clipped. It doesn’t seem like the child view (or its drawable) actually scales itself to fit inside the measurement.

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

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        int newChildWidth = width / getChildCount();
        int newChildHeight = height / getChildCount();

        final int size = getChildCount();
        for (int i = 0; i < size; ++i) {
            View child = getChildAt(i);

            child.measure(MeasureSpec.EXACTLY | newChildWidth, MeasureSpec.EXACTLY | newChildHeight);
        }

        setMeasuredDimension(width, height);
    }
  • 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-03T06:53:38+00:00Added an answer on June 3, 2026 at 6:53 am

    So far you’ve got the right idea, if you want the child views to lay out exactly diagonal, it’s the job of the enclosing ViewGroup to provide them the MeasureSpec to accomplish that and MeasureSpec.EXACTLY will tell each child how large to be. You could also swap that out for MeasureSpec.AT_MOST to allow children with less content to be smaller, they would just never exceed the size you provided, whereas EXACTLY makes them all the same size every time.

    The problem you are describing now is one that cannot really be solved without some View customization. Android views pretty exclusively operate in the paradigm of “resize the view to its content” and not “resize the content to the size of the view”. So if you tell a Button to be smaller than the space it needs to draw its text/image contents, it will simply clip. The one exception to this rule is ImageView. If you would like to circumvent this, you will need to customize Button to resize the text size and drawable based on the specs given to it in onMeasure()

    Another possible option, since you are customizing ViewGroup anyway, is to make use of child static transformations. By calling setStaticTransformationsEnabled(true) in your ViewGroup and overriding getChildStaticTransformation() you could apply a scale factor to each child view that wants to be larger than your specific size. This transformation is similar to how animations work, so the whole view will be scaled down.

    HTH

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

Sidebar

Related Questions

I currently have implemented an overlay item that shows an icon for Geo-point on
I'm currently writing a class that implements the SeekableIterator interface and have run into
I have a class derived from JTree with custom TreeCellRenderers. I have implemented drag
I have implemented a custom QAbstractListModel that is displayed in an editable QListView .
I have implemented a sorting algorithm for a custom string that represents either time
Backgroup: We are looking at SAS BI Dashboard. We have currently implemented almost all
I've been trying to implement unit testing and currently have some code that does
To clarify, I currently have a live app that does not implement in app
I have implemented a custom trace listener (derived from TextWriteTraceListener ) and now I
I have implemented a custom allocator (to be used by STL containers within my

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.