Reputation: 5087
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.
Upvotes: 1
Views: 4196
Reputation: 7350
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);
Upvotes: 0
Reputation: 98501
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.
Upvotes: 0
Reputation: 13543
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
Upvotes: 3