Can I show animation until my Activity loads? - java

I was wondering if I can display some kind of animation until my activity loads. Because when I start my app , it takes about 5 seconds to see the Activity. Until it loads, a white screen is displayed (refer to the image below).
Can I do that ?
And if I can , how it should be done ?
Thanks you in advance.
Have a great day.

if you are downloading some data or u got ridiculusly large listview - you got other problems....
downloading data: reduce amout of data that is needed for app to work
listview - user freaking recyclerview
and if this is a legit problem - then you need some sort of listener(that indicates whenever job is complete) like Thread.join() and whenever that listener activates - it should go to another intent and kill last one (to kill you need to use finish();)

Try to use ProgressDialog in AsyncTask
new YourFragment.YourAsyncTask().execute();
Use this library sweet-alert-dialog for more animation Loading
class YourAsyncTask extends AsyncTask<Void, Void, Void> {
SweetAlertDialog pDialog;
#Override
protected void onPreExecute() {
//show your dialog here
super.onPreExecute();
pDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.PROGRESS_TYPE);
pDialog.getProgressHelper().setBarColor(Color.parseColor("#fdc80b"));
pDialog.setTitleText("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
//hide your dialog here
super.onPostExecute(result);
pDialog.dismissWithAnimation();
}
}
}
}

Related

Display progress dialog till a condition will be true or time ended

I have an app that user submit the log in form , when it sent the data to server and create a connection for its account.
In this connection i have an integer field named as state.
the state value is : 1 for connecting, 2 for connected and 0 for failed.
I want to show a dialog to user show is Connecting ... and then check the state of connection if its return 0 or 2 dismiss the dialog and show the related message else if it doesn't change after 15 sec dismiss dialog and change the state to 0 !
How can i do this logic ?
I am assuming to make the network all you are using an Asynctask. In this case you can use the methods onPreExecute and onPostExecute.
For more information about network calls and Asynctasks, please read http://developer.android.com/training/basics/network-ops/connecting.html. I've given a brief explanation below though.
If you create a dialog or initialise it in your onCreate method (or something similar), you can call the below methods to show and hide the dialog when the call starts and finishes
onPreExecute() {
dialog.show();
}
onPostExecute(Object result) {
dialog.dismiss();
}
You can also modify the UI from doInBackground through the use of onProgressUpdate(). This will allow you to call to the dialog whilst performing the logic in doInBackground by calling the method publishProgress(). The exact place you should call the method I'm not sure of because I don't fully understand your bigger picture but I hope this helps you along the way
This is one way.You could also use AsyncTask.onCancelled()
public class TestActivity extends Activity{
private Dialog dialog;
#override
protected void onCreate(Bundle bundle){
//relevant activity code.
TestAsync ta=new TestAsync().execute();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
ta.cancel();
if(dialog!=null)
dialog.dismiss();
}
}15*1000);
}
public class TestAsync extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
//create your dialog here
}
#Override
protected void onPostExecute(Void result) {
if(dialog!=null){
dialog.dismiss();
}
}
#Override
protected Void doInBackground(Void... params) {
//relevant AsyncTask code
}
}
}

Getting data after the page loads works inconsistently

I'm attempting to make a menu that resizes to fit each device size. I have the buttons scaled, and the text will now correctly autofit into the buttons, but some text is longer than others. To accommodate this, I attempted to get the textSize of the largest text item and set the other text sizes to it with:
private class myAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(final Void... params) {
return null;
}
#Override
protected void onPostExecute(final Void result) {
super.onPostExecute(result);
about.setTextSize(contact.getEndTextSize());
hours.setTextSize(contact.getEndTextSize());
shop.setTextSize(contact.getEndTextSize());
}
}
The issue is that this will only work part of the time. I can navigate to one page multiple times and it will either work or won't.
My understanding is that onPostExecute is called when the background computation has finished. Is the issue that this timing doesn't necessarily coincide with when the page finishes rendering? Is there a better way to approach getting the textSize?

