I need to show standard loading circle on half-opaque background above current layout (with background and buttons etc). I found ProgressBar and how it works, but I need to render it on existing layout, and that's the problem.
To be more clear - imagine login screen with some image and button aka "Register". On click I need to show partly visible black background and a loading circle.
Try and see if this works (use it as an inner class)
private class executeHospitalNameGet extends AsyncTask<String, Void, Void> {
private ProgressDialog Dialog = new ProgressDialog(SettingsActivity.this);
private Context mContext;
public executeHospitalNameGet(Context context) {
mContext = context;
}
protected void onPreExecute() {
Dialog.setMessage("Getting Hospitals...");
Dialog.setTitle("Requesting Hospital Name");
Dialog.show();
}
protected Void doInBackground(String... urls) {
//DO WORK HERE
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
}
}
You would call it with this
new executeHospitalNameGet(getApplicationContext()).execute();
Use a RelativeLayout, It will allow you to have overlapping Views, check out this link:
http://developer.android.com/reference/android/widget/RelativeLayout.html
I think you're looking for something like modal dialog with loading animation...
check it out this... http://developer.android.com/guide/topics/ui/dialogs.html#ProgressDialog
Related
I have a progress dialog fully defined by java code. I just want to change the background color and probably text color. I've seen other posts in stackoverflow like adding style.xml and so on, but none worked. Please don't refer me to other posts. What to do?
My code sounds like this:
Class A extends Activity {
ProgressDialog pd;
Context context;
public void onCreate(){
context = A.this;
pd = new ProgressDialog.show(context, "" , "Loading");
}
}
![A sample of progress dialog which i want. Actually i have it
, but the problem is the background color which i want to be white.]1
pd = new ProgressDialog.show(context, "" , "Loading");
pd.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.RED));
Do this in your code:
Class A extends Activity {
ProgressDialog pd;
Context context;
public void onCreate(){
context = A.this;
pd = new ProgressDialog(context, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert);
pd.setTitle("");
pd.setMessage("Loading");
pd.show();
}
}
I want to show some message and a progress bar while my app initializes.
I need to insert some dictionaries of words into a SQLite database the first time my app is run. To do this I have an AsyncTask which opens my SQLiteOpenHelper and closes it again, just so the database initialization is done once.
private class AsyncDbInit extends AsyncTask<Void, Void, Void> {
private Context context;
private Intent intent;
public AsyncDbInit(Context context, Intent intent){
this.context = context;
this.intent = intent;
}
#Override
protected Void doInBackground(Void... params) {
DatabaseHandler db = new DatabaseHandler(this.context);
db.close();
return null;
}
#Override
protected void onPostExecute(Void param) {
context.startActivity(this.intent);
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... params) {}
}
This AsyncTask is called in my onCreate() method, but I've also tried to run it from onStart() and onResume() without succes.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dispatcher);
... //some code finding the right intent
new AsyncDbInit(this, nextIntent).execute();
}
Somehow this last line, which calls the AsyncTask, stops my UI from showing up; the screen just stays blank until the AsyncTask is completed and the new activity is started.
When I comment that line out, the UI shows up just fine.
The only thing I can come up with is that the SQLiteOpenHelper somehow blocks the UiThread, but I couldn't find anything about that either.
In the AsyncTask we have some methods. Just like in doInBackground() we do the things we wants to be done in the background and there are two methods also whch are onPreExecute() and onPostExecute(). Create and progress dialog and show the dialog in onPreExecute() method and dismiss it in onPostExecute() method.
Try using AsynTask.executeOnExecutor() with the thread pool executor. If this works, it means something involved with loading your UI is also using an AsyncTask. AsyncTasks by default run sequentially on a single work thread and this can introduce contention. This serial execution is often what you want, but not always.
Does you UI use any libraries to load strings or other content? Can you provide your layout XML?
I use a lot of AsyncTasks but always like this:
private class connectAsyncTask extends AsyncTask<String, Void, String> {
//Create a Progress Dialog
#Override
protected void onPostExecute(String info) {
//Dismiss the dialog
}
#Override
protected String doInBackground(String... params) {
//complete code to get data to be populated on the view
}
#Override
protected void onPreExecute() {
//Show the dialog
}
But its like showing the progress dialog until I fetch the data and then populate all the View on the layout.
Instead I want it to be like, some data will be sent as Bundle, those can be used to populate their respective views and anything like a ImageView, whose Image is yet to be fetched must show a progress dialog.
Many apps like Amazon etc.., does this. Even the dialogs doesn't have any rectangular backround or text. How to do this?
I have a MainActivity. Sometimes when it is loading I observe black screen for a second.
I measured timings for operations in onCreate method and discovered that more than one second was spent for setContentView(R.layout.main_screen);
I prefer to show previous screen (in my case Splash screen) instead of this black screen during setContentView execution. How can I rid off this black screen?
Seems android in some way preloads layouts and such problems occurs sometimes. But if I kill my process and start app I always see this black screen.
Use a static variable to handle the View cache.
Use an AsyncTask to don't freeze your origin Activity
Use LayoutInflater to inflate the View layout and cache it
In the onCreate() of the target Activity set the cache
Something like this:
Origin activity
...
//noinspection unchecked
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
LayoutInflater inflater = (LayoutInflater)
MainParentActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// VERY VERY SLOW action if your Activity is heavy
DialerActivity.cachedView = inflater.inflate(R.layout.dialer_portrait, null, false);
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
Intent intent = new Intent(MainParentActivity.this, DialerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
}.execute();
...
Target activity
public class DialerActivity extends MainParentActivity {
static View cachedView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (cachedView != null) {
setContentView(cachedView);
} else {
setContentView(R.layout.dialer_portrait);
}
}
. . .
You can use also a ProgressDialog while inflating to avoid freeze sensation on the transition.
I am having problem with setting a new Drawable to my ProgressBar.
If I use the setProgressDrawable() inside onCreate() method it works great. But when I try to call the same method inside a Handler post callback it doesn't work and the progressbar disapears.
Can someone explain this behaviour? How can I solve this problem?
downloadingBar.setProgress(0);
Drawable progressDrawable = getResources().getDrawable(R.drawable.download_progressbar_pause_bg);
progressDrawable.setBounds(downloadingBar.getProgressDrawable().getBounds());
downloadingBar.setProgressDrawable(progressDrawable);
downloadingBar.setProgress(mCurrentPercent);
First you should reset the progress to zero
Set the progress drawable bounds
Set new progress drawable
Set new progress
Bumped into this problem myself and I managed to get it working :)
I used the AsyncTask to handle the background tasks/threads, but the idea should be the same as using Runnable/Handler (though AsyncTask does feel nicer imo).
So, this is what I did... put setContentView(R.layout.my_screen); in the onPostExecute method! (ie. instead of the onCreate method)
So the code looks something like this:
public class MyScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.my_screen); !!! Don't setContentView here... (see bottom)
new MySpecialTask().execute();
}
private int somethingThatTakesALongTime() {
int result;
// blah blah blah
return result;
}
private void updateTheUiWithResult(int result) {
// Some code that changes the UI
// For exampe:
TextView myTextView = (TextView) findViewById(R.id.result_text);
myTextView.setText("Result is: " + result);
ProgressBar anyProgressBar = (ProgressBar) findViewById(R.id.custom_progressbar);
anyProgressBar.setProgressDrawable(res.getDrawable(R.drawable.progressbar_style));
anyProgressBar.setMax(100);
anyProgressBar.setProgress(result);
}
private class MySpecialTask extends AsyncTask<String, Void, Integer> {
ProgressDialog mProgressDialog;
#Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(MyScreen.this, "", "Calculating...\nPlease wait...", true);
}
#Override
protected Integer doInBackground(String... strings) {
return somethingThatTakesALongTime();
}
#Override
protected void onPostExecute(Integer result) {
mProgressDialog.dismiss();
setContentView(R.layout.my_screen); // setContent view here... then it works...
updateTheUiWithResult(result);
}
}
}
To be honest, why you need to call setContentView in onPostExecute I have no idea... but doing so means you can set custom styles for your progress bars (and they don't disappear on you!)
Maybe you put the code in a thread which is not main thread.
If you want to work with the UI, you must do that in the main thread :)
I was also facing the same issue but in my case it is due to the use of Drawable.mutate() method. When i removed that method it started working fine. I also noticed that this issue exist below api level-21(lollipop).