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

The Archive Base Latest Questions

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

So i have two custom views. I need to know the pixel dimensions of

  • 0

So i have two custom views. I need to know the pixel dimensions of my custom view and i got it the following way:

    protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
    {
        width_dimension = xNew; // << this is the field i need to share
        super.onSizeChanged(xNew, yNew, xOld, yOld);
    }

I have another custom view, where i need to pass the field “width_dimension”. What is the proper way to do this? I tried passing the info in the onCreate() method of my activity but it is called before the onSizeChanged() of my custom view so the field is undefined. I also tried passing in the onSizeChanged() method (above) but my OTHER custom view wasn’t instantiated by the xml layout yet (threw a null pointer exception).

Is overriding the onSizeChanged() method the proper way to determine a views width (it is set to wrap_content in xml)? And how can i share this information to another custom view?

Sorry if this question is not very clear. It’s hard to explain and the problem i’m facing is i need to know WHEN all my custom views have been instantiated and laid out so i can exchange dimension information (determined at runtime) between them.

  • 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-17T21:16:26+00:00Added an answer on June 17, 2026 at 9:16 pm

    You could use dimensions see here: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension are useful if you have the custom views implemented in xml.. you could also load the dimension in code like this:

    Resources res = context.getResources(); // you can use application or activity context for this
    float customWidth = res.getDimension(R.dimen.custom_width);
    

    I hope it helps!

    EDIT

    So it seems that this helped:

    yourlayout.getViewTreeObserver().addOnGlobalLayoutListener(ViewTreeObserver.OnGl‌​obalLayoutListener yourCustomListener); 
    

    The callback will get called after the view will get measured and draw. Also I would suggest to remove the listener from the view once the callback is called to avoid useless calls.

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

Sidebar

Related Questions

I have two overlapping custom views that need to both receive touch events (e.g.
I have two helper methods inside a custom View which are called by their
I have a view with two subviews: A UIImageView A small Custom View which
I have a custom dialog with one editText view and two buttons ok and
I have a view with custom drawRect method which has two text lines drawn
I have a zoomable UIScrollView which contains a custom drawn view (two circles). When
I have two custom ActionFilters on an action. In first of the actionfilters, I
I have two custom value transformers contained within my Other Sources folder, bound to
I have created two custom controls. On the basis of features any one of
I have two apps that use custom URL schemes to switch between each other.

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.