I have an ImageView in which i want to show ProgressBar while loading the image from URL. To load image i am using VolleyPlus library. Here is the code to load image:
DiskLruBasedCache.ImageCacheParams cacheParams = new DiskLruBasedCache.ImageCacheParams(getApplicationContext(), "CacheDirectory");
cacheParams.setMemCacheSizePercent(0.5f);
SimpleImageLoader mImageFetcher = new SimpleImageLoader(getApplicationContext(), R.drawable.progress, cacheParams);
mImageFetcher.get(image_url, imgView);
Now my problem is Can i make drawable to make ProgressBar (R.drawable.progress) within ImageView.
You use setDefaultImageResId to an image to show during loading and if failed but you will not be able to use a view though. You can however use FrameLayout and ProgressBar behind NetworkImageView to show spinning progress while loading.
In case of successful download you do not have to do anything thing because Android should draw the progress view because it is hidden, in case of failure you will have to take it off.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<com.android.volley.toolbox.NetworkImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
</FrameLayout>
Related
I am having a recyclerview inside a relative layout and the app is working fine, the navigation drawer is smooth. But I added an Imageview over the recycler view so that I can hide and show based on the availability of the data. But soon as I have added the Imageview the navigation drawer becomes very slow. And when I remove the Imageview it again works smooth. The image size is just 38kb. Can someone tell me how to show empty state in an efficient way
This is my Layout with the ImageView
<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="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.test.sample.itemHistory"
tools:showIn="#layout/app_bar_item_history"
android:background="#ffffff">
<android.support.v7.widget.RecyclerView
android:id="#+id/materialList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageViewNoItems"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/noItems" />
</RelativeLayout>
Thanks in Advance
In my case, I try to load Image (PNG File within the Project Drawable Resource Folder) to my RecycleView's ImageView as well using Picasso and I facing the same problem where the Navigation Drawer sliding animation is being slow down.
I found that the main issue is because my Image File is quite Large so the Picasso load the Full Size image into the RecycleView's ImageView which would cause the Lag.
My Solution are: (I am using KOTLIN Language)
Re-size the image using Picasso built in resize function
Without Resize (Which Cause Lag):
Picasso.get().load(R.drawable.icon).into(holder.view.ImageView)
Resized (Solve Lag Problem):
Picasso.get().load(R.drawable.icon).resize(400,400).centerCrop().into(holder.view.ImageView)
Use setImageDrawable instead of Picasso
This Method does not required to Resize the image:
holder.view.ImageView.setImageDrawable(holder.view.ImageView.context.resources.getDrawable(R.drawable.icon))
used picasso or glide library for image loading.. and also add one line in your manifest and your project become smooth as before.
<application
android:largeHeap="true">
</application>
largeHeap is used to avoid outofmemory exception while loading images.
i'm trying to show gif files in web view and create a nine patch photo(as frame)around the web view.but always web view and it's frame gets fill parent width.but i need the frame exactly shows around gif file without any extra space,this is my code:
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="#drawable/frame_tir3"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/web1"/>
</FrameLayout>
and i tried very different codes but any doen't work.
I wan't to indicate that my webview is loading in some way. Using the Progressbar seems like a good solution but I am not certain how to properly do this.
By Progressbar I mean a spinning thingie in Android.
This is what I know by now and what I have tried:
I know about onPageStarted(...) and onPageFinished(...) callbacks which you define inside the WebViewClient and then hide or show Progressbar based on calls to those methods. BUT this approach is creating many problems with the HTML page inside the webview. For example when I hide Progressbar HTML elements resize and than go back to their original sizes for a brief moment. This looks really ugly and I don't know why this is happening. I tried putting my Progressbar and webview inside Frame and Relative layout (in order to have the progress cantered) and with both of these I get the above problem. HTML page loading is Javascript heavy since there is http://cubiq.org/iscroll-4 library on it but I doubt it's the problem with the library since the same page loads without strange behaviours when opened in Browser app on the phone.
My main problem is that I don't know how to define a layout containing my Progressbar and webview and avoid strange zoom in/out jumps. That is why I am asking how should one show progress bar correctly over a webview.
EDIT:
This is one of the layouts I tried:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
It's more or less similar with relative layout but I use centerInPerent=true on both axes for the ProgressBar
There's an efficient way to show progress in an activity. If you are using ActionBar, then
you can set an INDETERMINATE progressbar (the spinning one), or a horizontal progressbar (as seen on web browsers like Chrome). To do this you have to set the Window Feature to appropriate values like:
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
OR
requestWindowFeature(Window.FEATURE_PROGRESS);
To set the progress value, do this:
setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
Then call setProgress(int) whenever you want to change the progress value.
You can controll the visibility of the progressbar using
setProgressBarIndeterminateVisibility(true); //sets it to visible
AND
setProgressBarIndeterminateVisibility(false); //hides the progressbar
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view.
I would use a RelativeLayout :
<?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">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:centerInParent="true" />
</RelativeLayout>
This should work...
I'm making an app that displays images from urls in a listview. I use a asynctask to get the image and place it into the imageviews. The way I currently have it is that an asynctask is called in the getview method. The do in background part gets the image from the url. The problem I'm having is when I scroll to an element in the listview, initially the wrong image is there but the correct image is quickly loaded. Why is this happening?
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="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitStart"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Make sure that you are setting your ImageView to null as soon as you declare it. It's hard to tell without knowing what the image is, but it's possible that the ListView is reusing old images. Reference below:
wrong images are displaying first and then correct images are displaying during scrolling of listview
I'm using an AsyncTask to download Images for my Listview, because I dont want the download of the Images to block my UI-Thread. While the images are being loaded, I want to show an animated progress circle in the spot, where the image will be.
But I cant find an Image of the progress circle. What is the Ressource-Id? Or is there any other way? Does someone has a link to this image?
Look at the progress bar. It can work (and it does by default, AFAIR) in indeterminate mode, which means it shows a rotating circle, like the one you are asking for. I know this is not an image, but what you can do, is to place a FrameLayout instead of the image, with progress bar as the only child. Then, once the loading of the images finishes, remove the progress bar and add the image.
Define this a global variable ProgressDialog pd;
Just before launche the AsyncTask do:
pd = ProgressDialog.show(CurrentClassName.this,"This is the title","This is the detail text",true,false,null);
When its done onPostExecute just call pd.dismiss();
For more detail look at : ProgressDialog
You will have to take special considerations for it to work when you rotate the device while the dialog is up.
This is an example of a layout file for intro activity with an image and circle progress bar:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/intro_description"
android:scaleType="fitXY"
android:src="#drawable/intro" />
<ProgressBar
android:id="#+id/progress"
style="?android:attr/progressBarStyleInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="150dp"
android:visibility="visible" />
</FrameLayout>
You want an indeterminate ProgressBar. Take a look at developer's website -
ProgressBar