wrap_content doesn`t work with background - android

There is Frame Layout, where width\height = wrap_content. But it increases to max size when I try to set a background, even if it doesn't contains any elements.How to fix it?
<FrameLayout
xmlns:android="schemas.android.com/apk/res/android";
xmlns:app="schemas.android.com/apk/res-auto";
android:background="#drawable/oblakol"
adroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true" >
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</FrameLayout>

Your background is much bigger than you think. Check size of background!

Wrap content is working but your image size is large and hence you are not getting the expected results.
Try importing image as -> IMAGE ASSET so the size will be auto-Adjusted
or
set size manually!

I hope this will help you ...
wrap_content will put the background of it's original size.
Using large resolutions will cause lagging in your app while using. So, you should resize your background according to your needs : Image Resizer

When you set the dimensions(layout_width or layout_height) of a view to WRAP_CONTENT, you are giving freedom to the view to expand without any limit(as per its content ). Same is with your scenario as you've set the height and width to WRAP_CONTENT for your Frame Layout.
So whatever the space occupied on screen, its due to the large size of your android:background="#drawable/oblakol" drawable image. Try setting the height and width of the Frame Layout to some random values to test it.
android:layout_width="250dp"
android:layout_height="250dp"
I've attached some screenshots for your reference.
android:layout_width and android:layout_height set to WRAP_CONTENT. Actual dimension of the android Image is 800x799(wxh)
android:layout_width and android:layout_height set to WRAP_CONTENT. Actual dimension of the android Image is 256x256(wxh)
android:layout_width and android:layout_height set to 250dp. Actual dimension of the android Image is 800x799(wxh)
Hope it helps.

Related

ImageView src moves the image to center leaving sides visible when changing width and height

Please don't mark this a duplicate, as I am unable to find the solution.
Hi, I am trying to add src image with some background color
Here is what I am doing
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:background="#drawable/bg_layout_corners"
android:backgroundTint="#color/color_fbfbfa"
android:gravity="center">
<ImageView
android:id="#+id/iv_dashboard_niggle"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:scaleType="centerInside"
android:background="#color/colorAccent"
android:src="#drawable/niggle2_3_3_3" />
</RelativeLayout>
and here is the output I am getting
When I try to decrease the width or height of imageView, the actual image is placing at the center and some border is visible.
I also checked for scaleType="center", this is also not working
Please help me.
Hope this solves your problem.
iv_dashboard_niggle.setScaleType(ImageView.ScaleType.fitXY);
Set wrap_content as height for your relative layout.
Then the space will go.
The image that you wanted to set in a image view needed to be in the same aspect ratio to fit exactly without applying scaleType...
for example
If you want to set a square image that have height and width ratio 1:1 then ImageView ratio needs to be 1:1
Well but also we have ScaleType if images not according ratio then we can set scale type to match the size of image view by resizing our image
In your case it's completely depend on what you want to show.
If you don't want background border then simply set background color to transparent by using
android:background="#android:color/transparent"
Best way is define ImageView size according aspect ratio if you don't want to use scale type

Image Button getting larger one to the screen...?

I'm developing an android application in eclipse....
When I'm trying to put an image button in XML file... it become large one to the screen...
I tried to adjust the width and height(ex: 50dp and 50dp), But its cropping the image...
What to do....???(the code below is fake one don't mind it,plz...)
<Button android:id="#+id/categorybutton"
android:background="#drawable/button_bg"
android:layout_height="match_parent"
android:layout_weight="50dp"
android:layout_width="50dp"
style="#style/CategoryButtonStyle"/>
please, remove:
android:layout_weight="50dp"
...
Use android:src"#drawable/button_bg"
Remove android:layout_weight="50dp"and set value of android:layout_height to "wrap_content"
Here's the correct way to format that View, assuming the parent is a LinearLayout with orientation:"vertical", weightSum="100", and you want the button to fill half the parent.
<Button android:id="#+id/categorybutton"
android:background="#drawable/button_bg"
android:layout_height="0dp"
android:layout_weight="50"
android:layout_width="#dimen/width_dimen"
style="#style/CategoryButtonStyle"/>
Do yourself a favor and store your dimensions in dimens.xml. It will save you a lot of time in the future. Also, weight is not in pixels, so take out the dp. We've set the height to 0dp so the button fills the entire allocated weight.

what size to use for background?

I want to use an image of backgrounds, but I do not know that size must be set for different size android, there are several specification is that you can tell me that you need to use size in pixels . for my application
<LinearLayout 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:orientation="vertical"
android:background="#drawable/your_selected_background_image"
>
android:layout_width="fill_parent" and android:layout_height="fill_parent" attributes in your main layout will fill the background of your app with the image that you have selected completely on any device.
Use a NinePatch Image.
A NinePatch is a PNG image in which you can define stretchable regions that Android scales when content within the View exceeds the normal image bounds. You typically assign this type of image as the background of a View that has at least one dimension set to "wrap_content", and when the View grows to accomodate the content, the Nine-Patch image is also scaled to match the size of the View
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/myninepatch" />
http://developer.android.com/guide/topics/resources/drawable-resource.html#NinePatch
According to the android developer documents fill_parent is renamed as match_parent in API Level 8 and higher and it is recommend to use match_parent instead of fill_parent.
math_parent/fill_parent can make the View's size stretch to its parent's size,so if you using android:layout_width="math_parent" and android:layout_height="match_parent" in your background layout. Your background image will be automatically stretch to the size of your screen size(Accurately, it is depend on its parent's size).
wrap_content make the view's size just big enough to enclose its cotent. So using wrap_content the view's size and its background will stretch depending on the view's content.

Scrollview + scaling images = doesn't scroll properly, why not?

I have an image that I scale to the width of the phone/tablet. It is 224x1632. The imageview is within the scrollview, and I need to be able to scroll up and down on that image.
The issue is that the scrollview sets according to the image size (1632 in length) on create, but when the image scales/stretches it is 3x taller than the original. Now the scroll-view is too small to scroll the entire image.
Any way to make the scrollview fit the image?
Note: the image length will differ per phone, so I can't set it to a predetermined size.
XML Code:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:scrollbars="vertical"
android:src="#drawable/tiles" />
</ScrollView>
You set your ScrollView's height to wrap_content which means "be as big as my content." You also set your ImageView's height to fill_parent which means "be as big as my parent." Either one of these statements is enough to prevent scrolling. Your ImageView should be wrap_content and your ScrollView should be fill_parent.
Try scaleType="center" instead of "centerCrop"

9patch stretches vertically, but does not horizontally

I created a 9patch image and somehow it does only stretch vertically.
I tried other 9-patch images, but they have the same effect, whyle they work in other situations. So the 9patch should be fine I think.
This is my XML code:
..
<ImageView
android:id="#+id/bottombar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:contentDescription="#string/app_name"
android:src="#drawable/bar" />
</RelativeLayout>
Anybody had the same issue and know how to solve it?
Thanks
If you are sure that the height of your View is really taller than the current image (as the others have suggested) then you should change your ImageViews scaleType. The default is FIT_CENTER which does not stretch the image, you should set it to FIT_XY.
Also you may try to set your 9-png file as the background of your ImageView not as the src and I think this will also stretch the file.
Did you add a black dot to the top dead center as well as the left side center?
The issue may be in layout_width="match_parent" you have mentioned width to match parent this may be the reason of 9 patch image to stretch vertically. But for height you have written height="wrap_content" So it doesnt stretch in height
You have wrap_content specified for the height. Are you sure the content is tall enough to make the image stretch?

Categories

Resources