0

I need to get height of my tab(shown in the picture) in pixels in my app. I need some information that will help me to know what is height of Tab depended from the screen size. Can anyone help me?

Thanks in advanceenter image description here

Tell the truth I don't need Tab's size in code, I think that each device with same size will have same Tab height, so I want to know what part of screen will take my Tab ,

e.g. I have device with 1080 X 720 pixels , my Tab will take 1/10 part , it means that Tab's height will be 108 pixels

Hayk Nahapetyan
  • 4,452
  • 8
  • 42
  • 61

1 Answers1

0

What I meant to say in my comment earlier was that there are many popular solutions here to get the height and width of any android view. I have tested this code right now and it is working fine. Try this:

final RelativeLayout topLayout  = (RelativeLayout) findViewById(R.id.topRelLayout);
        ViewTreeObserver viewTreeObserver = topLayout.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
          viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 16)
                topLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                else
                topLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
             int  viewWidth = tabs.getWidth();
              int viewHeight = tabs.getHeight();

              Toast.makeText(HomeScreenActivity.this, "height = "  + viewHeight + " width = " + viewWidth , 2000).show();
            }
          });
        }

Where topRelLayout is the id of root layout of your xml file and tabs is the reference to your tab view.

Even following simple code is working fine too.

tabs.post(new Runnable() {
            @Override
            public void run() {
                int w = tabs.getMeasuredWidth();
                int h = tabs.getMeasuredHeight();
                Toast.makeText(HomeScreenActivity.this, "height = "  + h + " width = " + w , 2000).show();
            }
        });

Using either of the solutions you can get the height/width of any view in any screen size.

EDIT :

After reading your edited question I assume that you need what percentage of the screen has been occupied by Tab widget. Only solution I know can be implemented is :

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
         final   Display display = wm.getDefaultDisplay();

            tabs.post(new Runnable() {
                @Override
                public void run() {
                    int h = tabs.getMeasuredHeight();
                    int screenHeight = 0;
                    if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 13)
                    {
                        Point point = new Point();
                     display.getSize(point);
                     screenHeight = point.y;
                    }
                    else
                        screenHeight = display.getHeight();
                    double tabPart = (((double)h/(double)screenHeight) * 100); 
                    Toast.makeText(HomeScreenActivity.this, "height = "  + screenHeight + " tabPart = " + tabPart  + " %", 2000).show();


                }
            });
Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38