Equal Button Widths In A View - android

I'm not doing too bad with layouts at the moment but there is just one piece I can't seem to do with ease. Imagine if you will two, or three buttons next to each other and I want them all to be the same size (width) relative to the width of the screen. e.g if the screen was 300 pixels each one would be 100 pixels wide (Assuming there is no boarder etc). A good example of this would be in Excel, I would select three columns next to each other and hit "Distribute cell widths evenly"
So, my question. With a RelativeLayout is there any easy way to position two or more buttons next to each other and have their width automatically adjusted so the widths are the same regardless of text or images.
I could use code to get the screen width and manually setup the button's width's but I'd rather the Android OS did it automatically.

How does one create Buttons with Equal Widths?

Related

loading an image, is the width/height pixel count maintained

I have read all about supporting different screens and I understand the concept of Density Pixels (DP). However one thing I am not sure about. Lets say I have an image file which is 200 by 200 pixels and I loaded it using xml with wrap_content attributes at the top left of the screen (0,0). Now let's say I want to place a textview programmatically beside it.
If I put the text view at coordinates (250,0). Does that mean that the text view will never overlap the image and will always be to the right no matter what the device density/screen size is.
I understand the distance ( gap) will be different but I am hoping my theory of not overlapping will hold.
I tried it on 2 device and no issues but I am not sure if this is coincendence
Thanks
If you are using px unit it will always remain the same. But if you are using dp unit instead of pixel you cannot definitely be sure.
By the way you should use toRightOf property instead of this approach if you only want this.

android layout - position ImageButtons in equal quadrants of the screen and size them to proportion

I am creating an android app and I am having some trouble with the XML file. What I want is four ImageButtons displayed in the center of four quadrants of the layout (so one in each quadrant). I also want thees ImageButtons to be sized by a percent of the screen (so the button would be bigger on a bigger screen and smaller on a smaller screen) but to a maximum of a specific size.
description of what I have in the layout that works:
The layout that contains thees buttons takes up 70% of the screen height and maximum width (there is another layout in the top 30%) and the screen is locked in vertical orientation. so I'm only looking to complete this quadrant ImageButton style of view.
my attempts to accomplish this was:
1) grid layout: this wrapped my buttons up and they did not take up the whole screen or one quadrant filled up the whole screen and the other three quadrants were not visible.
2) layout dimension percent: several linear layouts positioned in vertical and horizontal orientation and using the layout_hight=0dp (or width), layout_weight="0.50" "trick" to position the quadrants out. This worked nicely but there is a warning i get that the layouts are inefficient when you use a percentage to size a layout within a layout that was position with a percentage, and the ImageButtons did not want to stop at a maximum size completely ignored maxHight & maxWidth (i did have adjustViewBounds="true").
3) I can make all this work easily by calculating sizes and positioning everything by code but I would really like to do this in the xml file and leave that as a last resort.
I would appreciate any help, even a push in the right direction would be grate. I have been stuck on this for a while thank you.
Just do it programatically. It's much simpler as xml is very static and java is dynamic and in complex situations easier to use. Save yourself the trouble.

How to keep android device's layout responsive

I am new to xml and android and I was designing a simple application that has 3 buttons in two rows. Now I can't get to a way to make the layout responsive (adjust according to the screen resolution) for every android device. I did a little search but couldn't find anything relative.
So my first question is can you create a UI with xml that works with all sizes of screen?
If yes then how to do it? Or at-least please point me in the right direction.
Thanks
As user Radhe has suggested, android:layout_weight used appropriately within a LinearLayout will easily scale the buttons. If you have the buttons lined up horizontally within a LinearLayout, set each button's height to whatever you'd like (I prefer match_parent) and set each button's width to 0px. For each button, you may set a layout_weight. The weight of each button represents the ratio of the width of each button to every other within that LinearLayout. So if you have 3 buttons, each with a width of 1, the will all be the same width. If one button has a width of 2 and the other two have a width of 1 each, the one with the width of 2 will be as wide as the other two combined. Just play around with it until you get what you like.

Arranging Components in App Inventor?

Is there a more precise way of arranging components in App Inventor than using the Vertical/Horizontal/Table Arrangement formatting elements?
I want to sparsely position about six buttons across my app screen - all different sizes.
Thank you in advance.
the short answer is: no
well, additionally you also could use empty labels as delimiter between components...
another answer is: you could use a canvas and sprite components instead of buttons. You can define sprite positions exactly at x/y coordinates of your canvas.
However keep in mind, that there are different Android devices with different screens sizes and resolutions, so normally you wouldn't set buttons exactly at x/y coordinates to avoid strange layouts for e.g. smaller or larger devices.
The bit longer answer is to use labels as spacers.
Example:
Need to center a button at the top of the screen.
Add horizontal layout with 2 label texts and button in center inside of the layout.
Click each text label and remove the actual text from right side properties menu and choose fill parent width and height.
This centers the button because the layout automatically assigns one third size to each.
Labels are the best, but longer coding answer.

