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

  • Home
  • SEARCH
  • 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 6228925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:28:25+00:00 2026-05-24T09:28:25+00:00

I am trying to keep track of the bounding Rect for a child TextView

  • 0

I am trying to keep track of the bounding Rect for a child TextView inside of a class extending LinearLayout I am using View.getGlobalVisibleRect(Rect) in order to get the TextView’s bounding box relative to its parent. It works great on some devices, but there is obviously some kind of unit issue going on on other phones. Simple example of what I’m seeing:

//Extended LinearLayout's onDraw
protected void onDraw(Canvas canvas){
    super.onDraw(canvas);
    TextView tv = (TextView)findViewById(R.id.textView1);
    Rect bounds = new Rect();
    tv.getGlobalVisibleRect(bounds);
    canvas.drawRect(bounds, myPaint);
}

Whats expected is that it should draw a box under the the TextView tv. It does on my 3.1 tablet, but on my 1.6 and 2.3 phones it draw the box underneath of the TextView. It seems that I need to convert the values of the bounding box to a differnt kind of pixel unit in order to get the results I expect consistently.

Problem is I am not sure if the Rect returned is already in DIP or standard pixels. I have tried doing both:

bounds.top = TypedValue.complexToDimensionPixelSize(bounds.top, getContext().getResources().getDisplayMetrics());

and

bounds.top = TypedValue.COMPLEX_UNIT_DIP, bounds.top, getContext().getResources().getDisplayMetrics());

But neither of these seems to be converting the box top to where it should be. I would greatly appreciate any feedback or advice.

Thanks!

  • 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-05-24T09:28:26+00:00Added an answer on May 24, 2026 at 9:28 am

    Turns out that getGlobalVisibleRect is either broken pre-3.0 or doesn’t return what I am expecting. I found that I could do this instead.

    TextView tv = (TextView)findViewById(R.id.textView1);
    Rect bounds = new Rect(tv.getLeft(), tv.getTop(), tv.getRight(), tv.getBottom()); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to keep track of the state in this app using Backbone.js: I
I am trying to keep track of time in my program using System.currentTimeMillis() However,
Im trying to make multiple quizes that will keep track of each quiz on
Im really trying to keep my code clean by using the switch statement but
I am trying to create a persistent and shared variable that will keep track
I'm trying to create an app that will help keep track of attendance information
I am trying to create a java script function to keep track of the
I'm trying to keep track of a set of files, which may have the
I'm trying to keep track of number of comparison between array elements being made
I am trying to keep track of the days since the birth of 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.