2

In my application i have gridview in linearlayout. Due to different sizes of screen, i want to calculate the width of the gridview's container (which in linearlayout) and then i will decide, how many column should display on screen. My problem is. when i write

linearlayout.getwidth();

it returns 0 and its because its too early to get size of view in oncreate method.

What is the clean solution to get height or width of the view.

i tried

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int gridSize = display.getWidth();

but android says its deprecated :(

abidkhan303
  • 1,761
  • 3
  • 18
  • 31
  • Think if you really need to have it in `onCreate()` method. People tend to put all the code into this method, but sometimes it's not a good idea. Read more about `Activity` lifecycle: http://developer.android.com/reference/android/app/Activity.html – Piotr Chojnacki Jan 22 '13 at 21:53
  • I need this information before view is initialized because I want to decide how many columns should display in gridview. So not oncreate not onresume can help in this matter…If my question is poorly explained then tell me I will explain more it detail but help help kindly help. – abidkhan303 Jan 23 '13 at 10:42

1 Answers1

1

Display.getWidth() and Display.getHeight() are both deprecated as of API 13 in favor of Display.getSize(Point). In order to be compatible with both, you have to version-check and pick the appropriate one.

This is how I do so:

Point size = new Point();
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if (Build.VERSION.SDK_INT >= 13) {
    display.getSize(size); // API 13+
} else {
    size.x = display.getWidth();
    size.y = display.getHeight();
}

Then afterward, size.x is your width. (Don't forget to precede your method signature with @TargetApi(13) @SuppressWarnings("deprecation").)

Keep in mind, this is the unit's display width; if you want to simply get the size of one view, you have to call it after the UI is laid out. That's slightly unrelated to this answer, but you can find a solution in this thread's answer instead.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
  • I am facing problem with this code if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) { display.getSize(size); // PROBLEM IS HERE WITH GETSIZE() int width = size.x; int height = size.y; } Here is there error...The method getSize(Point) is undefined for the type Display. I tried everything but problem is still there – abidkhan303 Jan 23 '13 at 20:19
  • Why are you using `VERSION_CODES.GINGERBREAD`? I explicitly said to use 13 (which, by the way, is ICS, not Gingerbread). – Cat Jan 23 '13 at 20:49
  • It does not make any difference Eric. I simply copied your code and i put @TargetApi(13) @SuppressWarnings("deprecation") at the start of Oncreate method and on the start of Activity also but this problem is there – abidkhan303 Jan 24 '13 at 19:52
  • The code you put in your comment above says `GINGERBREAD`. Can you update your initial post and add the full relevant code you're currently using, as well as the stack trace? – Cat Jan 24 '13 at 20:13
  • Eric i found the solution and it is independent and can be used with every version.......WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); layoutsize = display.getWidth(); – abidkhan303 Jan 24 '13 at 20:48
  • Uhh.. okay.. that's still `Display.getWidth()` which is deprecated on newer versions. But whatever floats your boat, I guess! – Cat Jan 24 '13 at 22:33