0

Edit: I'm trying to get the height/width of a view, so that I can programmatically put some ImageView into it. However getHeight() and getWidth() returns 0 so I was not able to put the ImageView into the correct positions. I did make the ImageView appear on the RelativeView (main_activity) though.

I'm aware of the answer related the getHeight() returning 0 error: How to retrieve the dimensions of a view?
Unfortunately it's gives me exception on the line with ld.setLayerInset(), saying I'm trying to invoke the method on a null pointer reference. I' aware of the meaning of such an exception, but I don't understand what's wrong with my code as I was just trying to copy and paste the codes on the above link to my codes with amendments on the variable names. Here are some of my codes:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.activity_main).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            LayerDrawable ld = (LayerDrawable) findViewById(R.id.activity_main).getBackground();
            ld.setLayerInset(1,0,0,0,0);
            if (Build.VERSION.SDK_INT >= 16) {
                findViewById(R.id.activity_main).getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                findViewById(R.id.activity_main).getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    });
...
}

I think the problem occurs on getBackground() getting null then cause the next line to throw exception, but I don't know what's the problem at all.

Community
  • 1
  • 1
Jenny L.
  • 37
  • 9
  • Perhaps there is no background on whatever `findViewById(R.id.activity_main)` returns. – CommonsWare Dec 28 '16 at 14:50
  • Do you mean no background for the RelativeLayout? It currently doesn't have a background yet, but that would be quite strange if that's really the problem. Also how did that answer provider in the link knew OP or the others must have a background for their views anyway? – Jenny L. Dec 28 '16 at 15:15
  • "but that would be quite strange if that's really the problem" -- why? I would expect `getBackground()` to return `null` for something that does not have a background. – CommonsWare Dec 28 '16 at 15:21
  • Because we're not suppose to really need a background to get what e want in the link provided in the link, the height/width of a view. Would you expect every view to have a background in order for the code to work, provided the answer in that link is a solution to finding the height and/or width to get `getHeight()` and/or `getWidth()` to not return 0? – Jenny L. Dec 29 '16 at 04:09

1 Answers1

0

View's size is not related to its background at all. A view can have a non zero size without having a background (hopefully !). You do not have to carry of the view's bg.

Then a detail, the findViewById(int) method can be a really heavy call, you should call it once.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final View mainView = findViewById(R.id.yourViewId)
    mainView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            // At this stage of layout, if your view is well positioned, its width and height aren't zero
            int width = mainView.getWidth();
            int height = mainView.getHeight();

            // Do whatever you want with view's size here

            if (Build.VERSION.SDK_INT >= 16) {
                mainView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                mainView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    });
...
}
Pdroid
  • 867
  • 9
  • 13
  • I'd need the code to return the height and width of the view for some other codes, but the error is not on those codes so I didn't show them. I'm pretty sure there's an id for it and the id is correct. Also, even if I've just set a background for it (a transparent background) it's not working. – Jenny L. Dec 29 '16 at 04:05
  • There is no direct link between width/height and background, if getBackground() returns null, it's because your view has no bg. So the LayerDrawable you expect to be the background is not the background of the view at all. You have to set your RelativeLayout background first. – Pdroid Dec 29 '16 at 13:40
  • But what if I want the relative layout (as it's the root layout) to be "match parent", aka the screen size of the device (minus the theme bar) ? How do I get the size that's not depending on a background at all, or do I just need a random background of any size to make this work? – Jenny L. Dec 30 '16 at 01:55
  • The size of the view do not depends of the background at all. A view (most of ViewGroup, hopefully !) can have a non zero width and height without background. I think you missunderstand someting fundamental to reach what you want to achieve. I really suggest you to explain what is your goal precisely. – Pdroid Dec 31 '16 at 17:57
  • I'm trying to get the height/width of a view, so that I can programmatically put some `ImageView` into it. However `getHeight()` and `getWidth()` returns 0 so I was not able to put the `ImageView` into the correct positions. I did make the `ImageView` appear on the `RelativeView` (`main_activity`) though. – Jenny L. Jan 03 '17 at 13:51
  • just put it using addView(View child) and then set layout params to be wrap_content and/or match_parent ? – eriuzo Jan 03 '17 at 13:55
  • I don't want it to be `wrap_content` or `match_parent` though, besides I'd like the views to be put into somewhere specific on the layout/screen. – Jenny L. Jan 14 '17 at 02:40
  • You simply need to call `View.getWidth()` and/or `View.getHeight()` on your `mainView` inside `onGlobalLayout()` callback. Without carring about background. (Your background can add margins but it's another subject) – Pdroid Jan 24 '17 at 15:36