Please advice how to place an image as background for LinearLayout so this image would look as is, not stretched, titled or something.
Other solution is to define your image in a XML and refer to this XML on your background as explained here : Background Image Placement
Like that you can control your image (background) layout
Jojo's solution might work if the content of Linear layout is smaller than image height.
if i add two or edit text automatically the image starts stretching. if u still want to avoid and make the imageview at background use the below xml layout.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:id="#+id/imageButton1" android:src="#drawable/icon" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#null"></ImageButton>
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="100px"></Button>
</RelativeLayout>
Because only image button has the src options that remains constant to width and height of image.
make android height and width of LinearLayout into wrap_content using this code in .xml file
android:layout_height="wrap_content"
android:layout_width="wrap_content"
to set background to the image use code
android:background="#drawable/image"
Now place the image in the drawable file
Related
I just want to make a simple image view but when I run that app the unnecessary white background comes. I don't want to need that part. any solution.
and here is my xml file for that image view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/pot1"/>
</LinearLayout>
In code use -
imgview.setScaleType(ScaleType.FIT_XY);
Or in xml Image view -
android:scaleType="fitXY"
remove the gravity attribute if you want to make it stick to the top of screen or add scaleType attribute fitCenter, cropCenter or fitXY if you want to make it fill the screen.
When assigning an image as a background to a Layout(Linear || Relative) it stretched all over the Layout, but I want to make that background image to be centered inside the Layout as in the ImageButtons :
android:scaleType="centerInside"
Is there's a way to achieve that in Linear or Relative Layouts?
You should try the....
1.) take the RelativeLayout propery matchParent both. and centerInparent=true
2.) take one ImageView inside it Property WrapContent.
now take the background or src of inage view
you can use another layout LayoutB inside the LayoutA. and set the width and height to wrap_content of LayoutB..and set the image to background of LayoutB..and also set the arributs like this:
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
This will help you
<?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/someImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/someImageSrc" />
</RelativeLayout>
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>
I should like to have an android app which has its background image on the bottom of the view.
Hopefully this could be achieved by only using XML.
My background image (bg.png) is located at the folder "res/drawable-mdpi".
The XML code is now:
<?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:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg"
android:layout_alignParentBottom="true"
android:layout_marginBottom="0dip" />
</LinearLayout>
But this is stretching the whole image over the whole page. I only want it stretched in horizontal direction.
The height has to be in proportion with the (stretched) width.
And this image has to be the background of a view/layout, I want to keep using the layout and add items/objects to it.
I hope someone could help me with this, I've been looking on the internet for it for a few hours and I couldn't find anything more helpful then I have now.
Thanks in advance,
Dennis
Firstly Change your LinearLayout to a RelativeLayout (android:layout_alignParentBottom only works with RelativeLayout).
Secondly use android:src="#drawable/bg" on your ImageView (not background)
if you really want to use the LinearLayout, you can try adding this attribute to your imageview android:layout_gravity="bottom"
I am trying to add ImageView programatically inside a LinearLayout, which has vertical orientation. My layout file is describe as the xml below:
<?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="wrap_content"
android:paddingTop="5dip"
android:id="#+id/mainView"">
<TextView android:id="#+id/tvTituloInformacao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#color/white"
android:gravity="center_horizontal"
android:shadowColor="#color/black_translucent"
android:shadowDx="2.0"
android:shadowDy="2.0"
android:shadowRadius="3.0"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<LinearLayout android:id="#+id/resourceContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginBottom="10dip"
android:padding="3dip"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_below="#+id/tvTituloInformacao"
android:background="#color/black_translucent2" />
</RelativeLayout>
And my java source code that adds the ImageViews to the LinearLayout is as follow:
for(Resource r : mUser.getPictures()) {
ImageView img = new ImageView(this);
img.setVisibility(View.VISIBLE);
img.setTag(r.getThumb60());
imageLoader.DisplayImage(r.getThumb60(), this, img);
((LinearLayout) tempFotosView.findViewById(R.id.resourceContainer)).addView(img);
}
What happens is that before and after each ImageView I am getting some extra spaces that should not be there, so the height of the images container increases a lot. One test that I did and got the expected result was not to use the imageLoader and instead I set the ImageResource programatically to a static image file. This way I didn't get any extra space.
The weird result is illustrated in the image below:
Is there a way to not add this extra space?
I think images you are loading in image views may have some extra spacing on top and bottom, and other assumption you are loading images in imageview, with different resolution than your screen-size, and setting scaleType of ImageView to centerInside, if it is the case please try using following by fixing height of the image view as well, and set scaleType to fixXY.
This happens when the image is wider than the screen size after the ImageView has been built. Android shrinks the image's dimensions accordingly to aspect ratio, but doesn't remove the allocated space around it earlier.
For a cleaner fix, try this code after your imageLoader.
((LinearLayout)findViewById(R.id.resource)).add(img);