Android - Fill Layout with Buttons - java

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.

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

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.

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);

Related

Size of ImageViews (with VectorDrawables) in RecyclerView

There is a dialog with a lot of vector images. How to calculate the size of each picture for any screen?
If i use
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ava_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ava_item_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"/>
</FrameLayout>
i get this result
But i need automatically set size for imageviews. How?
In order to get all VectorDrawables of the same maximum size in Vertical Scrolling RecyclerView, you need to install in xml at the root element
android:layout_width="match_parent"
android:layout_height="wrap_content"
I suppose for horizontal scrolling the values of the parameters should be reversed.

Positioning icon half above layout

Recently, designers in my company liked creating half-above elements in layouts for Android apps. I've been struggling for a while trying to make these elements behave as good as possible, but I'm already tired of it. Is there any way of positioning views like on this image
with an assumption that if user touch row/card it became "checked" and this black dot icon became visible (second click makes it invisible of course).
You can do something like this:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toEndOf="#+id/circle_view"
android:background="#android:color/holo_blue_bright">
<android.support.v7.widget.CardView
android:id="#+id/second_view"
android:layout_width="100dp"
android:layout_height="match_parent"/>
</FrameLayout
<View
android:id="#+id/circle_view"
android:layout_width="16dp"
android:layout_height="16dp"
android:background="#android:color/holo_red_dark"
android:layout_marginEnd="-8dp"
android:layout_centerVertical="true"/>
</RelativeLayout>
second_view is endOf circle_view so if circle_view is gone second_view will align to startOf parent.
circle_view should have marginEnd set to negative value of its width divided by two

Different ImageView size in a listView

I have a custom listview with ImageView and EditText , my problem is, with different image the size of imageview is different, like in photo.
How can I make sure that all images have the same size?
Image of listView
This is my XML code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1">
<ImageView
android:id="#+id/imageView1"
android:layout_width="150px"
android:layout_height="150px"
android:adjustViewBounds="true"
android:layout_weight="0.1"
android:scaleType="fitXY"
android:layout_marginBottom="1dp"/>
To deal with different aspect ratio images your best approach is to choose a aspect ratio in your layout and center crop your images so they fill the available space.
<ImageView
android:layout_width="160dp"
android:layout_height="90dp"
android:scaleType="centerCrop" />
Well for better size, create linear layouts for each row containing an Imageview and Edittext.
You have set ScaleType to fitXY and that is good. Now use dp or fill_parent for Imageview size.
(Posted on behalf of the OP).
I have solved this - I have simply remove this android:layout_weight="0.1".

Android Studio: Graphics are cut

I have the Views attached to each other in the following order:
Activity->RelativeLayout->RelativeLayout->Text
When I change the Y of one of the Text attached to the inner RelativeLayout, parts of it are cut.
Here's a screenshot:
http://s28.postimg.org/ypmvrjz25/image.png
I'm coding purely in Java, no XML.
What's going on?
It looks like one layout is overlapping on another because the menu bar is pushing it down. If you just want the text centered I would suggest setting that in the parameters like so.
android:gravity="center"
This places the text in the center if that is what you want.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView12"
android:layout_gravity="center"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
As #Jonathan Whalen suggested you probably have some overlap, I suggest you to change your relative layouts background color to see wich one is overlapping the text. Also try to not use fixed height for the layouts but use wrap_content instead, as fixed dims are frequently the cause of layouts overlap.

spinner gravity

spinner gravity only seems to work in the horizontal dimension. I would like the displayed text to on the top vertically and centered horizontally. Is this possible?
I use an xml spinner normal style like this:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:paddingBottom="5dip"
android:id="#+id/spinnerTarget"
android:gravity="top|center_horizontal"
android:textColor="#ffffff"
/>
you have wrap_content for height, so the view shrinks around the text and there is no space for the text to be positioned on the top of the view. android:gravity works only if there is enough space for the content within the activity. center_horizontal works because you have set fill_parent for the width and I assume the view takes all of the screens width, thus allowing the contents to be centered.
Try rearranging your layout so that there is more space for the view in terms of height.
cheers!
Here is what I ended up, hope i helps
<Spinner
android:id="#+id/m1_ss_spinner"
android:background="#drawable/btn_default_normal_invisible"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_gravity="top"
android:layout_below="#id/m1_ss_head"
/>

Categories