Continuously refresh Bluetooth RSSI using AsyncTask

I want to make the hand phone continuously refresh the RSSI values of other devices while the phone is being moved away from them.
In order to make the screen display keep updating the new RSSI values, I choose to use AsyncTask so as not to freeze my UI.
But somehow my code does not work. Every time it is run, the app dies. For debugging purpose, I put a Toast.makeText there. But nothing appears on the screen. So maybe that means my AsyncTask is actually not executed at all?
AsyncTask Codes:
private class Refresh extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
while(true){
Toast.makeText(getApplicationContext(), "Loop is being executed", Toast.LENGTH_SHORT).show();
init();
startDiscovery();
}
}
}
OnCreat() Codes:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
startDiscovery();
new Refresh().execute();
}
Do please help me!
Where am I wrong?
ToT

AsyncTask is still running after onPostExecute() in debug window

I have a program that creates an android application.
The main class of this program uses Async Tasks to connect, then request data. Both connect and request data are started via a button and a progress bar is displayed in both cases.
When I start the Async Task for connect, the program runs through the methods onPreExecute(), doInBackground(), onProgressUpdate() and onPostExecute(), which is as expected.
However after onPostExecute(), when I look in the debugger window, AsyncTask is still running and continues to run. When I then request data, a new AsyncTask is created and I have two running.
How do I terminate the first AsyncTask (and indeed the second once it has finished) as after using the program for a while I end up with around 20 AsyncTask threads still running!
I've included my code below:
private ProgressDialog connectionDialog;
private ProgressDialog requestDataDialog;
private ProgressDialog usingDialog;
private int currentDialog;
public void connectClick(View view) //When the connect button is clicked
{
currentDialog = 1;
new PerformTask().execute();
}
private class PerformTask extends AsyncTask<Void, Integer, Integer>
{
protected void onPreExecute()
{
showDialog(currentDialog);
}
protected Integer doInBackground(Void... voi)
{
int total = 0;
//Perform the task required
publishProgress(total);
return total;
}
protected void onProgressUpdate(Integer... progress)
{
usingDialog.setProgress(progress[0]);
}
protected void onPostExecute(Integer result)
{
removeDialog(currentDialog);
}
}
protected Dialog onCreateDialog(int id)
{
//Setup the dialogs
}
public void requestDownloadClick(View view)
{
currentDialog = 2;
new PerformTask().execute();
}
Just for completeness and so I can accept the question, here is the answer, given by grv_9098, found at stackoverflow.com/a/3077508/1514187 :
AsyncTask manages a thread pool, created with ThreadPoolExecutor. It will have from 5 to 128 threads. If there are more than 5 threads, those extra threads will stick around for at most 10 seconds before being removed. (note: these figures are for the presently-visible open source code and vary by Android release).
Leave the AsyncTask threads alone, please.

Progress Dialog blocking

Progress Dialog not showing up was solved by removing the blocking call.
My purpose is to download large amount of data from internet and keep the end user informed about download status, however I have to wait to the data to complete downloading for proceeding to the next step, and because of that I have to block the code from executing.
Blocking causes the progress dialog not to show up or freeze. I need tip for implementing those tasks the best way. Because my tasks are simple it seems for me to be overkill to implement task complete listener and I was wondering if there was any other way to solve my problem?
You can use this:
#Override
protected void onPreExecute() {
dialog= ProgressDialog.show(YourActivity.this, "", "MessageYouWantToDisplay");
}
#Override
protected void onPostExecute(T result) {
dialog.dismiss();
}
You call the class that extends AsycTask by typing:
new NameOFClass().execute();
Try this first as a test and put a Thread.sleep() in you doInBackground method to understand how it works. And after that use it in your project with your true methods, data etc.
Try this:::
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(YourActivity.this);
dialog.setMessage("Progress start");
dialog.show();
Log.d(TAG, "Showing dialog");
}
#Override
protected void onPostExecute(T result) {
dialog.dismiss();
Log.d(TAG, "Dismissing dialog");
}

Categories