Question about DP in Android - android

Let's say I prefer using "dp" rather than "fill_parent" or "wrap_content", what would be the appropriate number to use in order to fill the screen with a picture?

This depends of the screen and of its density. Check this article to learn more about that.
(This may lead you to reconsider using fill_parent / wrap_content by the way).

Why would you "prefer" this? Precise dp values for layout_width/layout_height are one tool alongside match_parent and wrap_content and building a nice UI will usually involve all three. Which one is appropriate for each dimension of any given View is situational and match_parent's entire reason for existence is to match the size of a View's container, in your case the screen.
(* match_parent is the newer name for fill_parent.)

Related

How to position images at the same spot all android devices?

I have to position the images on my layout such that it does not change its position with varying screen resolutions. They should be exactly where they are placed. layout_weight attribute can be used with linear layout. But I am using relative layout and I am dealing with cropped images. Any solution on how to position the image views? Setting out margin height and width is not worth.
In short - you can't. Devices have different resolutions as well as screen sizes ratio. Thus it's impossible for the app to look same because there is no same definition in Android. The only way to design an interface - is to use bindings to other objects or parent view's borders. Try to investigate what are the rules, e.g. "to the bottom of the picture with 10dp gap" or "next to the TextView" - and it's the constants that are kept on every device.
I can't imagine a scenario where this makes sense but it obviously must exist since the Android team built support for it. The docs on the dimension type clearly state that it supports "mm" and "in" as qualifiers for millimetres and inches respectively.
That should get you what you need but I would advise reevaluating that requirement, as it would make design a massive pain.

Is it possible to set my layout to be square and proportional to one of the two dimensions via XML?

I don't think this can be done, but I'm going to ask just in case.
I have a situation where I want a WebView to be square. I need it to be 80% of the smaller of the two available dimensions and then for the other dimension to be the same length.
So, for example if the user is holding the device in portrait mode, I could set the parent layout weightSum to 10, set the WebView's horizontal layout_weight to 8 and then have it automatically size the height to whatever the width winds up being.
I know how to do this at runtime in Java, I'm wondering if I'm possibly overlooking some fancy-schmancy XML technique that would accomplish the same effect.
It works nicely with Images (using adjustViewBounds and scaleType) but those params aren't visible to Layouts.
Can't be done in SDK XML.
You have to use code, but IMHO rather than doing it via setLayoutParams() at the fragment/activity level you should subclass WebView (or it's container) and implement your special layout requirements in onMeasure().

android set button height [duplicate]

I'm using several buttons in my app, but both layout_width/height "wrap_content" and "fill_parent" looks weird. The former being to small and the latter too large - both looks weird, and the former is not easy to hit with your finger.
How should I size buttons? Is it typical to define their sizes in dip? Or should I use "fill_parent" with a padding? Buttons looks weird in my app, not so in others.
That is difficult to answer in the abstract. Here are some techniques to consider:
Use android:padding="4dip" (or some other value) to make a wrap_content Button a bit bigger
Use android:textSize on the Button to make the content bigger (use some size in scaled pixels, or sp)
If you want the buttons to fill the space but divide it among themselves, use a LinearLayout, give each button a height (or width, depending if column or row) of 0px, then use android:layout_weight to allocate space between them on a percentage basis. Here is a sample project outlining this technique.
I think it is better to use fill_parent with a padding/margin instead an exact width value. So you are more flexible when the size of the parent view changes.

Android: Adjust sizes programmatically according to screen size

Is it a bad habit to get the width and hight of the device and set images/button sizes programmatically accordingly.
I find it inaccurate to use different folders for layouts and densities as it gives me wierd results on some devices (on top of the inacurancies)
Your experience is appreciated.
Thank you
Yes it is very wired thing to make the layout for the all supported screen of android. And there are lots of screen resolution available in market.
Once i have made a Demo and it Works for me. I have made one Button which height and Width is same. Now i have set its required height and width as per the one Screen in which it is looking perfect.
After that i have calculated the pixel that it required to make it Possible in that screen and based on that i have applied it to all screen.
Now it works great in all device with any density and resolution.
So if there is any view that generate at run time and you want to set its height and width then the best way is to calculate its height-width ratio and use it.
hope it will helps you.
Enjoy Coding. . . .
:)
Well, most of cases you will have layouts which are, or will become, complex, and it will be difficult to calculate the positions programmatically.
And it will be also a disadvantage mantaining it, because you will not be able to use the interface stuff (grafic layout and so on), and other people, or yourself, will not understand the calculations the same way they would if they see the views in XML. Reorganizing, changing somewhere a position could be painful.
You also will be working frequently with bitmaps, which have a fixed size, if you calculate the dimensions programmatically and stretch they will not look good. At least you would need different set of bitmaps and load accordingly.
It helps if you for example use relative layouts with rules (like above of / align at the bottom of the parent, etc), linear layouts with weights, and dip (density independent pixels). You should do programmatic layout only when it's not possible in other way. Or in some certain cases where it really-really makes things easier.

Android: How to size buttons?

I'm using several buttons in my app, but both layout_width/height "wrap_content" and "fill_parent" looks weird. The former being to small and the latter too large - both looks weird, and the former is not easy to hit with your finger.
How should I size buttons? Is it typical to define their sizes in dip? Or should I use "fill_parent" with a padding? Buttons looks weird in my app, not so in others.
That is difficult to answer in the abstract. Here are some techniques to consider:
Use android:padding="4dip" (or some other value) to make a wrap_content Button a bit bigger
Use android:textSize on the Button to make the content bigger (use some size in scaled pixels, or sp)
If you want the buttons to fill the space but divide it among themselves, use a LinearLayout, give each button a height (or width, depending if column or row) of 0px, then use android:layout_weight to allocate space between them on a percentage basis. Here is a sample project outlining this technique.
I think it is better to use fill_parent with a padding/margin instead an exact width value. So you are more flexible when the size of the parent view changes.

Categories

Resources