How to set LinearLayout background image to fill the screen - android

I have a LinearLayout with a background image set and I want the image to keep its aspect ratio because now it gets modified by the screen size. In xcode you can set the imageView property Aspect fill but in eclipse I can't find a solution for this problem.. This is my LinearLayout xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bgapp"
android:scaleType="centerCrop" >

For this case you have a few drawable folders in which you need to keep images that corresponds to the respective device dimensions.
i.e.
HDPI - have a different resolution image
MDPI,XHDPI,XXHDPI are various types of dpi's used
for further info here is the link where u can get the exact dimensions:
http://developer.android.com/guide/practices/screens_support.html

I think android:scaleType="fitXY"
solve your issue . Kindly Try once.

Linear Layout should be like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bgapp" >
I have same issue before five days, I have resolved it by adding fill_parent on width and height, I was surprised when I have resolved my issue.
Hope it will help you.

Related

Imageview left and right white space issue

I'm trying to set 1080*1920 image in imageview but it showing white space left and right side. It showing same in all device having resolution of 480*800,720*1280,768*1280,1080*1920,1440*2560. Please check attached image Also, i tried to set android:adjustViewBounds="true" but it didn't work if i set android:scaleType="fitXY" image is streched. Please help me with some solution as i'm trying all possible way but not worked till now.
This is xml layout code :
<?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">
<ImageView
android:id="#+id/img_gallery1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/atest"
android:scaleType="fitXY" --> iF i use fitXY then image is streched
android:visibility="visible" />
</RelativeLayout>
Try:
android:layout_width="wrap_content"
or:
android:layout_width="match_parent"
also make sure whichever container you have the imageview inside is able to fill the full screen.
The resolution you mentioned i.e 1920x1080 is ideally a landscape resolution.
So if you intend to put the image in portrait mode, then try changing the resolution of image to 1080x1920.
Also, as Philip said, make sure the imageview's container is able to fill the screen.

Android - Put a button over an ImageView to the exact spot

I have an ImageView (human body) and buttons over this image.
Is it possible somehow easily create this in RelativeLayout for the most different screen sizes? Or should I create different layouts for different screen sizes?
How would you proceed? What do you recommend?
Have a look at the percent library.
It would be perfect for something like this, and would work for every device. You'd have to work out the percentage width, height, and margin for each PercentRelativeLayout relative to your person image.
Here's an example:
<android.support.percent.PercentRelativeLayout 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">
<View
app:layout_widthPercent="25%"
android:layout_height="100dp"
app:layout_marginLeftPercent="5%"
android:background="#ff0000" />
</android.support.percent.PercentRelativeLayout>
and remember to
compile 'com.android.support:percent:23.0.0'
This is an example for an imageview with clickeable areas.
https://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/
I hope it will helps to you.
Yes it is possible with Relative layout but its difficult.
for this you have to focus on
different images with perfect cutting.
put images in all drawable.
Create Relative layout and starting with image-view at one side to end side as reference wise.
Margin add in different dimen.xml

How to put an ImageView relative to a background image in Android Studio

For my GUI i use the RelativeLayout and want to keep it as it is the best for different screeen resolutions.
For example i have this Background, where i want to put an ImageView exactly over the TV-screen (to show some pictures on the TV).
I want this to work in different screen resolutions.
How can i achieve this? Or is this even possible? Examples are welcome :)
My Background Image:
If I were you, I would separate the TV image from the background and make it a new ImageView. Then you can set the position of TV-Screen ImageView regarding to the TV ImageView.
Well i think you can try 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">
<RelativeLayout
android:id="#+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher"></RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:src="#drawable/ic_launcher"
android:layout_alignTop="#+id/back"
android:layout_alignRight="#+id/back"/>
</RelativeLayout>
But i think there could be an issue with different resolution try this and let me know if it worked.
You can have your TV as a PNG. Carve out the screen from this image. This will make the TV image TRANSPARENT for the screen(red outlined portion)
Next, your images/screen-slides must be exactly same as the TV image in terms of width and height. However, the actual content of the image/screen-slides must only be in the hollow portion.
I think i figured out how to do this.
At first i changed the background-picture from a Layout-Background to an ImageView:
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageView"
android:src="#drawable/background1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:scaleType="fitXY" />
Then i can set the TV-image relative to the Background-ImageView's f.e. top and right margin. (looks hardcoded like this)
<ImageView
android:layout_width="125dp"
android:layout_height="85dp"
android:id="#+id/imageView1"
android:src="#drawable/tvImage"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="44dp"
android:layout_marginRight="169dp"
android:layout_marginEnd="169dp"
android:scaleType="fitXY" />
Basically there are now two things i have handle, to make the TV-image fit when changing the screen resolution:
1) the width and height of the picture.
2) the position of the picture.
To find out 1):
My Background is f.e. 500x250dp.
In an Image program i measured the size i need for the TV-image: 125x85dp.
Now i can calculate the width and height back depending on the background size.
500/125 = 4 for width calculation
250/85 = 2,9 for height calculation
So if the Screen changes for example to 800x400dp the width and height for the TV-image an be calculated like this:
800 / 4 = 200dp
400 / 2,9 = 137dp
So my TV-image should have a size of 200x137
To find out 2)
Again i have to calculate like in Point 1). Find out the position of the TV-screen and then set the layout margins according to the calcualtions. I havent implemented this point yet, so i can't explain more, but i think you get what i want to say. If not feel free to ask :)
And in the end, i can setup the TV-image with this code (hardcoded values should be the calculated variables!)
RelativeLayout.LayoutParams layoutPar = (RelativeLayout.LayoutParams) tvImage.getLayoutParams();
layoutPar.setMargins(0,88,210,0); // or whatever
layoutPar.height = 137;
layoutPar.width = 250;
tvImage.setLayoutParams(layoutPar);

