0

I'm finding Android's choice of image resources for buttons based on screen-density to be unhelpful.

I'm developing an app for kids, and so I want it to have nice big obvious buttons, but at the same time I have to limit myself to the available screen real-estate.

Essentially I want to use image sizes appropriate to the screen-size that I have, not to it's density.

For example, lets say I want to fit 5 buttons down the side of the screen in landscape mode. I'd like to do something like this..

  1. Get the height of the screen in PIXELS.
  2. Calculate the height of each button (hButton), allowing for some space in between.
  3. Load the appropriate image resource. I.e. the smallest one that is at least hButton pixels.
  4. Scale the image to be hButton pixels high.

Something like that.

Does Android have a nice way of doing this, or do I have to code that myself? How do I access the appropriate images? (most of my buttons use two images - Up and Down)

Thanks

EDIT:

It seems I wasn't clear enough in my question. I don't need to know how to lay out buttons, and I have a good understanding of how Android chooses images based on density - THAT is the problem.

Consider these two devices -

1/ Galaxy I9000 phone - screen size 4" HDPI

2/ Samsung Tab 3 tablet - screen size 7" HDPI

Because both have the same screen density, both will show images at the same physical size. An icon that is 1" square on one will be 1" square on the other. THAT is the problem. I want the buttons to be proportional to the screen size.

I could try using size specific resources (e.g. drawable-small), but then there will be issues between small devices of different densities, which would then mean creating lots more directories, which seems like a bad idea.

I leaning towards putting a variety of sizes in the no-dpi directory, and then loading as needed.

xtempore
  • 5,260
  • 4
  • 36
  • 43
  • check this http://stackoverflow.com/a/7397829/2143817 – Beginner Sep 19 '13 at 08:59
  • Araki-san's answer below is the correct one. For your ImageButtons you will probably want to set android:scaleType to fitCenter. There's no need to do the calculation in code. – Dave Sep 19 '13 at 11:51

3 Answers3

0
  1. you can ask you button how bit it is

    int hButton = imageButton.getHeight()

  2. then you can get the height of the button in the same way and so some simple school math:

    getHeight()/buttonCount+(buttonCount*padding)

  3. load the biggest image you have got and scale it down to your needs, since you don't know how big it is, unless you map it yourself. By that I mean that you can build something like a utils class that contains a map with the imageNames and their size in px

Daniel Bo
  • 2,518
  • 1
  • 18
  • 29
0

The layout XML in Android is very flexible and powerful. You can layout your buttons dynamically like below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <Button
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:text="Button A"/>

  <Button
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:text="Button A"/>

  <Button
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:text="Button A"/>

  <Button
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:text="Button A"/>

</LinearLayout>

As for image resources, if you think the standard Android image resource selection is not helpful, I'm sure you are misunderstanding something. It is not about the size of buttons. It is about the density of images.

If you are thinking about using simple shapes as buttons, you may want to know about ShapeDrawables.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Yuichi Araki
  • 3,438
  • 1
  • 19
  • 24
-1

for get screen runtime height and width

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

for button height

int buttonHeight = hButton.getHeight():

then get the smallest button height calculate image height and set it as image resource. if u r asking for image resizing code then chek here.

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
DropAndTrap
  • 1,538
  • 16
  • 24