0

I wrote my own GridView and i using it with XML setting layout_width as match_parent. My question is how to get width in pixels from attrs. I know how to get my cyston attrs but dont know how to read width value before view is drawed.

Adrian Deja
  • 737
  • 1
  • 9
  • 17

3 Answers3

0

In android you can't access the attribute of size before drawing because the dimensions are determined when the drawing process begin. It is the same for all views describe by xml.

sam
  • 3,441
  • 2
  • 33
  • 42
  • So i can't read width for measuring sizes of each element in gridview? i want to do something like this: Define whole gridview in xml but in java i want specify number of elements (custom views) and i want them to fill all size of defined grid view. – Adrian Deja Dec 07 '12 at 19:49
  • so what youwant is the size of the gridviw when it is drawing ? Can i ask you why you need this ? – sam Dec 07 '12 at 20:01
  • grid view holds custom views extends from view. In java i define how much elements it should holds and add this elements. I also define number of columns. And i want to resize all view inside grid view to match defined grid size which in this case is match_parent. For egz. another layout or activity. – Adrian Deja Dec 07 '12 at 20:05
  • All i want to know now is how to get size of parent. – Adrian Deja Dec 07 '12 at 20:24
  • I'm sorry but it is just impossible to get the size in onCreate for example, but you can get it after the start process, so you can get it with a button or anything else like that but it's not what you want to do – sam Dec 07 '12 at 20:33
  • you can try to find a way to get the size that the match_parent will give , maybe it is possible to know this depending of the size of the screen – sam Dec 07 '12 at 20:34
  • I know i can take screen size. And i don`t want to do this in OnCreate. I made my own custom gridView and i wanted to read this from attrs. – Adrian Deja Dec 07 '12 at 21:08
0

I found this link that try to find a way to get the height so you could use the same to find the width of the screen (if your grid fill the size of the screen) : Android - get Height of the parent layout

They give : getApplicationContext().getResources().getDisplayMetrics().heightPixels

Hopes it helps

If you find it useful please check Resolved

Community
  • 1
  • 1
sam
  • 3,441
  • 2
  • 33
  • 42
0

I solve this. Just need to getMeasuredDimensions after onMeasure method is called.

Adrian Deja
  • 737
  • 1
  • 9
  • 17