File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes calculate ImageView dimentions before user interact the UI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "calculate ImageView dimentions before user interact the UI" Watch "calculate ImageView dimentions before user interact the UI" New topic
Author

calculate ImageView dimentions before user interact the UI

Dov Ezra
Greenhorn

Joined: Nov 23, 2010
Posts: 21
Hello again,

I'm trying to build a grid of N by N images in my UI.
This grid come with two different sizes, depending of what the user selected in the welcome screen. the two optional grids are positions inside a fix sized GridView, defined in the xml. the question is what should be the height and width of the Image view dimentions? it should be 1/4 of the gridView when user select a 4 by 4 grid, 1/6 if we whant 6 by 6 grid, and so on.

I tried to call gridView.getHeight() at my onCreate(), in order to use this to set my ImageView dimentions.
but getHeight() return zero. now I know that UI dimentions are set only after onCreate exits.

so what should I do?

I tried to set the imageView size with a set of hard coded values, depend of the grid size.
this work nice on my emulator, with a fixed sized screen. if I want to support different screen sizes and resulotions, this is the wrong way to go.

I also tried another strategy, with limited success:
I need the dimensions inside of the gridView's Adapter, where i find what image the user just clicked on. so lets pass a reference to the gridView into the adapter. the gridView should have done its onMeasure by the time we first call adapter.getView(int position, View convertView, ViewGroup parent);
right? so I tried this:

inside GameActivity.onCreate() :


and inside the adapter I have:



so will be able to find the gridView's dimentions when creating the Images.
the problem is that my GridView shows all images except the one in its top left corner.
now a click on any of the images on the grid and invokation of getView makes the top levt view to appear
but the bottom right image disappear, and vice versa. (attached image)

Any insight from experienced, please?



[Thumbnail for bad_puzzle.png]

Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
I'm no expert but here are some observations:

I tried to call gridView.getHeight() at my onCreate(), in order to use this to set my ImageView dimentions.
but getHeight() return zero. now I know that UI dimentions are set only after onCreate exits.

You can override the GridView class and intercept the dimensions in onDraw by calling native GridView.getHeight() or
whatever, and pass those to a static field in your Activity, or pass a reference to the Activity to your overridden
GridView class. Point is, the dimensions are available by the time doDraw() is called in the GridView.

I tried to set the imageView size with a set of hard coded values, depend of the grid size.
this work nice on my emulator, with a fixed sized screen. if I want to support different screen sizes and resulotions, this is the wrong way to go.


Find a good place to get the dimensions of the device screen and base all your other values on those--They should be available
in your GridView creator or in its doDraw() method. Not sure where the best place to get them is, maybe even onCreate().
Anyway you can then set all of your variables/dimensions scaled to the screen resolution of the device your app is running
on. This assumes that you have setContentView([GridView]) ie your GridView is the activity's top View, so that it fills the
screen and its dimensions are the screen dimensions.


SCJP 88% | SCWCD 84%
Dov Ezra
Greenhorn

Joined: Nov 23, 2010
Posts: 21
Hi,

Thanks for your advice.
but I think i'm missing something.
Find a good place to get the dimensions of the device screen and base all your other values on those


isn't that bounds me to a specific screen size?
and even I will make a list of all possible screens and manage a lookup table for all possible dimensions,
I still need to update that list every time a new phone or tablet comes out. right?
I probably didnt understand you.

but is good to know that view size is avaliable at onDraw(). so overriding it sounds like a good strategy.

Thanks,
Dov
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
What I am suggesting is to make all of your dimensions relative
to the device width and height, so that means NO lookup table.

So the only hard value you get is say int w = gridView.getWidth();//or set it from onDraw()
then when you set your ImageView bounds you set the width to
something like int myViewWidth = Math.round(W * .12f); //round or cast to (int), 12% of width
and you do ALL of your dimensions like that. A bit tedious but
that's the only way to make sure your app scales appropriately to
whatever screen its in.

I'm not sure when GridView would have the dimensions--it would in onDraw but it
might somewhere else

Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

You can get Width and Height of the Display(Window) in onCreate. Maybe you can try and guess the size of ImageViews from that.


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Peter Ja
Greenhorn

Joined: Jan 22, 2011
Posts: 1
try out this:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calculate ImageView dimentions before user interact the UI