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.
Related
I want to develop a simple sudoku app. For the layout, I need to have a 9x9 table/grid for the board, and 12 buttons under it and all this must fit in one screen,i had a couple of ideas but there is a problem in each of them
Using GridView and pass a 2d array to the adapter, BUT, the grid is scrollable and the player must see the whole board.
Using TableView, but its not clickable as the grid.
Create 81 button for the board in the xml or programmatically, I think it will be complex.
Is there are any other simpler or more efficient ideas! And if not , which one from the above is better.
There's an option for getting this done using weighted width and height. As far as I know this feature is only available in LinearLayouts.
The main idea I've just explained in this answer.
Please first off read the answer I linked to and then you could use the following approach to laying out your buttons.
Declare a main vertical LinearLayout and set its width and height to match_parent.
Add 9 horizontal LinearLayouts while their width is set to match_parent and their height set to 0dp with layout_weigth equal to 1.
Add 9 buttons (i.e. your cells) to each LinearLayout while their width is set to 0dp and layout_weight set to 1 and their height is set to match_parent.
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.
I'm developing an android calendar app so I need to align the days in a grid-like style. I'm using the API Level 8 so I need to align them by margins. But when switching to Bigger screens the numbers get to the left of screen and do not cover the whole screen.(I know that is because I use dp as a unit for my margin-left). Is there something like CSS % (percent) in Android Layouts?
Try this
android:layout_weight="1"
on each element in that section. It should space evenly.
It's weight attribute for views inside LinearLayout. Here is a good explanation what it means. But you can use it only to set view sizes, not margins. However you can put your view into RelativeLayout, place this layouts to take all available screen width and set attribute centerInParent=true in your view.
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?
I'm trying to layout a set of horizontal and vertical buttons. The problem is that if I indicate 'layout_weight' for buttons, their dimensions don't follow the 'layout_width' & 'layout_height' tags.
Here's what I'm trying to achieve
So, buttons must have identical height & width and distribute evenly horizontally and vertically.
Can anyone suggest a solution please?
Thanks
UPDATE: After a lot of investigation and trying out different solutions, I came to a conclusion that my only option is to create my own custom layout and place buttons correctly there.
That configuration (the L shape) might make this a tad trickier... since the corner button might size like it's horizontal counterparts or it's vertical counterparts, depending on which linear layout they are apart of. In any event, you should be using wrap_content for the height or width, and they should all have a weight of 1. It's my understanding that the wrap_content for the height or width is important for the Linear Layout to size them correctly. It might work with fill_parent when the weights are all 1, but if you use different weights on different Views with fill_parent, things won't appear as you expect.
If you can't make it work with linearlayout and weights (which you should be able to do), you could always try to do some manual workarounds using relativelayout.