I did an android application with splashscreen :
splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splashnine">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:textSize="16sp"
android:text="test" />
</RelativeLayout>
where splashnine.png is very clear with 1333x2000 but the picture is not clear when tha app is launched.
First, you should put different versions of this image to different drawable folders, such as drawable-hdpi, drawable-xhdpi, etc. If you just put the image to drawable folder, it's loaded as an mdpi image and then scaled up.
Second, when you set an image as a background, it's scaled up or down depending on the size of the view. It means that the quality of the image becomes worse because of resizing.
Maybe ,since you use "fill_parent" with layout size parameters, image would be resized according to your device screen dimensions. try to change fill_parent to wrap_content or give DP values.
Related
Problem: trying to give multi device support screen for a simple layout but in Nexus 6 its creating the problem,means not showing fit.
It's side is cutting down where as per as docs for nexus-6 resolution is 1440*2560 and image should be in drawable-xxxhdpi .so i keep a image with resolution of 1440*2560.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:id="#+id/imv" />
</RelativeLayout>
if i ll give match_parent so obvious it will stretch the image.
Please help me to get out of it.
Here is screen shots of screen -
http://s10.postimg.org/43od5j5h5/screen_shot_layout.png
Set the width of the Image to fill_parent (you can set margins if you want a bit smaller imageView or you could also just set a fixed width)
Use android:adjustViewBounds to keep aspect ratio
Set height to wrap_content (it will take whatever it takes to maintain aspect ratio)
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/icon"
android:id="#+id/imv"
android:scaleType="fitCenter" />
If you want to fit the image on the imageview, replace android:src with android:background in the ImageView in the xml code.
Try to use these lines of code in the layout
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:scaleType="fitXY"
android:id="#+id/imv" />
I am making an image with my s4 Samsung device which I am trying to put as splash image for my app but I am facing problem that the image is being displayed horizontal and not vertical when I run the app on the device. At first, I put the image in the drawable-hdpi directory and later in drawable-mdpi, drawable-xhdpi and drawable-xxhdpi directories but I am getting the same result.
How can I fix it?
xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background= "#drawable/splash_background"
>
</LinearLayout>
LinearLayout it's for oriented children not for background image. This post enter link description here
can help you
please change the orientation of your image using photoshop/paint or any other image editing software. then replace it with the old image.
Note:
android:orientation="vertical"
is not to change/ rotate the screen!!
orientation property of LinearLayout is defining the alignment of its child components. That means, if you add an ImageView and a TextView, the TextView will be added after the ImageView. Components will be added vertically.
If you need to change the orientation you go through layout for different screens.
and, the drawable-hdpi, mdpi, xxhdpi etc.. are to keep images depending on the device. The device will render images from any of the folder depending on its screen resolution. please read the above link at developer site, it is clear.
This works for me:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fl_draw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/splash_background"
android:scaleType="centerCrop" />
</FrameLayout>
</RelativeLayout>
I created an XML with a relativelayout that has a webview and a relativelayout inside it. What I did is that I load my app on the webview and the app logo below it using the relativeLayout1.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/urlContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/relativeLayout1" />
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#drawable/footerl" >
</RelativeLayout>
</RelativeLayout>
But what happens is that the image is correct on portrait on small devices, but becomes distorted on larger devices or when on landscape mode(Once I rotate it). So what I did was I added different sizes of the image on the /res folder:
res/drawable-hdpi/footer.png
res/drawable-ldpi/footer.png
res/drawable-xdpi/footer.png
res/drawable-xxdpi/footer.png
each of the image has a different size, using the maximum width of every device(320px, 480px, 780px, 1200px)
I also used different layouts:
layout/activity_web.xml
layout-large/activity_web.xml
layout-xlarge/activity_web.xml
layout-xlarge-land/activity_web.xml
Still the logo below is distorted. All of the layouts are in portrait mode except the layout-xlarge-land which is in landscape mode. Should I create landscape modes for layout & layout-large ?
UPDATE
I tried Darnmason's answer by adding an image view to see if it will work even though it is rotated into landscape mode:
but what happens is that the image showing but it's width is similar to portrait mode. Did I miss anything as to why it is not stretching?
Using an image as a background doesn't give you much control over image scaling. I would recommend using an ImageView setting
android:src="#drawable/footer"
android:scaleType="centerCrop"
and maybe you'll need android:adjustViewBounds="true"
This will ensure the image fills the screen, cropping as necessary depending on the aspect ratio of the device. It would still make sense to have a portrait and landscape version but you won't have to care about the slight variances in aspect ratio between devices. Some are more wide-screen than others!
I've got a linear layout with layout_height set to wrap_content, so that the layout height is depending of the number of text lines in it.
The problem (that has driven me crazy all morning) is that I want to set an image as background of the layout, but to be sure the image can be used whatever the layout size, I've made the image quite big in height. The goal is for the image to be cropped in height if the layout is small. But I can't manage to do that: whatever I try, the layout is resizing according to the image height.
So, how can I set an image as background of something, but cropping itself so that it doesn't reisze parent layout (of course, I don't know the parent layout size because it depends on the text inside).
As english is not my first language, hope my question is clear...
Thank you in advance.
Edit: some code sample and screenshots:
This is the original card, without the image in background.
This is the code with the image view added:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/drill_background_img" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
... [Card Content Here]
</LinearLayout>
</FrameLayout
... and the result is my card is stretched to the height of my image. But I don't want that. I want the card the same size. But I cannot reduce image height, because, the card can have more text and be more tall, and so my image will have to be taller.
This is code for a Linear Layout that will be in center and adapt the size of text inside.
I have tested it .
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvInfo"
android:textColor="#ffffff"
android:background="#drawable/toast_frame"
android:gravity="center" />
</LinearLayout>
Give it a try it should work. Here I am using an Image that is small enough to fit the smallest size. Android will automatically strech it. I use 9patch Images for such cases. To know more about 9patch visit this Link from developer.android.com
Simple online tool to generate 9patch
Also check this SO Answer
I have uploded toast_frame.9.png file here so that you can test it with the code I have given above
**EDIT **
change
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/drill_background_img" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
... [Card Content Here]
</LinearLayout>
</FrameLayout>
to
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/drill_background_img">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
... [Card Content Here]
</LinearLayout>
</FrameLayout>
I hope this will work.
I don't think you can crop the image when using it as "background" attribute for any view.
Image will always be stretched to fill the whole background. Normally for background you should use Nine Patches. Get familiar with it and maybe you can change your functionality or UI spec to use them instead of normal images.
The only way I know of cropping the image is in the ImageView and it's image set by "src", not "background".
I would generally avoid using fixed size images in the components that can change size. You also have to remember that in Android almost everything change size when you think about different orientations, screen sizes, screen densities.
I think that background is always resized to fit container size. But you can use FrameLayout or RelativeLayout and ImageView.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
...your content here...
</LinearLayout>
</RelativeLayout>
The code used in all the activites is:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
And the mytitle xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFF0" >
<ImageView
android:id="#+id/mytitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/header_bloodbank2" />
</LinearLayout>
The header in my app doesn't fit well to its dimensions.I've used 'fill_parent' for both width and height attributes. Still, no help.
The header doesn't wrap fully. What may be the reason behind it ? Help me out!
Also attached a snapshot!
From what I can make of your question is that you are trying to use a custom titlebar background Image, which in this case is the "I donate" image".
The only reason I can think of for your image width not being wrapped is that the image is not that wide.(Also, i guess you are using 9png). Now to correct it just move your image to lower density folder. For eg. if your image is in drawable-hdpi then move it from there to drawable-mdpi folder.
Also change your imageview in xml as follows:
<ImageView
android:id="#+id/mytitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="#drawable/header_bloodbank2" />
Your code
<ImageView
android:id="#+id/mytitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/header_bloodbank2" />
should work. Check whether your image doesn't have any transparent border around it.Some times .png images have transparent border which prevent images to fill full width and height.