Setting views width and height programmatically or through xml - android

I notice that in online tutorials people use specific dp values for width and height of any view
For example, android:layout width ="20dp"
I was wondering since we have so many devices and densities would it be better to determine this value programmatically?
For example I want a specific image to occupy 20% of the screen width then I would get the screen width and multiply by 20% and set width accordingly
U know dp is supposed to make it equal size on any screen no matter what density is but this not the case for many devices and example is galaxy s2 and galaxy note
Can you please enlighten me of my ways are correct?

the better way to do it is to use linear layout in your xmls and set layout_weight in it children with the value you want. You can use weight_sum in the linear layout to set the max weight too.
e.g
linear weight_sum = 100 and a textview inside with layout_weight = 20. it means your textview has 20% of the value of the linear.
p.s: for horizontal orientarion, weight = width and width = 0dp
for vertical, weight = height and height = 0
I hope to help you ^^

For anything that dp doesn't adequately compensate for, you can insert images of different resolutions into your alternate draw able folders. They're broadly named for the different screen sizes your app will come in contact with and android will adjust accordingly by itself. In my experience, I try to do as much graphics as I can by xml as I find it far less cumbersome.

Related

Best practice for defining width & height for Android UI elements

I read in some posts that it's better to define ui elements in xml and not programmatically, and also that I should use dp units inside xml, so I wonder now how can I determine which fixed values should I use ?
Let's say I want to have an ImageView with a width that is 1/3 of the screen's width. Is there an offline way to find the dp number I should specify inside the xml or should I still set the height & width programmatically ?

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.

Making buttons take equal height and width of screen

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

how to make the size of a textview in proportion to the device's physical size

I've been digging around for a while about this issue. In my layout, I have a textview. How do I make the width of my textview's width half of the screen's physical width? Can I do it in the xml layout directly or I should have a function in the java code,which gets the physical size and set the textview's size one half of that?
I don't care abou the resolution.
Thanks in advance!
You should use the layout_wheight attribute for yout TextView:
Android:layout_width="0dp"
Android:layout_weight=1
Use the same property (and same weight) on the layout you want to place on the side of your TextView, they will have both the same size = 1/2 of your physical screen.

Force width when generating dynamic linearlayouts

I'm setting up a layout, that I'm adding to (via a for-loop) based on a set of objects. The issue is if there are say 3 fields (columns), if the middle field has a longer text length in the first entry than the second... the middle field in the first one will consume a larger width, even if I've already defined a weightSum and a layout_weight for that center column. My question is, when dynamically adding rows like this, how can I ensure that they all end up being the exact same width, as defined in the XML?
Why don't you just set a fixed width?
As in setWidth(int width)
EDIT:
The formula to convert px to dpi is:
truePixels = DIPs * (device DPI / 160)
Here take a look how to get screen dpi.
Docs about supporting multiple screens.
If you're using xml layout for rows then:
android:layout_width="0"
If everything is done programmatically then setWidth(0)

Categories

Resources