Making buttons take equal height and width of screen - android

I am currently trying to make a view with four buttons that each take up one quarter of the screen. I know you can use android:layout_weight to set the weight so that the extra space on one axis is filled up, but is there a way to set it so that the height and width are evenly distributed among the four buttons using layout_weight? If not, what is the correct way to do this?

use table layout and create two rows for 4 buttons,determined the height and width
all of these codes are ( XML code)..
should that will working

Related

Make textview take up half of the width of the screen with relativelayout

I have a TextView that I want to have 1/8 of the whole screen, and dp just works to get half of the height but how do I get the 4 TextView's to each share the bottom half of the screen equally?
Right now they share the height equally, but the ones on the left are wider than the ones on the right.
Inside of your RelativeLayout you need to make a horizontal LinearLayout just for these TextView's. Then to make them all take up an equal amount of space, assign an equal weight to them all with weight="1".
If you want a TextView to take up more or less space than the others, just assign a proportionate weight to it. For example if you have 4 views and want the first one to take up 1/2 of the space and the other 3 to share the rest equally, set the weight of the first view to 3 and the others to 1.
You can't with a relative layout- consider a PercentRelativeLayout

Android Layout: Creating a 2x3 grid - trying to avoid nesting LinearLayout with weights

I need to create a 2 by 3 grid, where all the screen space is divided evenly - and I know that nesting linear layouts with weights is bad practice, so I'm looking for an alternative solution.
I need the space to be divided evenly, regardless of screen size - 3 rows with 2 columns each.
From what I understand, GridView/GridLayout/TableLayout will not automatically fill space or divide it evenly - so is there any alternative that will suit my needs?
I know you can set the Layout Params programmatically - is this better practice than using nested LinearLayouts with weights?
Thanks for your help
EDIT: To be clear, I intend to display six images, two in each row with three rows. I simply want to divide the screen space evenly - that is, have three rows of the same height, and two columns in each row of the same width.
EDIT2: This is what I'm using as of now, though I am still interested if someone has a better recommendation. I'm not sure this is better than using nested layout_weight values
In my XML I've weighted 3 LinearLayout containers to each take up 1/3 of the available screen height using layout_weight. (XML has a LinearLayout as it's root element)
Each container as two images as children, for which I'm setting the width programmatically as follows:
//Get appropiate width
DisplayMetrics metrics = con.getResources().getDisplayMetrics();
int width = metrics.widthPixels / 2;
//Set gif views to correct width - half of screen size
teamViewGif1.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif2.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif3.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif4.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif5.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif6.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
Again, I'm still interested if anyone has a better solution or advice
EDIT3: Changed layout to use relative layout instead, and now setting both the images height and width programmatically.

Android- How to display items with variable space between then

I have a horizontal layout to display 5 icons.
I would like to know if there is a way to display the first one always X dp from margin left and the last one X dp from the margin right and the others with the same space between then.
With LinearLayout the space is always the same even when I change the device , so sometimes my horizontal row became much more bigger than the space that the icons use and with RelativeLayout i just can align 3 items in this way ( alignParentLeft, centerInParent, alignParentRight)
With RelativeLayout I think you can easily put three of your icons at the proper place. For the last two icons, I don't think there's something you can do in your xml. Actually I have met the same problem before.
An approach is (maybe not the best one) to calculate the margins of the two icons in your java code. It's really easy to get the parent view width (in pixels) and you know how much space(a percentage) there should be before/after your views. Just a multiplication and addView with LayoutParam is sufficient.
A LinearLayout with android:width="match_parent", android:layout_marginRight and android:layout_marginLeft within a RelativeLayout should work.
The Linear Layout will be xdp from the left and xdp from the right, with the icons spaced evenly inside.
If you use the Linear Layout, you can use the weight attribute to calculate the correct spacing between the images.
Set each of their weights to 1 so that they take an equal space in the row :
android:layout_weight="1"
I hope that can assist you.

Android: how define android:layout_span for height

Suppose there are two Button in TableLayout and if I mention android:layout_span="1" in both the button then the complete width is divided into two part and each button sit into one part.
Can we achieve this same thing for height.
In my application four button is there which arranged in linear order one on the top of other.
I want all these four button cover complete height of the screen in any size of screen.
My Problem is here if I launch this screen on one device then it look like:-
But if I launch this layout on big screen then it looks like:-
How on all the screen the button cover(uniformly distribute)?
Set the layout height for each view to be 0 and instead use layout weight. Use the value 1 for layout weight for all the buttons for them to be equally spaced!

Android ellipse overflow priority

I have 2 textviews per horizontal linearlayout row on two rows. All the views are set as 0dp width and weight 1. They all have the same font and text size etc.
The views on the left side are gravity aligned left and the two on the right are gravity aligned right.
When both textviews text length overflow android always gives precedence to the textview on the right and ellipse the views on the left.
Is there a method that can be used to control which view ellipses when both views on the same row would not fit.
Ideally I want the views on the right to ellipse in favor of those on the left. Or failing that make them ellipse evenly per row.
thanks
i don't think there is a feature of order of the views to manage how they are measured.
you can customize the linearLayout by extending it in order to support this feature , but this is too hardcore for this task .
i would suggest putting the problematic views (those that take too much space and you don't with them to take too much space) into a new layout , and set its width to match_parent .
this way , it should take the rest of the space at the end of the measurements of the other views ,

Categories

Resources