Multiple screen size support in Android-Studio - android

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.

Related

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 ?

Android "wrap_content" and "minHeight" issue

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!

Android layouts - button size

This is my code for the button. Thing is, it's the same size on all devices. It's big on small ones and small on big ones. How do I alter its size relatively? Here's my code.
<Button
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/blue_gradient"
android:layout_alignRight="#+id/relativeLayout1"
android:layout_alignTop="#+id/relativeLayout1"/>
I would like the size to be consistent, depending upon the device's resolution.
EDIT
I want the buttons to be square.
You are specifying the width and height in dp, or density-independent pixels. This unit of measure is designed to produce the same physical size on all devices regardless of pixel density, because pixel density varies by device. You can also use either of the following values for layout_width and layout_height:
wrap_content makes the view large enough to hold its content.
match_parent uses the corresponding dimension (width or height) of its parent view.
Also with any of these you can stil specify padding and margins, a la the typical box model.
Android currently does not support percentage style measurements, like making the width be 60% of the available width. The closest you can get to that is using a LinearLayout and specifying layout_weight values for its child views.
If you want finer control over the size of a view, you can subclass it and override the onMeasure method, which is where you would calculate the size of the view and set its width and height by calling setMeasuredDimension(w, h). Here's an example of someone doing just that: https://stackoverflow.com/a/3147157/1207921
the best way is to use
android:layout_width="wrap_content"
android:layout_height="wrap_content"
It manages size for all devices very well.
For square buttons you need to set width/height in the code. this will help you.
You might want to try using image buttons with different sized images according to screen resolution (Android, (newbie) different res imagebuttons).
If you use LinearLayout with android:weightSum="2.0" and you set all of your image buttons with android:layout_weight="1" you will have their width/height exactly 50% of the LinearLayout according to the LinearLayout orientation.

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