0

I'm trying to determine the screen's width and height to use them in a fragment. My fragment's layout has the id background.

If inside onViewCreated I use:

background = (RelativeLayout) view.findViewById(R.id.background);
        background.post(new Runnable() {
            @Override
            public void run() {
                int width = background.getMeasuredWidth();
                int height = background.getMeasuredHeight();
                Log.d("askj", width + "+" + height);
            }
        });

I get 1440+2276

but if I use:

 WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics displayMetrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(displayMetrics);
    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;
    Log.d("askj", width + "+" + height);

or other related methods I get:

1440+2560

I'm using those parameters to set a background and I can clearly see that the whole screen size is not taken if I use the second approach. I don't really want to use that Runnable() so is there any way in which I can solve this ?

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/background"
    tools:context="com.example.home.background.HomeScreen">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/img"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:textSize="22sp"
        />
</RelativeLayout>
Bogdan Daniel
  • 2,689
  • 11
  • 43
  • 76

2 Answers2

0

You can measure the background View before your activity has added it to its view hierarchy by yourself using measure method.

background = (RelativeLayout) view.findViewById(R.id.background);
background.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
int backgroundHeight = background.getMeasuredHeight();
int backgroundWidth = background.getMeasuredWidth();

In my case I measured an inflated View but it had the width and height set to match_paent.

View view = inflater.inflate(R.layout.table_field_text, null, false);
view.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int viewHeight = view.getMeasuredHeight();
vovahost
  • 34,185
  • 17
  • 113
  • 116
  • I'll try it now and get back to you :D – Bogdan Daniel Feb 12 '16 at 10:51
  • I got back height - `16777215` width - `16777215` – Bogdan Daniel Feb 12 '16 at 10:58
  • Do I have to convert them or something ? Why are they equal ? – Bogdan Daniel Feb 12 '16 at 11:02
  • No. `background.getMeasuredHeight()` should have returned the correct height in pixels. I also updated the answer with an example that returned the correct value in pixel. In my case I used `ViewGroup.LayoutParams.WRAP_CONTENT`. Maybe `.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)` doesn't return the right value when used this way. – vovahost Feb 12 '16 at 13:24
  • I changed it to match your edit and now I get 16777214 - 16777214 – Bogdan Daniel Feb 12 '16 at 14:12
0

Use ViewTreeObserver like Below:

background = (RelativeLayout) view.findViewById(R.id.background);

final ViewTreeObserver vto = background.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
@SuppressWarnings("deprecation") 
@Override 
public void onGlobalLayout() { 
    //get View Width and height here 
    int width = background.getWidth();
    int height = background.getHeight();

    //Call anyfunction which uses width and height here
    function(width,height);

    //Then remove layoutChange Listener 
    ViewTreeObserver vto = background.getViewTreeObserver();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        vto.removeOnGlobalLayoutListener(this);
    } else { 
        vto.removeGlobalOnLayoutListener(this);
    } 
} 
}); 
Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32
  • I was hoping for a solution to displayMetrics. If I want to go for something like this I can keep using `background.post` method – Bogdan Daniel Feb 12 '16 at 14:14