I have developed an android application. I used android:background property to make an image as background. But when it is displayed on square displays, the image keeps stretching.
How to make Android Background fit the screen without any stretching ?
I have seen many apps with images or videos that are non stretched , but fills the entire screen of the android phone.
My xml code is given below
<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" tools:context=".MainActivity"
android:background="#drawable/Splash" >
</RelativeLayout>
You can try and use an ImageView rather than setting it to the background of the RelativeLayout
I have modified your code to look like this
<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" tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/Splash"/>
</RelativeLayout>
Only problem with this solution is that the image might be cut off at parts on some devices. To solve this issue you can create a few images with different aspect ratios for relevant devices and put them into the relevant drawable-xxxx folders.
Also have a look at this thread for other solutions that might suit your requirements better.
you need severalimage for different displays
Supporting Multiple Screens
In Android to support multiple screen you must have to add different images according to screen resolution and screen size.
For more details just visit these links:
Drawable folders in res folder?
http://developer.android.com/guide/practices/screens_support.html
You could use a FrameLayout and put ImageView as background
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgPlaylistItemBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:maxHeight="0dp"
android:scaleType="fitXY"
android:src="#drawable/img_dsh" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
Related
I have designed a splash screen in Adobe XD as shown:
enter image description here
Now when I exported the parts of the image to android studio, I was unable to make all the images stick to the bottom as in XD. enter image description here
Can someone tell me how I would go about doing that and would be the best ViewGroup for such task?
Here is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/group_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</LinearLayout>
It can be done with either ViewGroup. But the problem is not the the view group - the problem is that you have a single image for the whole content - it is bad practice.
There should be at least 2 images - one for the background and the other is for the content.
It is due to fragmentation issue regarding device screens - there too many different resolution, aspect ratios and dpis to make the single image work properly on them all.
I will show you an example based on your UI with a background and logo image with small images below logo. You just need to separate it into two files - background could be jpg but logo must be png with transparency. In case you will do it - here is how your layout should look like.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/bg" />
<ImageView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:padding="24dp"
android:src="#drawable/logo" />
</FrameLayout>
You may want to experiment with android:padding and android:scaleType to achieve the best result.
Edit
Changed android:layout_height="match_parent" to android:layout_height="wrap_content" - it will work better this way
Hope it helps.
I'm trying to make a textureview keep the 16:9 ratio when layout changes.
I use percentrelativelayout to implement it.
Also I need the textureview not exceed the screen size both on width/height. So I use 2 layers to make sure it won't exceed the screen.
It resize perfectly on layout preview in android studio. But the width isn't changing to keep the ratio in runtime on the device.
Here's the layout xml and the screenshot.
Have someone met the same question?
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<android.support.percent.PercentRelativeLayout
android:id="#+id/percent"
app:layout_aspectRatio="178%"
app:layout_widthPercent="100%"
android:background="#android:color/darker_gray">
<TextureView
android:id="#+id/texture"
app:layout_aspectRatio="178%"
app:layout_heightPercent="100%"
android:layout_centerInParent="true">
</TextureView>
</android.support.percent.PercentRelativeLayout>
</android.support.percent.PercentRelativeLayout>
One layout in the bottom is set to VISIBLE
After bottom layout set to GONE
I had to play around with these PercentRelativeLayouts to get the result I was looking for too. Below is the code I used to get the same result you are looking for except I used a fragment. I've tested this on multiple devices and it worked on all of their different screen sizes. In your case, I would suggest replacing my "article_preview_frame" FrameLayout with your TextureView and see where that gets you. Make sure that you embed your PercentRelativeLayout inside a regular RelativeLayout.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/article_preview_relative_layout"
>
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/article_preview_container">
<FrameLayout
app:layout_widthPercent="100%"
app:layout_aspectRatio="178%"
android:layout_alignParentTop="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/article_preview_frame"
/>
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'm just starting android programming and so I'm really noob!
I'm trying to set a plaid image as a background and I want to prevent from shuffling the squares in background and becoming rectangle when phone rotates!
can you come up with a solution to my problem!? preferably handling in XML file! tnx
This is what you are asking for:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/background_image"
android:contentDescription="#string/text" />
</FrameLayout>
<!-- rest of your layout !-->
</RelativeLayout>
just make sure that your image for background is big enough to fill big screen sizes.
I have 2 images main_bg and a transparent image ship. Ship is cropped from main_bg and I want to place cropped image ship on main_bg on the exact position where ship appears on main_bg. When I use
android:layout_width="match_parent"
android:layout_height="match_parent"
ship appear as an elongated one. When I use "wrap_content" instead of "match_parent",
it appear as a small image. How can I set ship on main_bg with its exact width and height.
This is my xml code.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/main_bg"
android:gravity="left"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/ship"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ship"
android:orientation="vertical">
</LinearLayout>
How can I place it properly?
You should try using FrameLayouts to superimpose one image over the other. Like:
FrameLayout
Image
FrameLayout
Image
And set the dimensions of the FrameLayouts as you want to.
You can use two FrameLayout and do something like this :
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/main_bg" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ship" >
</FrameLayout>
</FrameLayout>
use LayeredImageView from this post How to maintain multi layers of ImageViews and keep their aspect ratio based on the largest one?, see the example with a flag how to place layers