Views taking equal share of Layout - android

I've created a custom dialog builder that contains 2 buttons.
Depending on the dialog's setup, I may choose to hide one of the buttons completely, using Window.GONE.
Ideally what I want to happen is:
1. If there is only one button, then fill the layout with it
2. If there are two buttons, then split up the space in the layout equally with these
Is it possible to do this without having to work out the width of the dialog, the number of buttons and then set the sizes manually?
I was hoping there may be a neater way to perform this

ok, here is how I would do it:
<LinearLayout layout_width:fill_parent layout_height:wrap_content>
<Button
layout_width=fill_parent
layout_height=wrap_content
layout_weight=1/>
<Button
layout_width=fill_parent
layout_height=wrap_content
layout_weight=1/>
</LinearLayout>
The trick is to put both elements a width of fill_parent and a weight of 1. If they are both drawn, they will each take up half the screen. If you use View.Gone, one of them will disappear and the other should take up all the space.

yes of cource put your views means button in linear layout and give yor buttons equal layout_weight will solve your problem.

Related

Leave blank line in Linear Layout

I am developing for android. The app is designed in XML in Linear Layout. What I want to do is to leave some blank lines between the consecutive buttons/text to ensure some neatness in the GUI. Is it posible to do it in Linear Layout? Please help...
You should use margins to seperate different components in your layout. An example of how this would look in your XML is: android:layout_marginTop="12dp"
If you want to leave space outside the border of text/button and next view elements you can use android:layout_marginLeft, android:layout_marginRight, android:layout_marginTop and android:layout_marginBottom.
If you want to leave space between border of the text/button and actual content of the text/button you can use android:paddingLeft, android:paddingRight, android:paddingTop and android:paddingBottom.
For more info check out this question
how to increase the space between the elements in linearlayout in android?

How do I layout a button bar with buttons of different heights?

How do I go about implementing a button bar with buttons of different shapes and heights? As an example (please excuse my poor drawing skills for this quick mockup):
The example bar has 3 buttons, with the middle button (3) a different shape and height than the other 2 buttons (1,2). The button bar will be a view that is included and merged into other views so as to seem to float on top of the parent view.
I was thinking of implementing buttons 1 and 2 into a layout, and then button 3 as another layout that I then merge with the first two button's layout.
like my previous comrades said, you need some kind of layout or container that can have a background (if you wish for button #3 to hoover above it) then use relative layout for mixing the two the disadvantage of this other than complexity is that youcannot relate to the other two buttons since they reside in a different layout.
More elegant solution may be to have a special background drawable that can:
have a method setCurrentHeight() that will specify the height the actual viewable section should have the rest will be filled with transparent color.
override it's own draw so just before it's drawing it will have a callback called, call back you can register yourself to.
then you can register the callback in your activity to take the current position of the #3 button and set the height accordingly, this way you are still with one layout with special drawable as background.
A customized LevelDrawable might do the trick.
I would layout this bar as follows:
A RelativeLayout as a container for the rest, with height set to wrap_content and alignparentbottom = true
An ImageView for the bar
2 Buttons with a transparent background (Button 1 and 2)
Button 3 is a custom Button with a custom Image of a trapezoid as background
So you will have a Layout similar to this:
<RelativeLayout
...>
<ImageView
.../>
<Button
... Button 1 />
<Button
... Button 2 />
<Button
... Button 3 />
</RelativeLayout>
I don't exactly know that this will work, and I can't test it, but you might give something like this a try; I believe it can all be done elegantly through XML.
Have a RelativeLayout (id:mainLayout) that will contain all of your views, wrap_content for both dimensions.
Have a blank View as your first child that will serve as your background bar
Set the View's background color/image to what you want; layout_width to fill_parent; layout_height to wrap_content; layout_alignTop="#id/leftButton"; layout_alignBottom="#id/leftButton".
Add an ImageButton for your center button (id:bigButton), wrap_content for both dimensions; layout_centerInParent="true".
Add an ImageButton for your left button (id:leftButton), wrap_content for both dimensions; layout_toLeftOf="#id/bigButton"; layout_centerInParent="true".
Add an ImageButton for your right button (id:rightButton), wrap_content for both dimensions; layout_toRightOf="#id/bigButton"; layout_centerInParent="true".
In my head, I believe this works, but I could be off. Regardless, something to think about, and I hope it helps you find a solution. :)
Better you can tablelayout with different button styles or relative layout for button "3"

combining wrap_content on parent and fill_parent on child

Setting two or more elements of a linear layout the same height seems to be a great problem.
I want to set four buttons in a row to the same height.
android:layout_height="wrap_content" does it for the moment but when the text on one of the buttons is longer than one line this button is increased and therefore bigger than the other ones. Due to different localisations I don't know, when and which button may have a second line.
So my idea is, to set the parent linearlayout to android:layout_height="wrap_content" and all (!) child heights to android:layout_height="fill_parent".
This works (all buttons have the same size), but I'm not sure if this causes any other problems? Because it the parent gets it's height from the childs and vice-versa.
In theory what you are describing should not work ("Because it the parent gets it's height from the childs and vice-versa".) However, we made it work in LinearLayout because it was a very common use case. I recently added similar support to FrameLayout (this feature should be part of Honeycomb.) What you are doing is therefore perfectly valid and will work just fine.
That doesn't make sense :(
Why don't you use android:singleLine="true" and some ellipsode?

How to make a View to fill the space between two views?

I want to layout my views in the following way: [Button] [SomeView] [Button]. I want to set specific sizes for buttons (in mm), and then have the SomeView fill the remaining space between them.
How to achieve this?
Maybe you could set the layout:weight of the [SomeView] to 1 and put all of these views in a linearlayout. Hope it works!

Is there a way in Android XML to make a row of buttons with widths set to widest button?

Is there a way to declare a row of buttons in XML so that all the buttons have the same width, which is equal to the wrap_content width of the widest button? I'm familiar with the trick of setting all the widths to 0 and assign them all a weight of 1, but that won't work if the parent layout width is set to wrap_content. I don't want to set the parent width to fill_parent because I don't want the buttons stretched more than necessary.
The only way I can think of doing this is in code (either with onMeasure logic in each button that communicates with the other buttons or with a custom layout class).
I think you'd have to do this in code.
Creating a custom layout class would be the way to go. Override onMeasure() and make it look something like this:
Call setLayoutParams on all children to set their layout_widths to WRAP_CONTENT.
Call super.onMeasure()
Iterate child views to find the one with the biggest getMeasuredWidth().
Iterate all other child views calling setLayoutParams() with the widest pixel width.
Call super.onMeasure() again. :)
That should work but I won't stake my reputation on it... happy to help you further if it doesn't.
To get the buttons in a row in the XML you need to add the buttons with in a LinearLayout and change the orietnation to horizontal i.e.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</LinearLayout>
As for getting the widest button and changing all the other buttons to match I am not sure as a guess you would have to have some sort of method within your activity to get the widest button and then programaticaly set all the other buttons to be the same.
Just find out what your widest button is, but it in a view with a horizontal width to match, and then use layout_width="match_parent" or "fill_parent" in < 2.3.
It'll make them all use the width assigned.
If you want to do it programatically, you need to iterate over all the sections, find the max, than iterate again and set it.

Categories

Resources