How to start page loading animation and stop on page loaded - android

I am developing one Android application which is connecting to Web Service. How can I keep page loading animation while redirecting to another page ? I have tried following code but whenever I have pressed back button the loading animation is remains on previous page. And sometimes blank screen is coming. Please help me to improve my code and help me to resolve this issue.
public void redirection()
{
ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "","Please wait...", true);
Intent i = new Intent(this, SecondClass.class);
startActivity(i);
}

Follow that link http://www.androidhive.info/2013/06/android-working-with-xml-animations/
Here define the animations
method start
#Override
public void onAnimationStart(Animation animation) {
}
method end
#Override
public void onAnimationEnd(Animation animation) {
}

use asytask in this process...
public class asynclass extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "","Please wait...", true);
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
//do your work which u want
}
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
dialog .dismissDialog();
//and here u can pass intent if u want to pass
}
}

Hope that you are using AsyncTask for to download your data ..
Initiate progress dialog as
Private ProgressDialog pDialog;
Try putting this code on onPreExecute()..
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(getParent());
pDialog.setMessage("Please wait ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
And on PostExcute stop progress dialog after you completly downloaded data as
pDialog.dismiss();

Related

AsyncTask not even starting

I was sitting 5 hours on one code with AyncTask which was not running properly. I just created another simple Activity (because in last one onPostExecute() wasn't working) and now this simple Activity is also not starting the AsyncTask. Can anyone see what I'm doing wrong?
public class ServerStatus extends Activity {
Context context;
private ProgressDialog pd;
int a;
TextView test;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.server_status);
context=this;
test=(TextView) findViewById(R.id.welcomemessage);
new Download().execute();
}
public class Download extends AsyncTask<Void, Void, Void>{
protected Void onPreExecute(Void... arg0) {
pd = new ProgressDialog(context);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
return null;
}
#Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a++;
return null;
}
protected Void onPostExecute(Void... arg0) {
if (pd!=null)
pd.dismiss();
test.setText(a);
return null;
}
}
}
Also, does NavigationDrawer block UI thread? Because I can't even update TextView when I implement it.
The methods aren't correct. You should add the #Override annotation to them so it will yell at you when doing it wrong.
onPreExecute() doesn't take any params so it should be
#Override
protected Void onPreExecute() {
also change the param type of `onPostExecute() to
#Override
protected Void onPostExecute(Void arg0) {
Remove the "..." See Varargs for an explanation.
Docs
Post explaining AsyncTask and getting values
those params in the class declaration are for doInBackground(), onProgressUpdate(), and onPostExecute()
As far as the NavDrawer, I'm not sure what issue you are having with that.
You need to change your code in onPreExecute
pd = new ProgressDialog(context);
to
pd = new ProgressDialog(getActivity());

How can I retain an instance of fragment in Android?

I made a fragment and I am showing a progressDialog. When the orientation changes my progress dialog doesn't show again, while I am using a setRetainInstance(true).
Code is
public class FragmentTest extends Fragment implements OnClickListener
{
Button proceed;
ProgressDialog pDialog;
#Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
if(pDialog!=null)
{
pDialog.show();
}
}
class inner extends AsyncTask
{
#Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setMessage("Processing...");
pDialog.setCancelable(false);
pDialog.setMax(900000000);
pDialog.show();
}
#Override
protected Object doInBackground(Object... arg0)
{
int k=0;
for(int i=0;i<10000000;i++)
{
pDialog.incrementProgressBy(i);
for(int j=0;j<10000;j++)
{
k=k+j;
}
}
// TODO Auto-generated method stub
return null;
}
#Override
protected void onPostExecute(Object result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
System.out.println("enter here");
pDialog.dismiss();
}
}
#Override
public void onStop()
{
// TODO Auto-generated method stub
super.onStop();
if (pDialog!=null && pDialog.isShowing()){
pDialog.dismiss();
}
}
}
But my progress dialog is not showing. Can any one tell me how to solve the problem?
You need to create the instance of ProgressDialog before you show.
Best approach is to create a method showProgress
private void showProgress(){
pDialog = new ProgressDialog(getActivity());
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setMessage("Processing...");
pDialog.setCancelable(false);
pDialog.setMax(900000000);
pDialog.show();
}
And call it when ever required to show the progress dialog.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
showProgress();
}

unable to show a progress bar properly

i want to show in my activity a progress bar as a response to a button click.
i read in another question that i should use async task in order to show/not show the progress bar but when i click on the button the progress bar is not shown properly (it appears for much less time then it should)
any suggestions?
the activity code:
public void chooseContactFromList(View view){
ProgressBar pBar = (ProgressBar) findViewById(R.id.progressBar1);
circleActivity progressTask = (circleActivity) new circleActivity(pBar).execute();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
CharSequence[] cs=nameList.toArray(new CharSequence[nameList.size()]);
builder.setTitle("Make your selection");
builder.setItems(cs, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
reciverNumber = phoneList.get(item);
}
});
AlertDialog alert = builder.create();
alert.show();
progressTask.cancel(true);
}
the AsyncTask code:
public class circleActivity extends AsyncTask<Void, Void, Void> {
private ProgressBar progressBar;
public circleActivity(ProgressBar pBar) {
progressBar=pBar;
}
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
#Override
protected void onPostExecute(Void result) {
progressBar.setVisibility(View.INVISIBLE);
}
#Override
protected void onProgressUpdate(Void ... progress) {
}
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return null;
}
}
thanks
As you are doing nothing in the doInBackground(), the progressBar is shown for few moments only. If you really want to see it, then try doing some operation in doInBackground() which will take some time.
eg. Try Thread.sleep(1000); in doInBackground to test it.
And I suggest you to refer following links.
http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html
http://developer.android.com/reference/android/os/AsyncTask.html

Use progressBar instead of ProgressDialog

I want to use progress bar while downloading from server,Actually i was using progressDialog but it will be good look if i use progressBar instead of progressDialog.I have following code for Progress dialog .
public class FeaturedData extends AsyncTask<Void, Void, Void> {
Home home;
ProgressDialog dialog = null;
public FeaturedData(Home home) {
// TODO Auto-generated constructor stub
this.home = home;
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
//calling here method
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = ProgressDialog.show(home, "", "", true);
}
As per your below comment:
Actually i want to do it programatically don't want to declare in xml.
=> i would suggest you to take it inside the XMl layout.
Inside the onPreExecute(), make it visible using progressBar.setVisibility(View.VISIBLE)
and inside the onPostExecute(), make it GONE by using progressBar.setVisibility(View.GONE)

outofmemoryerror in android

I have one running some methods in doInBackground in AsyncTask ,which is calling webservices and inserting number of records into the database , at the same user click on other screen then outOfmemoryError is coming I want to stop the background thread temporarily till the screen loads and then thread has to resume from it stopped such that the memory will release. can anybody tell me how to achieve this.
Thanks in Advance.
The Asynctask can be executed only once (an exception will be thrown if a second execution is attempted.).. so to pause and resume .. i think you should use a backgroundthread instead...
//decleration
ProgressDialog dialog;
//onCreate
dialog = new ProgressDialog(ActivityName.this);
public class BackGroundTask extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
//perform background task
return null;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setMessage("Please wait.....");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
#Override
protected void onPostExecute(String sResponse) {
if (dialog.isShowing())
{
dialog.dismiss();
}
}

Categories

Resources