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)
Related
My question is not about the number of chars nor the textSize attribute in TextView/EditText.
I have a fixed EditText. And the maxLength is 10. And all the chars must be shown at once.
However, each char has different width and height. For example, l and L. If you drag the mouse over(block selection) the l and then the L. You will notice that they have different width.
So, LLLLLLLLLL and llllllllll has different width.
For instance,
LLLLLLLLLL
llllllllll
(both are 10 letters)
Moreover, different font shows different width.
My question is how to get the actual width of chars in PX so that the string won't hidden in the EditText. Let's say, the width of EditText is 200px. And it's limited to 10 letters. And it will show
LLLL(LLLLLL) // (LLLLL) is not shown because of the EditText's width. But you can see when you move the cursor.
llllllllll
How can you get the width of the chars?
you can use TextPaint and measure letters, but there is an easier way for your case. get familiar with em unit
you can declare this value for EditText/TextView by setEms(intNum) or android:ems="intNum". set also width to wrap_content and your View will have width of intNum widest characters (usually Ms). there are also useful minEms and maxEms attributes
edit: HERE you have more complex explanation
PS. don't declare sizes of your Views with px unit due to different densities, use dp. some units explanation in HERE
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 ?
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 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
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.