0

I am trying to create a bitmap out of a relative layout that i have created programmatically. The Realtivelayout is showing as expected but when i try to create a bitmap , it returns illegal argument exception that height and width must be > 0 this is how i am doing it

Bitmap.createBitmap(saveLayout.getWidth(),
            saveLayout.getHeight(), Bitmap.Config.ARGB_8888);

Any pointers?

Edit: added this code, too:

    ViewTreeObserver viewTreeObserver = saveLayout.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver
                .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        saveLayout.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(this);
                        viewWidth = saveLayout.getWidth();
                        viewHeight = saveLayout.getHeight();

                    }
                });
    }

In above given code onGlobalLayout() also never seemed to be called.

Nate
  • 31,017
  • 13
  • 83
  • 207
chossen-addict
  • 370
  • 1
  • 7
  • 31

1 Answers1

1

If you're programmatically creating the saveLayout, you may be asking for its width and height too early. (just an educated guess, because I can't see the rest of your code)

See this explanation

The reason why that createBitmap() call throws that exception is:

if the width or height are <= 0

So, try to defer creating the Bitmap until after the view appears, or find another way to calculate the width/height.

This question has a few options/answers for how to defer this calculation to avoid 0 results

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • Hey , Thanks for the help Now i am trying to use this code to get width and height ViewTreeObserver viewTreeObserver = saveLayout.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { viewTreeObserver .addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { saveLayout.getViewTreeObserver() .removeGlobalOnLayoutListener(this); viewWidth = saveLayout.getWidth(); viewHeight = saveLayout.getHeight(); } }); but on global layout never seemed to be called , can you tell why? – chossen-addict Feb 19 '13 at 02:03
  • Please post additional code into your **question** above (using the **edit** link below it), and format it as code. It's really hard to read what you just posted here. Thanks. – Nate Feb 19 '13 at 02:05
  • does it at least get inside the `isAlive()` call, to add the new listener? Also, see [this question for some issues with this, as well as some other alternative solutions](http://stackoverflow.com/a/4406090/119114) – Nate Feb 19 '13 at 02:21
  • well yes it is getting inside the isalive() call. – chossen-addict Feb 19 '13 at 02:25
  • It's hard to say ... of the code I can see, I don't see anything else wrong. Did you also try the `onWindowFocusChanged()` method shown in the last link I provided? And, just to double-check, you are determining that `onGlobalLayout()` is **not** called by setting a breakpoint in that method, and not seeing it hit? – Nate Feb 19 '13 at 02:28
  • Thanks for the help , looks like i wasnt calling onwindowfocuschange properly. Thanks :) – chossen-addict Feb 19 '13 at 12:52