Which layout is suitable for all Android mobile?

I am developing one application, right now i am on designing phase. i design one screen
on the 3.7WVGA(Nexus One) screen in eclipse using Linear Layout. but when i test it on 2.7
my some icon are go outside of the screen. my question is that which layout is suitable for all screen whether i design it in 3.7 inch or run it on 2.7.
Please give me a suggestion.
Thanks in Advance.
1st i design it in 3.7 and second in 2.7.
Don't ever, ever, ever design a screen for Android based on an actual screen size. You will always screw yourself up because there are a hundred different screens out there. What looks good on one phone will look like crap on another. That being said, here are some tips:
Use RelativeLayout to lay your button contents out. Once you understand the model it's much easier than you suspect and it will make it easy to automatically scale things.
Only use actual pixel sizes for things that "float". You never want to specify the width of something and try to fill the width of the screen.
Include multiple resolutions of your images. Let the system pick the right resolution for you.
A table/grid layout will make things easier for you on the overall design.
Big panels of buttons are played out. There are other UI options at your disposal (menu buttons, swiping left and right through screens, etc.). When users see a field of buttons it looks like the app was slapped together.
For that kind of layout use GridView if you want it scrollable, or a simple RelativeLayout if you want all the elements to scale depending on the size of the screen (use toRightOf, toLeftOf, above, below and weight to achieve that)
You need to consider the guide provided by android
Multiple Screen Support
What you will do is to provide all screens icon regarding different screens and you can also specify layouts for different screens, for example you want to provide drawables and layout for multiple screens, you will provide resources in that specific folder + below suffix.
Screens for layouts for drawables
ldpi layout-small drawable-ldpi
mdpi layout drawable-mdpi
hdpi layout-large drawable-hdpi
xhdpi layout-xlarge drawable-xhdpi
This topic will be more relavent to your need.
The following are the view groups in android. you can use any of these as per your requirement. But in your case You can use GridView
View Groups in android
FrameLayout Layout that acts as a view frame to display a single
object.
Gallery A horizontal scrolling display of images, from a bound
list.
GridView Displays a scrolling grid of m columns and n rows.
LinearLayout A layout that organizes its children into a single
horizontal or vertical row. It creates a scrollbar if the length of
the window exceeds the length of the screen.
ListView Displays a scrolling single column list.
RelativeLayout Enables you to specify the location of child objects
relative to each other (child A to the left of child B) or to the
parent (aligned to the top of the parent).
ScrollView A vertically scrolling column of elements.
Spinner Displays a single item at a time from a bound list, inside
a one-row textbox. Rather like a one-row listbox that can scroll either horizontally or vertically.
SurfaceView Provides direct access to a dedicated drawing surface.
It can hold child views layered on top of the surface, but is intended for applications that need to draw pixels, rather than using widgets.
TabHost Provides a tab selection list that monitors clicks and
enables theapplication to change the screen whenever a tab is clicked.
TableLayout A tabular layout with an arbitrary number of rows and
columns, each cell holding the widget of your choice. The rows
resize to fit the largest column. The cell borders are not visible.
ViewFlipper A list that displays one item at a time, inside a
one-row textbox. It can be set to swap items at timed intervals,
like a slide show.
ViewSwitcher Same as ViewFlipper.
I have the same problem but i found a very simple solution is use dp and sp instead px. You may read this:
What is the difference between "px", "dp", "dip" and "sp" on Android?
And you may create icons with different resolution and put it in suitble folder.
create a table layout and every row contains a vertical linear layout put all buttons inside and provides weight to each button according to need,at the last put this table layout to ScrollView that is suitable for all android devices
I created a tool that allows you to scale/adjust your layouts for tablets and small screen devices and made a blog post about it here: http://onemanmobile.blogspot.com/2012/04/how-to-scale-your-android-layouts-to.html
Basically, defining your layouts in dp units for one size is not enough if you want your app to fit on all devices and tablets, since there's four different "density-buckets". This tool will allow your layouts to be converted into fitting these density buckets.
It also explains in further detail how to make more flexible layouts for all resolutions.

Categories

Resources