Volley NetworkImageView being resized after loading - android

I have a ListView where I display an Instagram-like feed with thumbnails and screen-filling pictures.
I use a Volley NetworkImageView for both the profile thumbnail and main picture and I have placeholders that are exactly the same size as the images downloaded from the server.
thumbnail: 128x128
image: 612x612
My problem is that after the image are loaded, they don't fill the screen anymore, it seems that volley is resizing them which results in abrupt change of layout. Also the layout of the thumbnail is changing slightly (padding) (see screenshots).
Any Idea of what might be wrong and can I do to fix this?
EDIT It seems that the layout is forcing the image to be smaller once it is loaded, if I set android:scaleType:centerCrop, the image takes full width but is cropped in height. How can I force the image to satisfy the constraint full width while keeping its height?
And the xml:
<?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="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<RelativeLayout
android:id="#+id/header_layout"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="false"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:adjustViewBounds="true"
android:src="#drawable/holder_spot_thumbnail" />
<TextView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="false"
android:layout_toRightOf="#id/thumbnail"
android:clickable="true"
android:gravity="center_vertical"
android:text="" >
</TextView>
<TextView
android:id="#+id/place_date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:layout_toRightOf="#id/thumbnail"
android:layout_alignWithParentIfMissing="false"
android:gravity="center_vertical"
android:text=""
android:textSize="12sp" ></TextView>
</RelativeLayout>
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/picture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/holder_feed_image"
android:padding="6dip"/>
</LinearLayout>

Related

Display a scrollable image from a url in android studio

I'm trying to create an activity that display an image that is obtained from an url. I want the width of the image to adapt the screen but the height of the image can be longer than the screen so that the image becomes scrollable (vertically).
First of all, I displayed the image from a drawable folder and it worked with the following code :
<ScrollView
android:layout_width="fill_parent"
android:id="#+id/scrollView1"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scrollbarAlwaysDrawVerticalTrack="true">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:src="#drawable/programme"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:scaleType="fitStart"></ImageView>
</LinearLayout>
</ScrollView>
I obtained what I wanted :
http://i.stack.imgur.com/imPkZ.png
But now I wanted the image to charge from an URL so I used the ion library :
Ion.with(imageView)
.load("http://...");
The image load but it is no longer scrollable and doesn't display as wanted.
http://i.gyazo.com/704324724364235bbe417d5447265c42.png
Do you have any solution?
Thanks
I found the solution by setting android:layout_width="match_parent" in ScrollView, ImageView and LinearLayout and setting android:adjustViewBounds="true" for ImageView like this :
<ScrollView
android:layout_width="match_parent"
android:id="#+id/scrollView1"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawVerticalTrack="true">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true"/>
</LinearLayout>
</ScrollView>

LinearLayout above ImageView not getting displayin Fragment?

