Progressbar won't appear on ImageView - android

I wonder if I'm doing anything wrong. My code is pretty straight forward and would like an example of how you guys made it work if you have a progress bar to load an image in any of your apps, thanks! I'm using volley's imageloader and the progress bar never ever appears
holder.mProgressBar.setVisibility(View.VISIBLE);
if (video.thumbnail != null) {
mImageLoader.get(video.thumbnail, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.mProgressBar.setVisibility(View.GONE);
holder.mScreenShot.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
holder.mProgressBar.setVisibility(View.GONE);
}
});
}
My XML
<RelativeLayout
android:id="#+id/image_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:background="#android:color/darker_gray"
android:id="#+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="#dimen/album_cover_height"
android:clickable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:scaleType="fitXY" />
<ProgressBar
android:visibility="gone"
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>

Definitely a timing issue. I replicated your scenario as best as I could.
public class MainActivity extends AppCompatActivity {
ImageView image;
ProgressBar pb;
public String url;
public ImageLoader imageLoader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
pb = (ProgressBar) findViewById(R.id.pb);
RequestQueue queue = Volley.newRequestQueue(this);
url = "http://www.hrwiki.org/w/images/thumb/d/d5/currentbad.png/180px-currentbad.png";
imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);
#Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
show();
new testTask().execute(imageLoader);
}
public void show() {
image.setVisibility(View.INVISIBLE);
pb.setVisibility(View.VISIBLE);
}
public void hide() {
image.setVisibility(View.VISIBLE);
pb.setVisibility(View.INVISIBLE);
}
private class testTask extends AsyncTask<ImageLoader, Void, Void> {
#Override
protected Void doInBackground(ImageLoader... params) {
SystemClock.sleep(5000);
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
imageLoader.get(url, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
hide();
image.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
}
With this layout file:
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/image" />
<ProgressBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:id="#+id/pb"/>
</FrameLayout>
I saw the same behavior until I put the aSync task in which basically waits for 5 seconds before loading the image. This definitely shows that OnResponse is getting called so quickly that you aren't seeing the progress bar.
Looks like Michelle's answer has what you would look for to implement some pre-check logic.

Refer to this previous question for a detailed explanation for how onResponse(ImageLoader.ImageContainer response, boolean isImmediate) is working.
Volley, How many times the onResponse in ImageLoader.ImageListener called
I believe it is basically just immediately turning off your progress bar. Haven't done this particular thing before but based on the other question I think you want to add a check for:
if(!isImmediate)
holder.mProgressBar.setVisibility(View.GONE);

Related

html5 video not work in android native webview

I have the following link
http://stage.diabetesmasterclass.org/Upload/myclinic/SAM/visit1/sam_storyline_output/story_html5.html
it contains an html5 video and I have tried to load it inside a webview in my android app
it keep loading and never run, I tried to enable javascript and to add a lot of configuration but did not work also
I even tried to use AdvancedWebView sdk to render it, and in this case it loads but did not run anything
can anyone help please ?
You can load page using below code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
protected AgentWeb mAgentWeb;
private LinearLayout mLinearLayout;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
mLinearLayout = findViewById(R.id.web_content);
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent(mLinearLayout, new LinearLayout.LayoutParams(-1, -1))
.closeIndicator()
.setWebChromeClient(mWebChromeClient)
.setWebViewClient(mWebViewClient)
.setMainFrameErrorView(R.layout.agentweb_error_page, -1)
.setAgentWebWebSettings(getAgentWebSettings())
.setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
.setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
//.interceptUnkownUrl()
.createAgentWeb()
.ready()
.go("http://stage.diabetesmasterclass.org/Upload/myclinic/SAM/visit1/sam_storyline_output/story_html5.html");
}
private WebChromeClient mWebChromeClient = new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
};
private WebViewClient mWebViewClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("Info", "BaseWebActivity onPageStarted");
}
};
public #Nullable
IAgentWebSettings getAgentWebSettings() {
return AgentWebSettingsImpl.getInstance();
}
#Override
protected void onPause() {
mAgentWeb.getWebLifeCycle().onPause();
super.onPause();
}
#Override
protected void onResume() {
mAgentWeb.getWebLifeCycle().onResume();
super.onResume();
}
#Override
protected void onDestroy() {
super.onDestroy();
mAgentWeb.getWebLifeCycle().onDestroy();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == 2) {
mAgentWeb.getIEventHandler().back();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Top"
android:textSize="22dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/web_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Bottom"
android:textSize="22dp" />
</LinearLayout>
</LinearLayout>
using this Webview
update Output look like this

ProgressBar is not functioning in android activity

I am using progressbar in simple login application.But the progress bar is not showing .Sometimes when i change the position of the progressbar is shows before the calling progressbar.show();
here is My XML code
<ProgressBar
android:id="#+id/progressBar"
android:indeterminateDrawable="#drawable/progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible"
>
</ProgressBar>
In onCreate() method:
progressBar= (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.GONE);
In button oncliklistener
btn_login.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
uid=usrusr.getText().toString().trim();
upass=pswd.getText().toString().trim();
try {
progressBar.setVisibility(View.VISIBLE);
checkLogin(uid,upass);
progressBar.setVisibility(View.GONE);
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
I have tried many other solutions from stackoverflow but none of them worked
Thanks:/
Try the below code.
private ProgressDialog progDailog;
private Button btn_login;
private EditText uid, upass;
private String uid1, upass1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progDailog = new ProgressDialog(this);
progDailog.setTitle("Login");
progDailog.setMessage("Please wait...");
progDailog.setCancelable(false);
btn_login = findViewById(R.id.btn_login);
uid = findViewById(R.id.uid);
upass = findViewById(R.id.upass);
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
uid1 = uid.getText().toString().trim();
upass1 = upass.getText().toString().trim();
progDailog.show();
checkLogin(uid1, upass1);
Runnable progressRunnable = new Runnable() {
#Override
public void run() {
progDailog.dismiss();
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(progressRunnable, 3000);
}
});
}
Here 3000 is milliseconds which means 3 seconds, you can change it according to your requirement.
Your XML will be like.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<EditText
android:id="#+id/uid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="uid" />
<EditText
android:id="#+id/upass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="upass" />
<Button
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="btn_login" />
</LinearLayout>
Please try this and let me know if you require any other changes.
Have you considered that your "data processing" code is running on the UI thread, blocking any visible changes. I would suggest trying putting
runOnUiThread(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
}
});
If your progressBar still not showing and nothing helps
Try added code to show the progressBar before calling the api and dismiss the progressBar on response of the request.
Ex.
void checkLogin(String name, String psd){
progressBar.setVisibility(View.VISIBLE);
//start the api call
}
void onApiCallresponse(){
runOnUiThread(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
//code for what you want to do after login
}
});
}
You can show your progress for 3 second like this :
progressBar.setVisibility(View.VISIBLE);
progressBar.postDelayed(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.GONE);
}
}, 3000) ;
checkLogin(uid,upass);
But it is better to show your progress when you start to check login and invoke api and hide it when you receive a result.

