Display a scrollable image from a url in android studio - android

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>

Related

white border depending of size of the image

I want to make a ImageView of specyfic height (1/3 of the screen) with white border and the image will be resize according to the height (to keep the proportion) for example. It will be perfect if also I will be able to add TextView below the image. I want to obtain smth like that :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="#color/backgroundContent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/white"
android:gravity="center"
>
<ImageView
android:contentDescription="#string/pic"
android:id="#+id/pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:padding="5dp"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressPic"
android:layout_centerInParent="true"
android:visibility="gone"/>
</RelativeLayout>
edit: Im downloading images form url using Glide library if that matter

How to fit imageView inside LinearLayout in android, but always keep its aspect ration

I am trying to make an ecommerce app, the biggest problem i face is fitting the images when i get it from an api call. The images can be in any size, so i need to make sure they fit perfectly in all screen sizes.
This is my cart xml listview item layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/primary">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="0.2">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#drawable/temp_image_product_detail"
android:id="#+id/layout_sort_by_listview_icon"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:layout_weight="0.8">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="adkfj;aklsjdf"
android:textSize="25sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs. 255"
android:textSize="25sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4.2"
android:textSize="15sp"/>
</LinearLayout>
I want to achieve the perfect fitting look for the cart page and the product listing page. Something like this shown in:
http://traverphillips.com/Amazon-Material-Design
I hope it will help i didn't tested it though :
your first LinearLayout should look like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/primary">
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:layout_weight="0.2">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/temp_image_product_detail"
android:id="#+id/layout_sort_by_listview_icon"/>
if you want the image to be fit horizontaly you can set layout_width to fill_parent and layout_height to wrap_content but you must set adjustViewBount "true"
Please use android:scaleType="fitXY" for fit in screen
Try layout_width="match_parent" and use layout_weight

Volley NetworkImageView being resized after loading

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>

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=""

down image[Reflaction one] not displaying as upper image

In my application i have two images original upper image and down reflection one. SO i made sample application and it is working fine as specified in this screenshot.
now i want to implement same in my application which has complex layout file. SO the code snippt is as follow. but 2nd image is always coming small with no respect of padding. CEnter point of both image is same but the width of down image is most probably half of it. I tried to play with margin but even if i increase it there is no change. let me know if you have any question. the screen and code snippt of my real application and image is as follow. i do not know why it is not working same way as it is working in sample app.
<RelativeLayout android:id="#+id/LinearLayout02"
android:layout_gravity="center" android:layout_height="fill_parent"
android:layout_width="wrap_content" android:orientation="vertical">
<View android:layout_width="match_parent" android:layout_height="32.5px"
android:layout_alignParentTop="true" android:id="#+id/top_padding"
android:background="#FFFF0000"></View>
<VideoView android:id="#+id/video_view"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:layout_width="fill_parent" android:visibility="gone"
android:layout_below="#+id/top_padding"
android:layout_height="match_parent">
</VideoView>
<ImageView android:id="#+id/video_image_view"
android:layout_gravity="center" android:layout_below="#+id/top_padding"
android:layout_above="#+id/bottom_padding" android:layout_height="match_parent"
android:baselineAlignBottom="true"
android:src="#drawable/anna"
android:layout_width="match_parent">
</ImageView>
<View android:layout_width="match_parent" android:layout_height="32.5px"
android:layout_alignParentBottom="true" android:id="#+id/bottom_padding"
android:background="#FFFF0000"></View>
<ImageView android:id="#+id/video_image_view_reflection"
android:layout_below="#+id/video_image_view" android:layout_width="fill_parent"
android:background="#FF00FF00" android:layout_alignBaseline="#+id/video_image_view"
android:layout_marginTop="32.5px"
android:layout_marginBottom="32.5px"
android:src="#drawable/anna"
android:layout_height="match_parent">
</ImageView>
</RelativeLayout>
Try this modifications:
<ImageView android:id="#+id/video_image_view"
android:layout_gravity="center" android:layout_below="#+id/top_padding"
android:layout_above="#+id/bottom_padding" android:layout_height="fill_parent"
android:baselineAlignBottom="true"
android:layout_centerInParent="true"
android:src="#drawable/anna"
android:layout_width="wrap_content">
</ImageView>
<View android:layout_width="fill_parent" android:layout_height="32.5px"
android:layout_alignParentBottom="true" android:id="#+id/bottom_padding"
android:background="#FFFF0000" ></View>
<ImageView android:id="#+id/video_image_view_reflection"
android:layout_below="#+id/video_image_view" android:layout_width="wrap_content"
android:background="#FF00FF00" android:layout_alignBaseline="#+id/video_image_view"
android:layout_marginTop="32.5px"
android:layout_marginBottom="32.5px"
android:layout_centerInParent="true"
android:scaleType="matrix"
android:src="#drawable/anna"
android:layout_height="fill_parent" >
</ImageView>

Categories

Resources