• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calculate ImageView dimentions before user interact the UI

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

bad_puzzle.png
[Thumbnail for bad_puzzle.png]
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dov Ezra
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get Width and Height of the Display(Window) in onCreate. Maybe you can try and guess the size of ImageViews from that.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try out this:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic