0

I want to get some layout dimensions, such as margin of a specific item for example. Is it possible to get that directly from resources?

Of course I could inflate a view with that layout, then get the item and ask it for the value, but is that the only option?

Alexander Dunaev
  • 980
  • 1
  • 15
  • 40

1 Answers1

1

Well yes, but I wouldn't advise it, you could find the dimensions of the screen, and then in your code do the math that figures out the dimensions yourself:

Display display = this.getActivity().getWindowManager().getDefaultDisplay();
        int w = display.getWidth();
        int h = display.getWidth();

Otherwise you could just do it the conventional way:

LinearLayout ll = (findViewById) ...
int whatever = ll.getWhatever();

Is there a specific reason you are avoiding that way? Might help find the solution

Parth Mehrotra
  • 2,712
  • 1
  • 23
  • 31
  • I am trying to build a [custom layout that is larger than screen](http://stackoverflow.com/questions/12130999/how-to-make-a-dynamic-layout-that-is-larger-than-screen) and also has variable and _changing_ dimensions, as the items are expanding and collapsing, so I need to recalculate the size every time. To optimize that, I want to find some dimensions at the very beginning, before any items are rendered. Of course I could delay this until the first item is on the screen, and then ask the item for a value, but that looks a bit ugly. Getting the value from resources looks much more elegant. – Alexander Dunaev Sep 06 '12 at 00:47
  • I'm sorry as far as I know, the "conventional" way is the only way, I don't think it takes much, in terms of load on the UI thread, otherwise you could always create a new thread. – Parth Mehrotra Sep 10 '12 at 16:53
  • Your answer doesn't explain the things, but in general it is correct because the method that you suggested is the only possible. And here is my explanation: the layout itself does not exist as objects with real dimensions, so we have to inflate a view _and_ wait until the view is rendered; only then we can query for the dimensions. As for getting the screen dimensions—your method will give us the full screen size, but sometimes we need the available viewport size; for that, consider using android.view.View.getWindowVisibleDisplayFrame(). – Alexander Dunaev Sep 17 '12 at 13:08
  • Just found this today, take a look at about 36:00 where he talks about copying a view http://www.youtube.com/watch?v=duefsFTJXzc&playnext=1&list=PL7A8B87ACD7717FE9&feature=results_main – Parth Mehrotra Sep 18 '12 at 21:03