views:

730

answers:

3

Just a quick question about how you would go about implementing this. I want there to be buttons at the bottom of the screen, but if the screen size is larger, more buttons would be added.

For example, at a small screen size, there might be 4-5 buttons at the bottom, but if you ran it on a tablet or something similar, there would be maybe 20 buttons.

Any suggestions? It can't scroll either, it just has to dynamically fill the layout with buttons.

Thanks.

+3  A: 

To put buttons at the bottom of a layout, do something like this to your layout:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout">
    <LinearLayout
        android:layout_height="wrap_content"
        android:id="@+id/button_layout"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:gravity="center">
        <Button
            android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"></Button>
        <Button
            android:id="@+id/Button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"></Button>
        <Button
            android:id="@+id/Button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3"></Button>
    </LinearLayout>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/button_layout"
        android:id="@+id/content">
        <ListView
            android:id="@+id/ListView01"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"></ListView>
    </FrameLayout>
</RelativeLayout>

To change how many buttons are shown based on the screen size, you should implement separate layouts for Multiple Screen Sizes..

http://developer.android.com/guide/practices/screens_support.html

Tim H
The position isn't my problem. And I don't want to have to set a different layout for every single blessed screen size. With the huge amount of devices on the market, there's no way I could keep it up to date.
GuyNoir
There are only 3 supported screen sizes: 240x320, 320x480 and 480x800. It's not that difficult to support all three correctly :)
Romain Guy
Really? I didn't know that. Are all tablets 480x800? And what about custom builds for specific devices?
GuyNoir
There might be devices with other resolutions but these are not Android branded devices and do not offer the Android Market.
Romain Guy
A: 

Sounds like you want to create your own custom layout class. That or just fill a LinearLayout (for instance) until you run out of screen space.

Romain Guy
Is there a way to tell when I run out of screen space dynamically?
GuyNoir
A: 

If you know the size of your buttons in pixels you could use DisplayMetrics to get the dimensions of the screen then calculate how many buttons will fit in your allotted space.

     DisplayMetrics metrics = new DisplayMetrics();
     getWindowManager().getDefaultDisplay().getMetrics(metrics);

metrics.heightPixels givs absolute height in pixels metrics.ydpi gives exact physical pixels per inch of the screen

and metrics.density gives logical density for scaling purposes

see here: http://developer.android.com/reference/android/util/DisplayMetrics.html

then just do something like

do{
    Button button=new Button(context);
    button.setText("yada yada")
    button.allYoursettings....
    .
    .
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    LinearLayout layout=(LinearLayout) findViewById(R.id.yourlayout);
    layout.addView(button,p);
} while(havespaceleft);
jkhouw1