Full screen background image in an activity

I see many applications that use a full-screen image as background.
This is an example:
I want to use this in a project, the best way I've found so far to do this is to use an image with a large size, put it in a ImageView and use android: adjustViewBounds="true" to adjust the margins
The problem is that if a screen with a very high resolution, the image falls short.
Another option I thought of is to use the image in a FrameLayout, with match_parent in width and height as background... this stretches the image, but I think the result is not very good.
How would you do it?
There are several ways you can do it.
Option 1:
Create different perfect images for different dpi and place them in related drawable folder. Then set
android:background="#drawable/your_image"
Option 2:
Add a single large image. Use FrameLayout. As a first child add an ImageView. Set the following in your ImageView.
android:src="#drawable/your_image"
android:scaleType = "centerCrop"
Another option is to add a single image (not necessarily big) in the drawables (let's name it backgroung.jpg), create an ImageView iv_background at the root of your xml without a "src" attribute.
Then in the onCreate method of the corresponding activity:
/* create a full screen window */
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.your_activity);
/* adapt the image to the size of the display */
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(),R.drawable.background),size.x,size.y,true);
/* fill the background ImageView with the resized image */
ImageView iv_background = (ImageView) findViewById(R.id.iv_background);
iv_background.setImageBitmap(bmp);
No cropping, no many different sized images.
Hope it helps!
You should put the various size images into the followings folder
for more detail visit this link
ldpi
mdpi
hdpi
xhdpi
xxhdpi
and use RelativeLayout or LinearLayout background instead of using ImageView as follwoing example
<?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:orientation="vertical"
android:background="#drawable/your_image">
</RelativeLayout>
It's been a while since this was posted, but this helped me.
You can use nested layouts. Start with a RelativeLayout, and place your ImageView in that.
Set height and width to match_parent to fill the screen.
Set scaleType="centreCrop" so the image fits the screen and doesn't stretch.
Then you can put in any other layouts as you normally would, like the LinearLayout below.
You can use android:alpha to set the transparency of the image.
<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="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/image"
android:alpha="0.6"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There"/>
</LinearLayout>
</RelativeLayout>
What about
android:background="#drawable/your_image"
on the main layout of your activity?
This way you can also have different images for different screen densities by placing them in the appropriate res/drawable-**dpi folders.
If you want your image to show BEHIND a transparent Action Bar, put the following into your Theme's style definition:
<item name="android:windowActionBarOverlay">true</item>
Enjoy!
use this
android:background="#drawable/your_image"
in your activity very first linear or relative layout.
In lines with the answer of NoToast, you would need to have multiple versions of "your_image" in your res/drawable-ldpi,mdpi, hdpi, x-hdpi (for xtra large screens), remove match_parent and keep android: adjustViewBounds="true"
Add android:background="#drawable/your_image" inside your Relativelayout/Linearlayout
Worked.
If you have bg.png as your background image then simply:
<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"
android:background="#drawable/bg"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"/>
</RelativeLayout>
Working. you should tryout this:
android:src="#drawable/img"
three step for put background
1)you should choose your like picture. for example :enter image description here
2)Then you copy this picture in drawable.
warning: you should choose types short for name picture.
enter image description here
3)you go to page xml Intended and write :
android:background="id picture"
for example my picture id is #drawable/download.
enter image description here
finish.
The easiest way:
Step 1: Open AndroidManifest.xml file
You can see the file here!
Step 2: Locate android:theme="#style/AppTheme" >
Step 3: Change to android:theme="#style/Theme.AppCompat.NoActionBar" >
Step 4: Then Add ImageView & Image
Step 4: That's it!

ImageView does not center scaled down images properly

I'm having trouble getting an image to scale and center properly in an ImageView.
When up-scaling, there are no problems, but when down-scaling, the image no longer appears to be centred, and the bounds of the ImageView are wrong, like this:
problem.png
<?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">
<ImageView
android:src="#drawable/stereomood_splash"
android:scaleType="fitCenter"
android:id="#+id/imageView1"
android:layout_width="100dip"
android:layout_height="200dip"></ImageView>
</LinearLayout>
Is this a bug, and has anyone found a workaround?
You should set android:adjustViewBounds="true" as in this answer.
You also need to set either android:layout_width or android:layout_height to be wrap_content, otherwise I believe your hard-coded bounds will override any view bounds adjustments.
Edit: Put this here as it's relevant to those encountering the same symptoms.
This problem is only present in the XML editor - on a real phone or emulator the image is sized and positioned correctly. Depending on the particular image and ImageView dimensions, the editor preview will show the image incorrectly resized and/or out of the view bounds.

Categories

Resources