Android background based on screen size - android

I'm creating an Android application, and want different background images for phone sized screens and tablet sized ones. I already have different resolution images for different resolutions (mdpi, hdpi etc.). The challenge is that most tablets use mdpi, but some phones might use it too.
I've tried to do some research, but only it seems to be possible to define screen sizes in the layouts, e.g. res/layout/layout-large. Does that mean I have to create a separate xml for tablets, linking to a #drawable/tablet-background? Or is there any other solutions, either xml or programmatic?

mdpi, hpdi etc. refers to screen density and that's independent from the device being a tablet or a phone. If I understood correctly, what you're trying to do is to use different background images for tablet and for phones.
There are three ways:
Create an xml layout for tablet and an xml layout for phone using layout-large, layout-xlarge folders
Using the same xml layout (except for an element id) in layout and in layout-large folders and change the background programmatically using something like this:
public boolean isTablet(){
return (findViewById(R.id.tablet_view) != null);
}
where tablet_view is an element id present only in the layout-large xml layout.
Another trick: Determine if the device is a smartphone or tablet?

and want different background images for phone sized screens and
tablet sized ones
Try the qualify the drawable folder with the desired screen size and also the density value for the target device, for example drawable-large-mdpi. The screen size must come first based on the configuration qualifiers table.

Related

How do I get specific xml layouts for specific phone screens in Android Studio

So I'm creating different xml files for different phone screens in Android Studio.
I tried a number of methods for creating xml for different screens.
The first one was 4 folders layout-small, large, normal and xlarge it worked for the big tablet screens except for Nexus one of size 480 x 800.
so I created a folder called layout-w320dp and it worked but it overrided my large and xlarge xml files.
As in when ever I click for tablet screens it brings up the w320dp xml file instead of the xml in large and xlarge folders.
So I created the other folders w480dp, w600dp, w720dp but using xml files messes up everything but if I don't use a xml from these folders it assigns the w320dp one to the tablet screens.
My toolbar for phone screens and xml is like this. It's assigning ones I don't want to phone screens. How can I override it and say for example this xml has to match with this specific phone screen size? Right now it's using XML and trying to fit it to a phone size that messes up everything.
Is there a way to assign a specific xml to a specific phone without it doing it for other phones?

How to make layout compatible with different android devices in android studio?

I am designing an app with a .jpeg background and with text views on particular places on the screen. I designed this for my nexus 6 and used dp for the widths and heights of text views and margin-left and margin-top. What my understanding of dp is that it changes with every device according to screen size and density. So it should work on different phones according to the value of dp for them.
But this is not the case. When I run that app on a different phone (nexus 5) the layout is all messed up. I tried using the different resources and qualifiers for different screens (large, small, normal). But the problem still prevails. The app considers both nexus 6 and nexus 5 as large screens. How do i fix this?
this is not the best solution i guess.
i guess your layout is just on the layout folder. i don't know if the first device you tested the layout is large or small but what you should do is create another folder named layout-sw600dp. this folder is used by tablets. the folder layout is used my phones and smaller devices. you could also create the layout-land and layout-sw600dp-land folders for the landscape orientation.
dp do change depending on the device but if your layout gets messed up then i guess the layout called by that device doesn't fit its resolution. i guess dp becomes smaller if the device is bigger and vice versa.

How to make my android game fit to all screen resolution?

I wanted to create a small game on android and then I realize that there is a problem with a screen resolution. I don't know how to make my game's control (Buttons, Textview, etc) will look good to all the screen resolution like (tablet and normal android device) that have different pixel. Like if I design layout for android phone and then if I run it on tablet then buttons, or any controls will look small. So the question is that how to make it (all controls) look more fit and good for any different pixel or resolution device?
You have to create seperate layouts for phablets tablets etc. create 5 folders under res directory.
layout-ldpi
layout-hdpi
layout-mdpi
layout-xdpi
layout-xxdpi
Then copy your file which by default in your layout folder and paste it in a all above folders. Then you have toh manually set the contents for different views . It is a very very boring tedious task

Android devices with different height takes same layout folder

I have a Micromax AQ5000 with Screen Resolution- 1280*720 pixels and Motorola Moto G XT1033 with resolution 720 x 1280 pixels.I have a layout folder named layout-sw360dp which I designed for devices like Samsung s4,s3,Micromax canvas etc but this Motorola device is also using the same layout and this creates the images displayed as distorted in it.
How can I create a folder for the small device(Moto g) I tried layout-xhdpi but it doesn't work how can I name layout with height and width.
Well you are right in some sense android should take layout dependent on different densities but some mobile do not fall under specific density. Therefore android will pick up default layout from layout directory.
to support multiple screen resolution provide different layout for different screen sizes, you can make following directories in res directory like this
layout-hdpi
layout-mdpi
layout-xhdpi
layout-xxhdpi
layout-w320dp-h408dp
layout-w480dp-h800dp
layout-w480dp-h854dp
layout-w720dp-h1280dp
layout-w1080dp-h1920dp
when you provide layout in all this directories you will give multiple screen support for different sizes as well
layout-w1440dp-h2560dp
Use "dip" instead they will help you in debugging your layout as they will try to keep a coherent size to multiple screen resolutions,
<ImageView
android:id="#+id/avtar_animation_11"
android:layout_width="45dip"
android:layout_height="45dip"
android:src="#drawable/avtar011"/>
while supporting multiple screen when you give "dp" to dimensions, Actually android expects you to provide different values for different screen resolution. Lets say below is your imagview dimensions make few folders in res folder in your android project like these below
values-hdpi, values-mdpi, values-ldpi, values-xhdpi, values-xxhdpi
and in them make a dimens.xml file each of them and write
<dimen name="image_view_width">28dp</dimen>
<dimen name="image_view_height">28dp</dimen>
now that i have mentioned "dp" here instead of dip android wants me to keep track for different dimensions for different screen resolution, So i will change the image_view_width and image_view_height values are in separate values folders where dimens.xml is located. Make sure your dp values change as per your screen resolution you want your view to fit in.
<ImageView
android:id="#+id/avtar_animation_11"
android:layout_width="#dimen/image_view_width"
android:layout_height="#dimen/image_view_height"
android:src="#drawable/avtar011"/>
hard part is over now android will pick one of dimens.xml values depending on which screen your app is running, Voila now your layout rocks

How can I fix android tablet's width and height

I am making android application in tablet using fragments so that I arranged 8 buttons vertically in 10.1 tablet it is showing fine but in 7.1 tablet only 5 icons it is showing.
So How can I manage height and width properly in all tablets ... please help
Remember to write different xml layouts (with same name) and place to different folders: layout-large, layout-normal, layout-small and layout-xlarge. Same is with drawables (different size images) and values (different type padding and other values). These folders are for different phone/tablet types with various resolution and screen size. If you do it, then you can easily manage layout for small device and not touch big screen devices. Device is picking xml file automatically so it's good practice to place xml files in different folders for various phones.
More information you can find in http://developer.android.com/guide/practices/screens_support.html
You can use different layout for different screen sizes. You can also add different buttons and other drawable also. Please see the link
Supporting Multiple Screens
You can write different xml layouts using different folders:
For example:
res/layout
res/layout-sw600dp //used by 7''
res/layout-sw720dp //used by 10''
Also you can use the same layout with the same component but with different dimensions.
You can use #dimen/my_dimen notation, and then put your my_dimen in different folders like:
res/values/dimens.xml
res/values-sw600dp/dimens.xml
and so on..
Official doc: http://developer.android.com/guide/practices/screens_support.html

Categories

Resources