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 ?
Related
I want to use this simple layout for items of list:
Note the right gray area (which I draw red rectangle on its border in below) must be a square:
So I have to approximate the height of layout. It must be simple, but the problem is that the text size of TextViews is set by using sp unit and height of layout must be set by dp unit and I did not found a way for converting dp and sp in .xml file. Also I do not want to create a CustomView or change properties of layout at runtime. All things must be done in .xml file. How I can approximate height of layout?
See the TextSize is different from TextView's width and height.
So Provide fixed width-height to all views in "dp". Fixed Text-Size in "sp" that fits perfectly inside TextView.
P.S. -> test in a few devices/emulators to verify.
I want to arrange a set of custom widgets inside a GridView. There are predefined templates which describe a count of elements and arrangement config. Max width and heigh for each of them have to be provided in order to avoid scrolling. How can I get a size of the displayed GridView when computing items size in adapter's "GetView" method?
You have access to few functions in order to get a GridView Height and Width as any other View. Look at the documentation here.
The size of a view is expressed with a width and a height. A view
actually possess two pairs of width and height values.
The first pair is known as measured width and measured height. These
dimensions define how big a view wants to be within its parent (see
Layout for more details.) The measured dimensions can be obtained by
calling getMeasuredWidth() and getMeasuredHeight().
The second pair is simply known as width and height, or sometimes
drawing width and drawing height. These dimensions define the actual
size of the view on screen, at drawing time and after layout. These
values may, but do not have to, be different from the measured width
and height. The width and height can be obtained by calling getWidth()
and getHeight().
Try doing a quick search on their doc next time, even if it's Xamarin, the Android doc is pretty reliable and you only need some adjustment to make it work.
(e.g.) : grivView.Width; instead of gridView.getWidth(); one being a property on Xamarin to follow C# standards, the other is a function.
I am building an Android app with a ListView. Coming from iOS I am used to setting fixed pixel heights for list view items, since the screen sizes of the used devices are always the same. Now for Android, I am wondering what is a good way to dynamically set the heights of ListView items so that it it looks nice on all screen sizes?
In android there are two famous properties. They are:
MATCH_PARENT formerly FILL_PARENT using this property for layout width or height will expand the view to the parents width or height minus margins
WRAP_CONTENT using this property for layout width or height will allow the view to take as much space required or available(if it exceeds screen dimension exception is inside scrollable views)
So for your tag set both width and height to match_parent. And in the custom row that you might be populating set the root layout width to match_parent and height to wrap_content.
Note: in android while we give fixed height at times but it is generally not a good practice.
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.
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)