20

I've been playing around with Android development and one of the things I'd like to be able to do is dynamically create a background image for my windows, similar to the one below.

alt text

This is from my BlackBerry app. It consists of three separate parts, the bottom right logo, the top left watermark, and the bottom right name. It works independent of screen size because the BlackBerry app just gets all three parts and generates an appropriately sized bitmap using the screen width and height.

Since Android has quite a bit more screen resolution possibilities I need to be able to generate backgrounds on the fly like this. However, I have not found any way to get the height/width of the window in Android. I can get the screen resolution, but that includes the application title bar and the notification bar, which is unacceptable.

I'd like to know how to get the size of my window, or screen resolution minus the title and notification bars. I think this might be possible using the dimensions of my layout managers but I cannot get the height/width of them in the onCreate method so I'm not sure what to do exactly.

Thanks.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Victor
  • 1,137
  • 1
  • 10
  • 15

5 Answers5

19

Use View.MeasureSpec.getSize method in onMeasure override.

    @Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    ...
}
Evgeny
  • 665
  • 4
  • 14
  • Evgeny's answer led me here, which answered the rest of my questions http://developer.android.com/guide/topics/ui/custom-components.html – Stanley.Goldman Sep 10 '10 at 00:09
  • Well I understood the approach of this answer only with the addition from Evgeni Shafran. (You have to write your custom Layout where you override the onMeasure method and set some static variables for the heigt and width.) Still this approach is not working because when do you want to get these static sets values? In any of the onCreate or onStart it will be to early and you get a null value. I suggest the .post(new Runnable) implementation by Adorjan Princz in http://stackoverflow.com/questions/6939002/if-i-call-getmeasuredwidth-or-getwidth-for-layout-in-onresume-they-return-0 – Simon Feb 26 '15 at 09:30
1

I have a tutorial on my blog that give you the ability to save your screen size on launch, You can read it here: http://evgeni-shafran.blogspot.com/2011/01/android-screen-size-problem.html

Basicly you need to overide an onMeasure method of the first layout that hold you full screen, and get the width and height from there

0

have a look at getwidth() and getheight() maybe? that should give you the size of the screen. though, I don't know If it takes the bars or not. But I don't think so...

Sephy
  • 50,022
  • 30
  • 123
  • 131
  • 1
    Which getWidth/getHeight methods are you talking about? I might not have been clear enough in the question, sorry. I'm developing for Android, not BlackBerry, so Screen.getWidth() isn't what I'm looking for. – Victor Mar 22 '10 at 01:02
  • getwidth does not apply to the screen but to a view or layout. (I understood you were on android as you tagged your question so ;)) So if you put for instance a Relative layout with attributes fill_parent for its width and height and do getwidth and getheight in your code, I think it should give you satifaction – Sephy Mar 22 '10 at 07:44
0

in the first onDraw, call getWidth, getHeight. these will not be valid before then.

if you are not using a custom view/layout then you can instead call getWidth/getHeight during the first onWindowFocusChanged

SteelBytes
  • 6,905
  • 1
  • 26
  • 28
-1

To accomplish this in my app, I had to find a View's dimensions within the main Activity. It looked something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    gestureScanner = new GestureDetector(this);   //Object for handling gestures.

    mvView = new MyView(this);  //MyView is class that extends View.
    setContentView(myView);
}

@Override
public void onShowPress(MotionEvent e) {

    //Returns the size of the entire window, including status bar and title.
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);

    //Try to find the dimension of the view without the status/title bars.
    int iViewHeight = mvMountain.getHeight();
    int iViewWidth = mvMountain.getWidth();


    Toast toast = Toast.makeText(this," View:" + iViewWidth + ","+iViewHeight + " Window: " + dm.widthPixels + " by " + dm.heightPixels, 2);
    toast.show();
}
rat
  • 11
  • 2