How do i set background image in Linear Layout so that it could fit every devices and what is the resolution for that image?
Use this link for supporting different screen sizes. You will have to make same image with different resolutions
And to set the image as background,
usage:
android:background:"#drawable/background"
Take this repeatable drawable app_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/your_image"
android:tileMode="repeat" />
And use it like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/app_background"/>
And if you want to scale your_image to all screen resolutions, define it as a 9-patch image: http://developer.android.com/tools/help/draw9patch.html
If you want to set through xml, then you need to do as below:
android:background="#android:color/white"
in case if you decide to use android's default color code or if you have colors specified in colors.xml, then use
android:background="#colors/white"
If you want to do programmatically, then do:
linearlayout.setBackgroundColor(Color.WHITE);
and if you want to set an image
android:background="#drawable/image"
You can keep your image in res/drawable folders
or create a folder inside res main folder which is name drawable-nodpi which is support all dpi device.
and also you can follow Simple Nine-patch Generator
then set background image thus. such as
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher">
Best of luck!
For using a background from drawable folder,
android:background="#drawable/background" >
prepare 4 images and place them in their respective folders.
xhdpi (xlarge screens) 960dp x 720dp
hdpi (large screens) 640dp x 480dp
mdpi (normal screens) 470dp x 320dp
ldpi (small screens) 426dp x 320dp
Related
I have four dimens.xml files in my values folder and I have different values for different dimens.xml files.
for example, in dimens.xml(hdpi) u_video_width is 306dp and dimens.xml(xhdpi)u_video_width is 404dp.but this values not working, because when i run my app in Nexus S (480X800) hdpi model genymotion
This is what my View looks like:
[![This is what my View looks like][1]][1]
As I said in hdpi dimens folder, I have 302dp but I have different width when I run my app (in XML activity file)
this is a my XML code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D53362">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<VideoView
android:layout_width="#dimen/u_video_width"
android:layout_height="#dimen/u_video_height"
android:id="#+id/video_view"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
This is a my dimens xml files
Nexus S has 480X800 resolution and i want to my VideoView to have 302dp in hdpi resolution and another resolution--another dpi how I can solve my problem?
To make same dimension appear the same on higher dpi devices android multiply these dimension with a constant ie
for mdpi it is 1x
hdpi 1.5x
xhdpi 2x
xxhdpi 3x
So in your case it is 306 * 1.5 = 459px
reset of the space (14dp ~35 px) is available on both sides of VideoView
Please refer android developer page
At least this is what I understood. Hope it helps you.
As far as I've understood, it seems it's working right. Your screen is 480px wide and your VideoView is 306dp(in a hdpi screen ~453px), and as it's centered, it remains a bit of margin.
If you want the same dp in another resolution, just remove u_video_width from the other files.
Let's say I have an ImageView like this:
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/pic.png" />
I will have different sizes for pic.png in many folders:
drawable-mdpi: 100*100px
drawable-hdpi: 150*150px
drawable-xhdpi: 200*200px
drawable-xxhdpi: 300*300px
Now, if I have this ImageView
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:src="#drawable/pic.png" />
What sizes should my images in drawable-* be? My ImageView will obviously be matching the screen bounds.
Thanks
You should create images of differents sizes and put them in its corresponding folders, drawable-mdpi, drawable-hdpi etc. Check the article of developer.android. You need to start at a scale, for example xxxhdpi, and from there scale down your images for densities mdpi, hdpi, xhdpi, xxhdpi. Based on the docs, you should follow this:
“So where do you begin when designing for multiple screens? One approach is to work in the base standard (normal size and MDPI) and scale it up or down for the other buckets. Another approach is to start with the device with the largest screen size, and then scale down and figure out the UI compromises you'll need to make on smaller screens.”
So, I have googled a lot, but still cant quiet grasp the concept. I have 3 folders : xhdpi, lhdpi and mhdpi. I do understand the conversion and downscaling to different dip and screen densities. Android selects resources for the right screen type when the application runs.
But, how do I start? I made a background for my application in Photoshop. The background was defined in 720x1080px and exported as an .png file. I put the .png in the xhdpi folder. Everything worked out fine on my Sony Xperia Z, but when a friend loaded it on his Galaxy 3 the background was "to-small" and did not fit his screen. I assume this is because the bacground was to small.
But how large do I need to make it? What px-sizes should xhdpi drawable resources be to fit every single xhdpi screen?
Would a good approach be to start with the largest size, xhdpi? And then convert the drawables down to mdpi and ldpi later on? If so, I need a starter size in px so I can create the background for the application in Photoshop.
Here is how I use the background : (Note, i changed my height to fill_parent it fixed the "not filling the whole screen problem")
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_xhdpi" >
</RelativeLayout>
To make PX sizes scale appropriately you will need to scale using the following ratios:
2X for XDPI
1.5X for HDPI
1X for MDPI (baseline
.75X for LDPI
When you are specifying sizes in your code (or XML), use the DP parameter to specify the value (or SP if you are specifying fonts).
for example: android:layout_marginLeft="12dp"
Here is the docs: http://developer.android.com/guide/practices/screens_support.html, you will notice this image, and a lot of other details describing this.
You can find in this table a briefing of different devices screen densities and resolutions; according to it, Galaxy SIII is xhdpi with a resolution of 1280 X 720. Besides, you could want considerate to follow compatibility mode guide to avoid rendering issues in multiple screens.
I am reading through http://developer.android.com/guide/practices/screens_support.html and trying to understand how pre-scaling works.
I have the following layout file which is optimized for the galaxy tab :
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayoutmain"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/mainmenu"
>
<ImageButton
android:id="#+id/start_button"
android:layout_marginLeft="150dip"
android:layout_marginTop ="70dip"
android:layout_width="500dip"
android:layout_height="214dip"
android:background="#drawable/startsession"
android:contentDescription="#string/descbuttonstart"
/>
I have placed the images for the screen in the drawable-ldpi folder. The problem is that when I test the app on a smaller screen (using the emulator) the screen does not scale properly - the button is massive and not positioned correctly.
Is it possible to just have one layout file for all screens and if so what is the secret to getting this working ?
Thank you.
NO , AFAIK its not possible you need to have three differents folders(hdpi,mdpi,ldpi) with different dimensions for buttons,images etc
and also for layout again you need to have three layout folders (layout-small,layout-medium,layout-large)
Screen size and screen resolution are two independent things. the resources in ldpi folder are for low resolution and not necessory small screen sizes. The way I prefer is to define resources for high resolution and let them scale down on others.
I'm using such layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/logo"
android:orientation="horizontal" >
The drawable/logo.xml:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/login_logo"
android:gravity="center" />
The drawable/login_logo.png has 280 pixels width, my Galaxy Tab screen width is 600px but in launched app I see the image takes almost all screen width and it is about 500px
Where did I make mistake?
SOLVED
By setting
<uses-sdk android:minSdkVersion="4" />
and putting image to drawable-hdpi
As you are saying it is for galaxy tab so it is coming under drawable-hdpi.So change the resolution of image with photoshop or something and put it inside drawable-hdpi.
and try to use android:layout_width="wrap_content". instead of android:layout_width="fil_parent"
You are setting your picture in background, and the android:background tag uses 9 patch images. So your background always stretches to the width and height of the view. Your layout width is set to fill_parent, so the image width is fill_parent too.
The solution could be to make a 9 patch image of the png you are using with .9.png extension.
Read here for drawing 9 patch images - developer.android.com/guide/developing/tools/draw9patch.html
I suggest that you have a good read of the documentation. If you have placed your image in the drawable-mdpi folder, then in high density screens the image will appear to be 1.5x in size. You will have to provide different resolutions of your image for different screen densities.