Hi I have a AysncTask that i'm wanting to inflate a loading view whilst that runs when it's finished hide it. does anyone know how to do this? at moment its not showing the loading view.
this is what i have tried
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
loadingView = LayoutInflater.from(getBaseContext()).inflate(R.layout.loadingcell,
null);
//Check Preferences which sets UI
checkPreferences();
PostTask posttask;
posttask = new PostTask();
posttask.execute();
}
#Override
protected Boolean doInBackground(Void... params) {
boolean result = false;
loadFixtures();
publishProgress("progress");
loadResultsFeed();
publishProgress("progress");
loadNewsFeed();
publishProgress("progress");
return result;
}
protected void onProgressUpdate(String... progress) {
StringBuilder str = new StringBuilder();
for (int i = 1; i < progress.length; i++) {
str.append(progress[i] + " ");
}
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Log.v("BGThread", "begin fillin data");
FillData();
loadingView.setVisibility(View.GONE);
}
}
loadingcell.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:id="#+id/loadingcell">
<include
layout="#layout/header" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="87dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/progressBar1"
android:gravity="center"
android:text="Loading..."
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/blue" />
</RelativeLayout>
You are not using an AsyncTask in you code. Use something like this:
private class doSomething extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
//Inflate you view or do anything here before loading stuff
}
protected Void doInBackground(Void... urls) {
//loading stuff
}
protected void onProgressUpdate(String... progress) {
//change your UI - like a progressbar
}
#Override
protected void onPostExecute(String unused) {
//Hide the progressbar
}
You need to have a content for the Activity - which you can set using setContentView
Related
I am using this code, everything are working fine, but progressbar is not showing. I want to block my ui during the method implementation and want to show progressbar. I want to get behaviour like progressDialog.
Here is my method
public void effct(int effectNo) {
final int finalEffectNO = effectNo;
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
progressLayout.setVisibility(View.VISIBLE);
progressLayoutLinear.setVisibility(View.VISIBLE);
Thread thread = new Thread(){
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
imageViewFragment.applyEffect(finalEffectNO);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
progressLayout.setVisibility(View.GONE);
progressLayoutLinear.setVisibility(View.GONE);
}
});
}
};
thread.start();
}
My xml code for progressLayout
<LinearLayout
android:id="#+id/progressBarLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:padding="12dp"
android:visibility="gone"
android:orientation="horizontal">
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="18sp"/>
</LinearLayout>
For above your requirement you must use ProgressDialog for same, here is an example
Example
Try using an AsyncTask instead of Thread, something like this :
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
progressLayoutLinear.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(final Void ... params ) {
runOnUiThread(new Runnable() {
#Override
public void run() {
imageViewFragment.applyEffect(finalEffectNO);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
});
return null;
}
#Override
protected void onPostExecute( final Void result ) {
progressLayoutLinear.setVisibility(View.GONE);
}
}.execute();
Hope this helps
There’s a few ways to solve your problem - check this video for more information. The most basic approach might be an AsyncTask, which could look like this:
public class DummyAsyncTask extends AsyncTask<Void, Void, Void> {
private final Window window;
private final ProgressBar progressBar;
public DummyAsyncTask(Window window, ProgressBar progressBar) {
this.window = window;
this.progressBar = progressBar;
}
#Override protected void onPreExecute() {
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
progressBar.setVisibility(View.VISIBLE);
}
#Override protected Void doInBackground(Void... voids) {
// Do the heavy lifting.
}
#Override protected void onPostExecute(Void aVoid) {
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
progressBar.setVisibility(View.GONE);
}
}
You execute it by running new DummyAsyncTask(/* params */).execute();.
Do note that AsyncTask does not adhere to the lifecycle and holding a reference to a Context aware instance is just asking for trouble. A safer alternative might be using LiveData:
public void effect(int effectNo) {
// Show progress and disable user interaction here.
final ListData<Content> data = model.applyEffect(effectNo);
data.observe(this, (Observer<Content>) c -> {
// Apply results, hide progress & enable user interaction.
});
}
Given the above, you should be careful how you handle showing & hiding progress, so you don’t accidentally disable user interaction forever.
My problem is that when i click button "Click" to call my API it should show my progressBar(spinner) while I'm calling API. Instead my application freeze for less than a second and then when it's done calling it shows my loading spinner for a brief time (it just flash)
Here is my code
private ProgressBar spinner;
public View onCreateView(...)
{
spinner = (ProgressBar) view.findViewById(R.id.progressBar1);
spinner.setVisibility(View.GONE);
...
}
private class MyTask extends AsyncTask<String, Void, Void> {
String pageContent = "";
DataOutputStream wr;
#Override
protected void onPreExecute() {
spinner.setVisibility(View.VISIBLE);
}
#Override
public Void doInBackground(String... params) {
requestResult.setSuccess(false);
HttpURLConnection connection;
try {
String url = "myURL";
//I only call my Api here. I delete rest so this won't bother you
requestResult.setSuccess(true);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
JsonParser parser = new JsonParser();
//Same here i delete nonimportant
MyResponse = new Gson().fromJson(jsonContent, MyResponse.class);
done();
spinner.setVisibility(View.GONE);
}
}
I've tried hounder different things it always freeze for a second and then my loading spinner flash and my content from API is shown.
Can you help me with that one, please?
My xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll_view_send"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/buttonSend"
android:visibility="gone"/>
</RelativeLayout>
</ScrollView>
Try the below snippet. It does not require spinner to be declared in layout file.
AsyncTask<String, String, String> asyncObject =
new AsyncTask<String, String, String>() {
ProgressDialog progDailog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progDailog =
new ProgressDialog(Activity.this);
progDailog.setMessage("Loading");
progDailog.setCancelable(false);
progDailog.show();
}
#Override
protected String doInBackground(String... urls) {
//Do background stuff here
return null;
}
#Override
protected void onPostExecute(String result) {
progDailog.cancel();
//Do post background stuff here.
}
};
asyncObject.execute(null, null, null);
Please try this working sample, pass the spinner to your MyTask like this:
private class MyTask extends AsyncTask<String, Void, Void> {
String pageContent = "";
DataOutputStream wr;
private final ProgressBar progress;
public MyTask(final ProgressBar progress) {
this.progress = progress;
}
#Override
protected void onPreExecute() {
progress.setVisibility(View.VISIBLE);
}
...
#Override
protected void onPostExecute(Void result) {
JsonParser parser = new JsonParser();
//Same here i delete nonimportant
MyResponse = new Gson().fromJson(jsonContent, MyResponse.class);
done();
progress.setVisibility(View.GONE);
}
}
Then call new MyTask(progress).execute();
EDIT: In your question you refer that your phone is freezing while calling the MyTask...
Please check this step to avoid freezing your UI in AsyncTask:
Do not call MyTask using new MyTask().get()
Try moving to doInBackground this part MyResponse = new Gson().fromJson(jsonContent, MyResponse.class); done(); this could be expensive.
Hope its helps!!
I want to display multiple image views from URL place alongside each
My xml is :
<HorizontalScrollView android:id="#+id/horizontalScroll"
android:visibility="gone"
android:layout_width="wrap_content"
android:background="#EEEEEE"
android:layout_height="100dp"
android:scrollbars="horizontal"
android:layout_gravity="center">
<LinearLayout
android:id="#+id/imgLayout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
My java code :
Drawable imgg;
ImageView image;
LinearLayout layout;
layout = (LinearLayout)findViewById(R.id.imgLayout);
private void grabImageFromUrl(String url) throws Exception
{
imgg = Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
}
/////////////////////////////////////////////////////////////////////////////////////////
class DownloadImageFromURL extends AsyncTask<String, String, String> {
String _ads;
public DownloadImageFromURL(String ads)
{
_ads = ads;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... f_url) {
image = new ImageView(getApplicationContext());
image.setId(imgId++);
try
{
grabImageFromUrl("http://dariran.com/Ads/Mobile/Small/" + _ads);
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(String... progress) {
}
#Override
protected void onPostExecute(String file_url) {
try
{
image.setImageDrawable(imgg);
layout.addView(image);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
DownloadImageFromURL Class built on a loop syntax(images count)
I loaded images but they placed on each other
I don't know why can't display true.
This is my xml file:
<ProgressBar
android:id="#+id/progress_index"
style="#android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"/>
<ListView
android:id="#+id/listview_index"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideInset">
</ListView>
I try to mProgress.setVisibility(View.GONE) after mListView.setAdapter() ,but when I check the "Show layout bounds" in the "Developer options".The layout not actually gone.As the picture shows,the ProgressBar is still in the center of the FrameLayout.Why?
Java code:
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mProgress = (ProgressBar) view.findViewById(R.id.progress_index);
mProgress.setVisibility(View.VISIBLE);
mAdapter = new CustomAdapter(getActivity());
new MyTask().execute();
lv.setAdapter(mAdapter);
}
public class MyTask extends AsyncTask<Integer, Void, ArrayList<NewsItem>> {
protected ArrayList<NewsItem> doInBackground(Integer... params) {
// some execution....
}
protected void onPostExecute(ArrayList<NewsItem> result) {
mAdapter.notifyDataSetChanged();
mProgress.setVisibility(View.GONE);
mListView.setVisibility(View.VISIBLE);
}
}
can you try this:
ProgressDialog mProgress = new ProgressDialog(activity.this);
...........
mProgress.dismiss();
I loading some data from a database and I'd like to display a circle animation during this, because it's take time. The circle appears but only after loadContent() which load data.
The code:
final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
ProgressBar p = (ProgressBar) findViewById(R.id.imgProgress);
p.setEnabled(true);
if (settings.getBoolean("my_first_time", true)) {
// the app is being launched for first time, do something
Log.d("Launched:", "first time");
loadContent();
// record the fact that the app has been started at least once
settings.edit().putBoolean("my_first_time", false).commit();
}
p.setEnabled(false);
In XML:
<ProgressBar
android:id="#+id/imgProgress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content" />
Why not just do this ?
public class SomeTask extends AsyncTask<Void, String, Void>{
ProgressDialog dialogue;
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialogue.dismiss();
//If Using some adapter; someadapteradapter.notifyDataSetChanged();
}
#Override
protected void onPreExecute() {
dialogue = new ProgressDialog(ProgressBar.this);
dialogue.setTitle("Loading items..");
dialogue.show();
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
//Your task!!
return null;
}
}
What can be more simple than this..:)
I don't know what is loadContent method but I'd suggest you to do an AsyncTask (if it's not the case). You will be able to show/hide your ProgressBar as follows:
// loadContent method
public class loadContent extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
// set your progress bar to visible
((ProgressBar) findViewById(R.id.imgProgress)).setVisibility(View.VISIBLE);
}
#Override
protected String doInBackground(String... params) {
// do some stuff: load datas from sql, load images.. whatever
// and return a string value for onPostExecute (string = "Loaded"
return string;
}
#Override
protected void onPostExecute(String result) {
if(result.equals("Loaded") {
// the loading is finish, display your datas
// set your progress bar to gone
((ProgressBar) findViewById(R.id.imgProgress)).setVisibility(View.GONE);
}
}
}
Here and here, you will see a good example of an AsyncTask method. Then, you just have to add the visibility attribute to your view as:
<ProgressBar
android:id="#+id/imgProgress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="gone" />
Hope this helps.