I have ImageView which needs to be shown in full height available. However at top i have created LinearLayout which contains Text and Image. I want it to display at top but also want to image to fit screen. I have linear layout at bottom and its working fine.
This is my code for LinearLayout which i want to show at top. I have hidden actionbar
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#android:color/black"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ImageView
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="10dp"
android:src="#drawable/back_button" />
<TextView
android:id="#+id/titleofscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Testing"
android:textColor="#22c064"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageShown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</FrameLayout>
If i dont set imageView scale type to android:scaleType="fitXY" then it works but image doesnt fit screen.
I want to display image to fit between above bar which contains Text and below it has some icons. But if i make image scaletype ='fitXY' it doesnt display above text. Following is screenshot but i want image to occupy space
If you want the image to be below the actionbar you should not use the FrameLayout as a parent. You either should use vertical LinearLayout with weight or `RelativeLayout". Try following code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#android:color/black"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ImageView
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="10dp"
android:src="#drawable/back_button" />
<TextView
android:id="#+id/titleofscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Testing"
android:textColor="#22c064"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageShown"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/actionbar"
android:layout_alignParentBottom="true"
android:scaleType="fitCenter"
/>
</RelativeLayout>
Also I doubt that you use fitXY as android:scaleType, because it doesn't maintain the aspect ratio of the image - so your image will be deformed (stretched). In your case you should rather use fitCenter, but it depends on how exactly you want to scale your image - you can also play with centerInside or centerCrop.

Android - Layout not in uniform

I have a horizontal list of images in my android application. But the problem is some of the images seems to not follow the layout I've defined in my xml. Like this:
my layouts look like this:
mainlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back_shop_list"
android:orientation="horizontal">
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/shop_hlist_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:scrollX="1dp"
android:scrollY="1dp"
android:overScrollMode="always" >
</com.devsmart.android.ui.HorizontalListView>
</LinearLayout>
then my listlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:translationY="200dp" >
<FrameLayout
android:id="#+id/framelayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<ImageView
android:id="#+id/img"
android:clickable="true"
android:adjustViewBounds="true"
android:layout_width="360dp"
android:layout_height="360dp" />
<ProgressBar
android:id="#+id/img_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:translationY="150dp"
android:translationX="140dp" />
</FrameLayout>
<TextView
android:id="#+id/labelText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:translationX="10dp"
android:textColor="#dc6800"
android:textSize="16sp"
android:textStyle="bold" />
<!-- Description label -->
<TextView
android:id="#+id/desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:translationX="10dp"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
</LinearLayout>
My guess is that the size of my image is lesser than what my ImageView layout_width and height. Or I dont really know what causes this problem.
But any help will do, please. Thanks.
EDIT: What I really wanted is that if the image is like image 4 and image 5, it should not have those spaces. I've tried getting the size of the image from my ImageLoader then set the layout parameters height and width in there while images are loaded. But it looked worse.
Are there other ways in which i can dynamically adjust my ImageView if ever the images are like image 4 and 5?
If actually you want to make the image occupy the desired space
try adding this image View property to your ImageView
You can try others I usually use fitXY out of the predefined list you will get there in XML Layout
android:scaleType="fitXY"
so as to make this property work we also need
android:adjustViewBounds="true"
that you have already added in your case
If actually you want to avoid on blank spaces and let the image occupy as much space as it want
In my layout I am using two image views to show a demo
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_numbers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="#+id/tv_numbers"
android:layout_below="#+id/tv_numbers"
android:layout_marginTop="5dip"
android:layout_marginRight="10dip"
android:orientation="vertical">
<ImageView
android:id="#+id/textView1"
android:layout_width="200dip"
android:layout_height="200dip"
android:src="#drawable/desert"/>
<ImageView
android:id="#+id/textView2"
android:layout_width="200dip"
android:layout_height="200dip"
android:background="#drawable/desert"/>
</LinearLayout>
ImageView 1 is assigning image as Source using android:src="#drawable/desert"
and below is the result as you can notice that it is showing WhiteSpace arround it.
ImageView2 is assigning image as Background using android:background="#drawable/desert"
and below is the result as you can notice that it is showing no WhiteSpace arround it.
Or try using these attributes to make it possible
android:maxHeight=""
android:maxWidth=""
android:minHeight=""
android:minWidth=""

Listview issue with item background

I've got problem with my ListView item's background, it's bigger than should be.
Here is my background resource:
And it's screenshot from my device:
Layout:
<?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="vertical"
android:background="#drawable/bg_nocar">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="50dp"
android:layout_marginLeft="6.67dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reservation"
android:textStyle="bold"
style="#style/reservation_text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reservation.1"
style="#style/reservation_text" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/reservation.selectclass"
style="#style/reservation_selectclass_text"
android:layout_marginTop="33.33dp"
android:layout_marginLeft="6.67dp" />
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#00000000"
android:dividerHeight="0.67dp" />
</LinearLayout>
That's my item layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reservation_row"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/car_bg"
android:orientation="horizontal">
<LinearLayout
android:layout_width="93.33dp"
android:layout_height="98.67dp"
android:orientation="vertical"
android:layout_marginLeft="6.67dp"
android:layout_marginTop="13.33dp">
<TextView
android:id="#+id/classname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/reservation_classname" />
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/reservation_name"
android:layout_marginTop="16.67dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="98.67dp"
android:orientation="vertical"
android:layout_marginLeft="6.67dp"
android:layout_marginTop="13.33dp">
<ImageView
android:id="#+id/img"
android:layout_width="150dp"
android:layout_height="93.33dp"
android:layout_marginTop="6dp" />
<TextView
android:id="#+id/price"
android:layout_width="30dp"
android:layout_height="20dp"/>
<TextView
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
Why the background is so big?
Since you dont want your image to scale to the dimensions of the list item, you should not put it as a background.
Instead you can do this. Let the background be white (since your car_bg resource is all white with one logo). Then, your listview layout can look like this:
<RelativeLayout> //background white
<ImageView> //Your car_bg with width and height set as wrap_content
<LinearLayout> //All the content that you previously had
</RelativeLayout>
This way your image will not get scaled and remain as the original one. That said, you must now take car of different screen dimensions yourself. You must have different densitiy images available for your car_bg resource.
Try scaling the ImageView to keep its aspect ratio instead of attempting to set its height/width manually. This is most likely why your ImageView appears to be stretched.

How to avoid space between imageview and gridview in linearlayout

I have a image view and grid view in my .xml file. When I am executing this file its getting some space between grid view and image view. How to avoid this space...can any one help me to solve this problem.
thanks in advance...
my xml code is:
android:layout_width="0px"
android:layout_height="0px">
</Button>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/prevMonth"
android:src="#drawable/cal_left_arrow_off2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<Button
android:id="#+id/currentMonth"
android:layout_weight="0.6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#drawable/calendar_bar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="#+id/nextMonth"
android:src="#drawable/cal_right_arrow_off2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_gravity="bottom|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/calendarheader"
android:src="#drawable/blue_bg_with_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ImageView>
</LinearLayout>
<GridView
android:id="#+id/calendar"
android:numColumns="7"
android:layout_gravity="top"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</GridView>
You should try setting the android:gravity attribute correctly, and it has to work:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:gravity="bottom|center_horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:id="#+id/calendarheader" android:src="#color/blue_start"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:minWidth="250dp" android:minHeight="30dp">
</ImageView>
</LinearLayout>
<GridView android:id="#+id/calendar" android:numColumns="7"
android:layout_gravity="top"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#color/blue_start">
</GridView>
</LinearLayout>
i'm using here simple color background, and there is no gap:
If you still having the problem, then you should see your blue_bg_with_text2 image, whether it has any empty pixels at the bottom.
ps: ignore the android:minWidth="250dp" android:minHeight="30dp" part, it is there to imitate an image with that size.
Update
Here is the same view using RelativeLayout (it's a bit clearer at least for me):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/selectedDayMonthYear"
android:textColor="#000000" android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="0px" android:layout_height="0px" />
<ImageView android:id="#+id/prevMonth" android:src="#drawable/cal_left_arrow_off2"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="#+id/nextMonth" android:src="#drawable/cal_right_arrow_off2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<Button android:id="#+id/currentMonth" android:layout_weight="0.6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#drawable/calendar_bar2" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/prevMonth"
android:layout_toLeftOf="#id/nextMonth"
android:layout_alignBottom="#id/prevMonth"
android:layout_alignTop="#id/prevMonth" />
<ImageView android:id="#+id/calendarheader" android:src="#drawable/calendarheader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:layout_centerInParent="true"
android:layout_below="#id/currentMonth"
android:layout_margin="0dp"
android:scaleType="fitXY" />
<GridView android:id="#+id/calendar" android:numColumns="7"
android:layout_gravity="top" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/calendarheader"
android:layout_margin="0dp"
android:background="#color/white_fader_end">
</GridView>
</RelativeLayout>
I've added to it my resources as background (two of them are actually screenshots from yours).
What i observed is, you must
either use a static image for your
calendar header background, or,
if you use an xml drawable, you have
to drop the stroke element from it / set its android:width to 0
otherwise it will add an unnecessary gap around your view's drawable, that is as wide as the stroke width you've specified.
my output using static images as backgrounds looks this way:
Update 2
The problem is not between your calendar header and gridView, but inside your gridView.
It has a 'padding' of 5 px inside (not real padding, see how it gets there):
The default selector for the GridView is a 9x9 patch rectangle with a border of 5px.
That 5 px is visible all around your gridView.
You can specify a different listSelector for this grid either by xml:
android:listSelector="#drawable/grid_selector"
or from code (onCreate):
gridView.setSelector(R.drawable.grid_selector);
Now if your grid_selector is an image or is a drawable xml without any margins, then the gap that causes the problem is gone.
You can also hide that gap, by setting a background color to your gridView, that matches or grandients the calendar header's background color.

Categories

Resources