TouchImageView Is not Showing Image After Downloading form Volley

<com.example.util.TouchImageView
android:id="#+id/imageViewRc"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="2dp"
android:layout_marginRight="3dp"
android:src="#drawable/placeholder_background"
android:scaleType="fitXY" />
Below is the code where I am wiring this up:
private void setImageWithPicaso(String imageUrl) {
if (!(imageUrl == null)) {
Picasso.with(getActivity()).load(imageUrl).into(imageView, new Callback() {
#Override
public void onSuccess() {
spinner.setVisibility(View.GONE);
}
#Override
public void onError() {
spinner.setVisibility(View.GONE);
AppLog.showToastMessage(getActivity(), "Rate Card loading failed!");
}
});
} else {
spinner.setVisibility(View.GONE);
AppLog.showToastMessage(getActivity(), "Rate Card loading failed!");
}
}
The download is happening , and when I start pinching the zoom it works
Appreciate your time.
Picasso.with(getActivity()).load(urlToLoad).placeholder(R.drawable.placeholder_background).into(imageView, new Callback()
modify with above code and put some pic rename it placeholder_background and put it in drawables

trouble with running YoutubeAndroidAPIDemo app

I checked stackoverflow for similiar problems and didn't found anything.
so here's my problem, I'm building an android app and I need to use YouTube API.
I followed this guide:
https://developers.google.com/youtube/android/player/
but I can't seem to run the Demo App.
the entire project is full of errors and I can't understand why,
all of the "R.id.blabla" items are having an error saying:
"blabla cannot be resolved or is not a field"
I checked those Questions - Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties
and : YouTubeAndroidAPIDemo does not run
tried everything there, didn't helped much.
I'm pretty sure I am missing something but I have no idea what.
anyone else encountered something like this before?
plz help :D
Try this:
layoutfile.xml
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/teacherName"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#fff"
android:padding="5dp" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/date"
android:layout_centerHorizontal="true"
android:layout_marginTop="115dp"
android:visibility="gone"
/>
<TextView
android:id="#+id/progressBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/progressBar1"
android:layout_centerHorizontal="true"
android:maxLines="3"
android:visibility="gone"
android:text="Video Loading..." />
TutorialVideoView.java
public class TutorialVideoView extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener
{
private VideoView videoView;
private MediaController mController;
private Uri uriYouTube;
ProgressBar progressBar1;
TextView progressBarText;
String v_title,v_date,v_id,v_url,v_teacher;
public static final String API_KEY = "Your API key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorialvideo_view);
progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
progressBarText=(TextView)findViewById(R.id.progressBarText);
v_id="Your YouTube Video ID";
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.setVisibility(View.VISIBLE);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(v_id);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};
}
Hope this may help you!
I had a similar problem, try deleting the: import R.java line, if it's there.

