Android Gif ImageView unable to set width to match parent - android

I m using GifImageView from here.
With the below code(without min-height) it just shows a thumbnail size of the gif -
<LinearLayout
android:id="#+id/linear_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/linearlayout1">
<pl.droidsonroids.gif.GifImageView
android:id="#+id/gif"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
the screenshot below shows the third image as the gif.
I had to set the min-height of the LinearLayout to 300dp so that the gif can fit the whole parent width.
<LinearLayout
android:id="#+id/linear_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/linearlayout1"
android:minHeight="300dp">
<pl.droidsonroids.gif.GifImageView
android:id="#+id/gif"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Now the gif is shown in whole width but the LinearLayout has paddings as the height is 300dp.

android:adjustViewBounds="true"
is the answer to autoscaling for all Views.

Related

Image Height Exceeding Wrap Content Android

I am using xamarin android to make a android app in which i am using cardview with recycler view.
Problem
Image height is exceeding the wrap content of the text.
What is Want
I want the image size to be the size covered by text.
What I Have Tried:
I have tried setting image as a background of the linearlayout but it sets the height of the layout itself much greater than shown in images below.
i have tried setting this in framelayout but it gives same result as below
Details about the images
numbers are provided with the instance of the card view in
1: image the image source height is increasing the view height
2: this image is setted perfectly because the original image height resolution is low and the image is stretched to view
3: again image height resolution is high so the view is extended by image
4,5 cardview without image
My Code:
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/cardView"
app:cardBackgroundColor="#android:color/transparent">
<ImageView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:id="#+id/cardImage"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/templateLayout"
>
<TextView
android:text="Title:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="#+id/listTitle"/>
<TextView
android:text="Start Time:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="#+id/listSTime"/>
<TextView
android:text="End Time:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="#+id/listFTime"/>
<TextView
android:text="Description:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="#+id/listDescription"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Images:
Add the following fields to ImageView
android:scaleType="centerCrop"
android:adjustViewBounds="true"
you can change the imageView attributes to android:scaleType="centreCrop" and change the height to wrap content. you are matching it to parent in you case,its the card view. Whereas, textview is in a linearlayout.
Orelse you can move ImageView to same linearLayout of the textView

Imageview keep aspect ratio, fill width AND height

I am facing a problem with setting background image for my app. I have linear layout with two imageviews. One with logo and weight 1 and one with background image with weight 3. I want the second one to be always centered horizontally and have sides cropped in vertical orientation and bottom cropped in horizontal orientation. CENTER_CROP almost does the job but I want to have the top of my image drawn. I don't care about the bottom.
In this configuration vertical orientation is perfect but horizontal cuts the top of image and I want the top to be always visible. Bottom can be cropped
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="50dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/masters_logo"
android:id="#+id/imageView" />
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop"
android:src="#drawable/masters_login_background"/>
</LinearLayout>
That's what I want to achieve.
Vertical orientation:
Horizontal orientation:
If you want to set background into your login screen, you should add android:background="#drawable/masters_login_background" attribute into your root LinearLayout.
Otherwise, you should use, for example, RelativeLayout as root element and add inside this your ImageView as top element, smth like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/masters_login_background"/>
<!-- other code here -->
</RelativeLayout>
I had to edit the ans as I understood your question wrongly. Please use the below code. This will postion your bg img in the background and logo above the bg img.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop"
android:src="#drawable/masters_login_background"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="50dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/masters_logo"
android:id="#+id/imageView" />
</LinearLayout>

Android - ImageView as header

I want to display an image on top of my activity that always fill the screens width. If I set its width to match parent and its height to wrap content there is a white stripe at the sides of the image on larger screen. How can I make that this image always fill the screen horizontally?
My xml:
<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:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/hello_world"
android:src="#drawable/tbbt" />
</RelativeLayout>
Thanks in advance.
change imageview to fit the complete screen width
android:scaleType="fitXY"
or
android:scaleType="centerCrop"

LinearLayout too wide / wrong size calculation of images

I have a LinearLayout that has only images in it. The image size is reduced as the height of the layout is not sufficiant. Images are sized correctly.
However: The width of the LinearLayout is too wide. It is actually the size the images would have if adjustviewbound=false would be set instead of true as it is in my case.
This appears to be a problem with the RelativeLayout around it. If I use a LinearLayout for the page, the calculation is correct.
Any ideas how to get the correct width? (no, I can not change the RelativeLayout).
Here is the layout for you to try (I used Android 4.3 API 18):
<?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" >
<LinearLayout
android:id="#+id/map_Test"
android:layout_width="wrap_content"
android:layout_height="10dp"
android:background="#color/red"
android:orientation="horizontal" >
<ImageView
android:id="#+id/map_DrillDownImage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#android:drawable/ic_dialog_alert" />
<ImageView
android:id="#+id/map_DrillDownImage2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:adjustViewBounds="true"
android:src="#android:drawable/ic_dialog_email" />
</LinearLayout>
</RelativeLayout>

LinearLayout ImageView Resize All Image Widths To Fit

I have a horizontal linear layout that contains 3 image views. Each image view contains the same image which is 200x200 pixels. Here is my layout xml:
<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"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image200" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image200" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image200" />
</LinearLayout>
</RelativeLayout>
And here is what it looks like:
Notice how the third image from the left is resized I assume because there is not enough real estate to contain 3x 200 pixel wide images.
What I would rather happen is instead of shrinking only the last image, all three images are resized evenly so that they all fit across the screen. How do I setup my layout to evenly fit all three images?
Thank you
UPDATE - after changing my settings here is what things look like:
Notice that the border around the image view is still 200 px high. Why is this happening?
Set Width and height to match_parent for imageviews and set layout_weight=1 for all imageviews. Also set Attribute android:scaleType="fitXY"
This worked perfectly for me:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:adjustViewBounds="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:adjustViewBounds="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:adjustViewBounds="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Layout width when set to 0dp, layout width is what came into picture. And here layout width of the LinearLayout is match parent. So it will take the whole width, and as per the layout width of the images, they take space in the ratio of there weight.
make your three images a width of 0dp
and add them a layout_weight of 1
;)
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/image200" />
#JanTacci Regarding your follow up question "Note, however, that even though the image has been shrunk the actual layout height is still 200 px. Why is this happening?"
I just had the same issue and fixed it by adding this property to each ImageView:
android:adjustViewBounds="true"

Categories

Resources