Android "wrap_content" and "minHeight" issue - android

I am having some difficulties setting up three simple buttons, created and exported in .png format. Their size is about 160x60px. In the activity_main.xml I have set for the height and width: "wrap_content" and created for each resolution (ldpi, mdpi...) the corresponding button sizes. As I view the Graphic Layout or test the app my buttons are stretched in height.
I figured out that in the "Property Inspector" the default minHeight and minWidth are set automatically to 48x64 dp, and I can't delete these values...I even tried to enter again "wrap_content" but it gives me an error. For now I have modified the "minHeight" to 40dp and my buttons are ok, but I am worried about screen compatibly and about having issues in the future on other different devices.
How to solve this properly?
Thanks!

Related

Multiple screen size support in Android-Studio

I have a project when I tried to make app supporting multiple screen size it didn't work the size looks very large in some mobiles and very small in others. So I made a test project I used Smallest screen width I made dimens.xml file for (320dp, 480dp, 600dp, 720dp) and the same for activity_main.xml, I put textView in every xml file like shown in picture to know which file the mobile will read from. the problem is that I have tested it on about 10 mobiles from different screen sizes and versions but all are reading from (320dp) what am I doing wrong?
To ensure that your layout is flexible and adapts to different screen sizes, you should use "wrap_content" and "match_parent" for the width and height of most view components, instead of hard-coded sizes.
"wrap_content" tells the view to set its size to whatever is necessary to fit the content within that view.
"match_parent" makes the view expand to as much as possible within the parent view.
FOR EXAMPLE:
android:layout_width="match_parent"
android:layout_height="wrap_content"
AND
android:text="" must be hard-coded or resource of string.
Thanks.

Why layout is disturbing even using width in dp Android

I have created a demo application in Android using Nexus 9 2048 X 1536
I have added an EditText and assign width as 1000dp it is working fine in Nexus 9 Emulator. But When I change the Emulator as Nexus 5X 1080 X 1920 to test the dp purpose, Then the EditText goes out of the layout and shown about half of it.
Why it is happening even I assign the width in dp. It should adjust the width by screen-resolution.
It should adjust the width by screen-resolution.
No, it does not. dp will adjust the width by screen density, not screen resolution. You asked for it to be 1000dp wide, and so it will be 1000dp wide, regardless of screen resolution or screen size.
You have a few alternatives:
1) The right and recommended one is to make different layouts for different screens. For this alternative see Supporting multiple screens
2) Another alternative I used to use before ConstraintLayout was to make everything proportional. When the app starts I take the meassurement of the Display and layout everything based on that. This is done mostly programatically not using the xml layout file.
3) Use the new ConstraintLayout from the support library. Using this layout you can constraint your widgest relative to each other and or to guidelines you can add to your layout to delimit sections of it. Your widgets that need to adapt will mostly have a width of 0dp or wrap_content and the constraints will take care of resizig them accordingly.
Using ConstraintLayout

How to set the layout_width = screen_width ? And how to manage good size for image compared to the device?

I already read a lot of threads on stackOverflow but I didn't find what I was searching for. I already read too the official documentation but I steel have some questions.
I'm trying to insert some image in my application and I'm trying to make the layout_width to the image equals to the width screen size (in portrait).
So I make many different size for the same image and I put them in
drawable-ldpi, drawable-hdpi ....
But when I'm trying my image on the emulator (hdpi) it works fine, but when I'm trying this on my tablet (hdpi too) the image don't get all the width screen.
I would like to know if I have to do multiple drawable about the screen size ?
and so then what about the drawable about the dpi ?
I know it's possible to put some vector image but I would like to know if I can choose the layout_width like this: android:layout_width = screen width.
and why is it so important to set the density ? A big tablet can be in mdpi et a phone on hdpi... what is so important in the density ?
Try using android:layout_width = "match_parent" in your layout.xml this will set the width of your main layout or parent layout to stretch from one side of screen to other side of screen. setting android:layout_height = "match_parent" and android:layout_width = "match_parent" will spread the layout over whole screen (top to bottom and left to right).
AND TO SAVE TIME AND EFFORT of creating different sizes of same image and icons,
you can use Android-drawable-importer plugin. With this plugin, import just 1 image and it will create different sizes (small,large,xlarge,xxlarge etc) automatically by it self.
Thank's for answer. I'll try to use this extension and see.
Edit:
It works fine but it make different image size only for the resolution (hdpi,mdpi ...) but it can't make different image size for the screen size ? Maybe I didn't see where I have to go ...
Do you know what I have to do ?
And I have a new questions: What is the vector image extension I must use in my projects ? I have .EPS and .AI but the plug in don't see theme in the search folder window. Do you know what is the step who can resolve it ?

Wrap content vs setting dp

This DP measure is pretty confusing, I'm trying to learn when should I use wrap_content and when should I set the height, when using ImageView.
My current issue is that I'm using wrap content on 3 images (and yes I have different resources for each screen size: mdpi,hdpi,xhdpi) BUT in some devices (both hdpi) since they have different width pixel size (480px for one and 590px for the other), in the 480px one, one of the images looks smaller cause their size is calculated cause of the wrap_content.
I could make my designer re-make all the images for the hdpi size, but I want to know when to use wrap_content and when to set DP size myself.
DP is just a unit of measure that normalizes for different screen pixel densities, which means a value like 50dp always has the same physical size no matter what device you run your app on.
As far as actually designing your layouts, you should almost always use either wrap_content or match_parent instead of setting hard numbers for width and height. Exceptions usually come about when you use layout_weight for children of a LinearLayout for doing proportional sizes, or when using the various layout anchors for children of a RelativeLayout.

Screen size supporting from xml?

Supporting multiple screen is most important thing is android i think.We are doing some view different size.I know it is basic but i am curious about that.
For example i do a Edittext and i give width 200 dip for 480X800 screen.When i run this project 240X320 screen it will seen too big.We can fix this problem image with the drawable folder but how can we fix it view this problem?I know we can give wrap_content but when we give dip it is bad? When we give dip what is best effort from scale?
THX.
Just like with drawables, you can create different folders with layouts for different screen sizes.
As long as you keep the names of the files consistent across the folders, Android will manage picking the right one automatically.
Just create folders such as:
res/layout-small
res/layout-medium
res/layout-large
res/layout-small-land (incase you want landscape mode as well)
However, this does mean you will need to manually make adjustments for all screen sizes. Like in your example of the EditText, you will need to manually set the size to 100dp for example in the small screen layout.
The best way is to use wrap_content, fill_parent.
You can see layout_weight parameter of linearlayout where you can give value in %. But remember to give ur height or width as 0dp when you are using layout_weight.

Categories

Resources