Android Horizontal RecyclerView wrap_content - android

I'm making horizontal RecyclerView with photos in it, and I have faced up with wrap_parent problem. If I put in my View width = wrap_content, it is screenWidth width, although image consumes very small space. If I change wrap_content to some value it works perfectly.RecyclerView is 75dp height and match_parent.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="#drawable/card_foreground">
<ImageView
android:id="#+id/bitmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/image" />
</LinearLayout>
Screenshoot
Dark blue is screen and light blue is photos. Problem is I have case 1 when I set view to wrap_content.
SOLUTION:
I have solved this problem recently, there is some bug or something simmilar, when you try to have on scaled ImageView wrap_parent. It doesn't work very well.
To solve this, I have resized every photo to final size before put it in RecyclerView. In adition, I have some small space between ImageView again, and I have solved by adding padding=0 and margin=0 to childern of RecyclerView.

First solution: LinearLayout weight attribute
As long as you would use for that view LinearLayout you may use for all of your ImageView views this attribute:
android:layout_weight="{number_value}"
Shortly, if you add for all of your ImageView views the same value for weight they would get calculated the same space, but not more than their parent view attributes.
http://developer.android.com/guide/topics/ui/layout/linear.html
Read please this issue:
What does android:layout_weight mean?
If you still face the problem, give me a call.
Second solution: ImageView scale attribute
For ImageView you can use scaleX, scaleY, scaleType attributes. For having the same height you should use the same scaleY value for all your ImageView items.
Think also about using TableLayout or GridLayout for this purpose.

Related

Adjust ImageView width according to height

I am trying to make a simple LinearLayout composed of an ImageView and TextView.
The ImageView should scale to match the LinearLayout height and not lose proportions while doing so.
This is the xml I currently have.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="#CCCCCC"
android:scaleType="fitCenter"
android:src="#drawable/strip" />
<TextView
android:id="#+id/logoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="what an awesome text"
android:textSize="18sp" />
</LinearLayout>
Using the above xml, the result is that the ImageView height will indeed match the LinearLayout height and it's width will be the same as the src image but the rendered image will scale properly and center, but this leaves the ImageView itself filling about 90% of the Layout's width as it's the src image width, leaving no space for the TextView.
I just would like to scale the ImageView to match the parent's height and it's width should be just as much needed to scale it proportionately.
Add a linear layout on the image view. Just the image view and nothing else. :) hope it helps
EDIT 1: As many of the other answers mentioned Using the layout_weight property will also help resolve this issue.
add weight 1 on imageview so there will be space for the textview
As I mentioned in the comment under your answer post where you cite KISHORE_ZE, it seems to be helpful only with an image of particular size, and on a particular screen density and resolution. So while it looks acceptable on your current device, it might be a mess on a different one. I suggest that you figure out what part of the layout you want to use for the image and use android:weight attributes for your ImageView and TextView to achieve wanted result.
As #KISHORE_ZE said, this solves the problem.
"Put the image view in a linear layout. Just the image view. Nothing else. "
this visual guide is a lot helpful.
link
in your image view add:
android:scaleType="fitCenter"
or
android:scaleType="fitXY"
whichever suites you.

android relative layout contains image view layout

I have a image view inside of a relative view, as per code below
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/maintenance_banner"
android:layout_alignParentTop="false"
android:layout_margin="0dp"
android:padding="0dp"/>
</RelativeLayout>
what i don't understand is the gaps at the top & bottom, as I have set both height to wrap_content, the gaps shouldn't be part of the content, only the imageview it self isn't it? Or does it means the gaps are part of the image view it self? but i set the padding to 0. also, for some reason I can get ride of them by setting scale type.
The image probably has another aspect ratio then your imageView.
Try to set the android:scaleType to centerCrop or whatever else does work for you.
The default scale type is: FIT_CENTER and FIT_XY for buttons.
Hope this helps
EDIT: Sorry I haven't seen the wrap content.
From the screenshot it looks like you have other views below, are you including this layout file into another one?
Cheers

Android: Create Horizontal List View but Image size cannot be set

I'd like to create an Horizontal List View.
And I reference this http://www.dev-smart.com/archives/34
But it set ImageView size 150dp in "listitem.xml"
<ImageView
android:id="#+id/image"
android:layout_width="150dip"
android:layout_height="150dip"
android:scaleType="centerCrop"
android:src="#drawable/icon"
/>
I tried to change the width and height of the ImageView, but it's useless.
It seems that in the HorizontalListView.java, the function "addAndMeasureChild()" has set
child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
But I change the MEasureSpec to EXACTLY or others, it's still useless.
Anyone can figure out this?
Thanks in advance.
Eric
MeasureSpec.makeMeasureSpec(getWidth()... is the width of the listview, try replacing getWidth() with params.width, this worked for me using a fixed size for the layout, in your case, the imageview. My solution doesn't work if you use MATCH_PARENT or WRAP_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"

Android wrap_content and fill_parent at the same time?

Is it possible to achieve something like this:
I have one picture to set as a background on layout in which is also TextView. So I want that layout to set his height to wrap content (which should be the height of TextView) and ImageView to has fill_parent as height and width.
I tried using:
LinearLayout with background and TextView
TextView with background
RelativeLayout with ImageView (set height to fill_parent) and TextView (set to wrap_content)
Is there a way to do this?
Sorry, if it's next question about the same, but I have no idea how to find a solution.
EDIT:
I'll try to be more specific. I want a TextView with background. But I want that background to be scaled to fit TextView which should be wrap_content width and wrap_content height. But I have image in higher resolution so there is a lot of unused space (background doesn't scale).
No, there is no way to set a view's width/height to both wrap_content and fill_parent simultaneously.
What exactly are you trying to achieve? Is this what you mean?
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
...
android:background="#drawable/image" >
<TextView
android:layout_width="wrap_content"
android:layout_width="wrap_content"
... >
</TextView>
</LinearLayout>
EDIT
If you want to nicely scale a background image for a TextView, I think what you might want to look into is a 9-patch image. See this post for more info. Also this.

Categories

Resources