Horizontal progress bar does not update its status

I am trying to show horizontal progress bar "Not ProgressDialog" on my activity like this
here is what my xml file contains
<ProgressBar
android:id="#+id/pdialog"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
/>
I am trying to update its status using AsyncTask Class by setting pdialog.setProgress() but its not showing any progress, it works with progressdialog but not with horizontal progress bar.
public class MainActivity extends Activity {
private SQLiteDatabase db;
private Cursor cursor;
private ProgressBar pdialog;
private ImageButton btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
btn = (ImageButton) findViewById(R.id.startbtn);
pbar = (ProgressBar) findViewById(R.id.progressBar1);
pdialog = (ProgressBar) findViewById(R.id.pdialog);
pdialog.setMax(100);
pdialog.setProgress(20);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pdialog.setVisibility(View.VISIBLE);
new DownloadFilesTask().execute();
}
});
}
private class DownloadFilesTask extends AsyncTask<Void, Integer, Integer> {
int load = 1;
protected Integer doInBackground(Void... params) {
try {
load = 10 * i;
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
pdialog.setProgress(load);
}
});
}
} catch (Exception e) {
}
}
protected void onProgressUpdate(Integer... progress) {
if (progress[0] == 100) {
pdialog.setVisibility(View.INVISIBLE);
}
}
protected void onPostExecute(Integer params) {
}
}
}
If load variable gets changed correctly:
Instead of this:
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
pdialog.setProgress(load);
}
});
You could use:
publishProgress(load);
which would automatically call on UI Thread:
protected void onProgressUpdate(Integer... progress) {
int p = progress[0];
if (p >= 100) {
pdialog.setVisibility(View.INVISIBLE);
}else{
pdialog.setProgress(p);
}
}
UPDATE:
remove android:indeterminate="true" as pointed out in other answer.
In your layout file please remove the following attribute android:indeterminate="true" from ProgressBar element.
I had the same question, but
I found the problem is the interface View.OnClickListener().
When the pbar was put out of the btn.setOnClickListener(new View.OnClickListener() {}), it worked well. Otherwise, it did not update.
Then, I made a constructor which passed the pbar into the OnXXXClickListener.
private class OnXXXClickListener implements View.OnClickListener() {
private ProgressBar bar;
public OnXXXClickListener(ProgressBar bar) {
this.bar = bar;
}
#Override
public void onClick(View v) {
bar.setProgess(50);
new DownloadFilesTask().execute();
}
}
Then the pbar could work well.

Categories

Resources