0

Possible Duplicate:
how do i keep the aspect ratio on image buttons in android?

i have 5 square dice as image buttons lined up near the bottom of my layout. i would like them to take up the whole width of the layout, but i can't seem to get them to properly keep that aspect ratio, while taking up the entire width. currently my code looks like this:

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_margin="1dp"
        android:layout_gravity="bottom" 
        > 
        <ImageButton
             android:id="@+id/die1" 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content" 
             android:layout_gravity="center"
             android:adjustViewBounds="true"
             android:layout_weight="1"
             android:layout_margin="2dp"
             android:scaleType="centerInside"
             />  
        <ImageButton
             android:id="@+id/die2" 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content" 
             android:layout_gravity="center"
             android:adjustViewBounds="true"
             android:layout_weight="1"
             android:layout_margin="2dp"
             android:scaleType="centerInside"
             /> .....

and looks something like this: 60dp

if i don't include the

android:layout_height="60dp"

then the dice become skewed and look like this: skewed

however the problem with leaving the "60dp" part in arises when i try to display on a tablet device or something with a high resolution it ends up looking like this:

enter image description here

where am i going wrong?!

Community
  • 1
  • 1
clayton33
  • 4,176
  • 10
  • 45
  • 64

3 Answers3

3

You're going to have to do 1 of two things:

1) Provide ldpi, mdpi, hdpi and x-hdpi versions of your dice images. Put those in their respective drawable folder and android will take care of the rest. Find out how here: http://developer.android.com/guide/practices/screens_support.html

Or

2) Get the screen size and set the imageview sizes manually java. You can get the screen size using:

 Display d = ((android.view.WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
 screenWidth = d.getWidth();

The rest is up to you. Good luck!

-edit- Setting ImageView (or any other View's size in Java)

 myHeight = screenSize/5; //These are in pixels not dp's
 myWidth = screenSize/5;
 LinearLayout.LayoutParams myLayoutParams = 
               new LinearLayout.LayoutParams(myWidth,myHeight);
 myView.setLayoutParams(myLayoutParams);

Hope it fares well.

Zaid Daghestani
  • 8,555
  • 3
  • 34
  • 44
  • thanks for your answer. 1) i can't get this to work. whether i put the resolution of the images to 90px or 900px i get the exact same ratio results. 2) i was kind of hoping to do this through xml and avoid doing a lot of math through code, but if all else fails i will implement this method. thank you – clayton33 Nov 01 '11 at 03:12
  • i am attempting to use approach number 2, however when i try to change the imageButton through java, nothing seems to happen. i am using something like myImageButton.setMaxHeight(boxSize); where boxSize is the getWidth divided by 5. is there a better command for adjusting the imageButton size? – clayton33 Nov 01 '11 at 10:28
  • Yes. I've added the code to the post. I'm so used to it I forgot how long it took me to find out how to set sizes in Java. – Zaid Daghestani Nov 04 '11 at 19:13
0

try

 android:layout_height="wrap_content"

&

<LinearLayout> attribite is android:layout_height="60dp" replace with android:layout_height="wrap_content"

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
0

Set the width of the ImageButtons to fill_parent and use scaletype fitStart for the images that hug the left margin, and fitEnd for the ones on the right. Should do the trick, at least as far as your example image goes. You may have some spacing issues if the proportional width of the images exceed the screen width, but it should work for you.