I want to show a ProgressDialog while jumping from my LoginActivity to my HomeActivity, basically while HomeActivity is loading.
I am using a separate thread to call the intent of the HomeActivity while I am showing the ProgressDialog from the main thread in LoginActivity.
Everything seems to be working fine, it is just that the ProgressDialog is displayed but there is no animated spinner.
I am wondering if i am doing this correctly.
Here the relevant code from LoginActivity:
public class LoginActivity extends AppCompatActivity {
private EditText userID, userPass;
private ProgressDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userID = (EditText) findViewById(R.id.edtX_srcLogin_userID);
userPass = (EditText) findViewById(R.id.edtX_srcLogin_userpass);
final Button bt_submit = (Button) findViewById(R.id.bT_scrLogin_submit);
bt_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
parseUserCredentials(userID.getText().toString(), userID.getText().toString());
}
});
}
private void parseUserCredentials(String userId, String userPassword) {
if ( userId.equals("userid") && userPassword.equals("1234") ) {
dialog = ProgressDialog.show(LoginActivity.this, "Checking credentials",
"Please wait...", true, false);
newThreadLoadActivity();
}else {
userID.setText("");
userPass.setText("");
userID.requestFocus();
dialog.cancel();
}
}
private void newThreadLoadActivity(){
new Thread() {
public void run() {
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}.start();
}
#Override
protected void onStop() {
super.onStop();
dialog.cancel();
}
}
EDIT
The main idea is to show the ProgressDialog whileHomeActivity loads, and cancel the ProgressDialog when HomeActivity is ready to be displayed.
Use the below code.It will make the delay for 10sec while moving to the next activity so your progress bar make be visible.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
dialog.cancel();
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}, 10000);
Related
This is my little test program. My problem is that from run() method I access to fields of wrong (old) Activity, which was destroyed after screen orientation changed. What's the way to handle this situation?
And, by the way, I must have my activity been recreated, because in real application I have different layouts for portrait and landscape modes!
public class MainActivity extends Activity {
private EditText edit;
private Button button;
private ProgressDialog progressDialog;
private boolean isLoginInProgress = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit_timer);
button = (Button) findViewById(R.id.btn_start);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
if (edit.getText().toString().length() == 0) throw new Exception();
long dTime = Long.parseLong(edit.getText().toString());
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
MainActivity.this.isLoginInProgress = false;
progressDialog.dismiss();
}
}, dTime);
progressDialog.show();
isLoginInProgress = true;
} catch (Exception e) {
Toast.makeText(MainActivity.this, "bad time value", Toast.LENGTH_SHORT).show();
}
}
});
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("loading");
if (savedInstanceState != null) { // activity is restarted
isLoginInProgress = savedInstanceState.getBoolean("fl_login");
edit.setText(savedInstanceState.getString("edit"));
}
if (isLoginInProgress) { // Show dialog again
progressDialog.show();
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("fl_login", isLoginInProgress);
outState.putString("edit", edit.getText().toString());
}
#Override
public void onDestroy(){
super.onDestroy();
progressDialog.dismiss();
}
}
You Can Use Database(SQLITE) for Storing Your Values..
This is my activity oncreate() method. I set a positive button ok with a dialogue window.
when I click on it, it navigates UI activity staticDisplay.class. I want to set a progress bar after clicking ok in the dialogue window until it loads the next activity.
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "ACTIVITY ONCREATE");
super.onCreate(savedInstanceState);
// setContentView(R.layout.dialog);
setContentView(R.layout.main);
if (Registration.isRunning == false) {
TextView title = new TextView(this);
title.setText("DM2");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
/* alert message */
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCustomTitle(title);
builder.setMessage(R.string.app_description).setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (Registration.isRunning == false) {
startService(new Intent(
getApplicationContext(),
Registration.class));
}
staticInfo();
if (Registration.ruid == null)
Registration.ruid = uId;
startActivity(new Intent(getApplicationContext(),
StatisticDisplay.class));
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
startActivity(new Intent(getApplicationContext(),
StatisticDisplay.class));
}
}
StaticDisplay.class on create method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statdisplay);
usage_btn = (Button) findViewById(R.id.appstat);
usage_btn.setOnClickListener(this);
setting_btn = (Button) findViewById(R.id.setting);
setting_btn.setOnClickListener(this);
}
you can use AysncTask for background process to be done and showing progressdialog as follows,
call the class
new Task().execute(CurrentActivity.this);
In Task Class:
private class Task extends AsyncTask<Void, Void, Void>
{
ProgressDialog dialog;
Context context;
public Task(Context context)
{
this.context=context;
//constructor for this class
}
protected void onPreExecute() {
//create the progress dialog as
dialog=new ProgressDialog(context);
}
protected Void doInBackground(Void... JSONArray) {
//Place your background process code
}
protected void onPostExecute(Void unused) {
//dismiss the progressdialog
dialog.dismiss();
}
}
This Code , Working For me
private ProgressDialog progress;
Button CallButton;
#Override
protected void onCreate(Bundle savedInstanceState)
{
progress = new ProgressDialog(Page.this);
CallButton=(Button)findViewById(R.id.button1);
CallButton.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
switch (v.getId()) {
case R.id.button1:
progress.setMessage("Please Wait Loading...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
new Thread()
{
public void run()
{
Intent i = new Intent(Page.this,Update.class);
startActivity(i);
progress.dismiss();
}
}.start();
break;
}
}
Please look at to following code snippet,
progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);
new Thread ( new Runnable()
{
public void run()
{
// your loading code goes here
}
}).start();
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg1)
{
progDailog.dismiss();
}
}
In the run() method, you can put your loading code.
I am using this simple code
public class Main extends Activity {
private ProgressDialog progressDialog;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//bouton Exemples de prix
findViewById(R.id.button1).setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
//start the progress dialog
runDialog(4);
Intent myIntent = new Intent(getBaseContext(), Exemple_prix.class);
startActivity(myIntent);
}
}
);
}
private void runDialog(final int seconds)
{
progressDialog = ProgressDialog.show(this, "", "Chargement...");
new Thread(new Runnable(){
public void run(){
try {
Thread.sleep(seconds * 1000);
progressDialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
The progressDialog doesn't show but it works if I disable
//startActivity(myIntent);
Your progress dialog is tied to the activity, so it shows on top of the first activity, then immediately you're starting the new activity which covers both the old activity and the progress dialog. Is there a particular reason why you're showing the progress dialog? If it's related to work in the second activity, you should show it there instead.
On a side note, don't create a new thread just to sleep like that. Just use postDelayed.
public class Talk extends Activity {
private ProgressDialog progDialog;
int typeBar;
TextView text1;
EditText edit;
Button respond;
private String name;
private String textAtView;
private String savedName;
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dorothydialog);
text1 = (TextView)findViewById(R.id.dialog);
edit = (EditText)findViewById(R.id.repsond);
respond = (Button)findViewById(R.id.button01);
respond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text1.setText("Welcome! Enter your name!");
respond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = edit.getText().toString();
text1.setText("Cool! your name is "+name);
}
});
}
});
}
}
Okay so i want to figure out how i would save the state of this activity. this is just a small snippet from my code to show you guys an example. So i want to save the state so when the activity is destroyed the user will come back where they left off.
Second thing, I would like to show a quick 5 second Progress dialog spinner between each button click.
For the second thing
This should work:
public class TestActivity extends Activity implements Runnable, OnClickListener {
private TextView tv;
private ProgressDialog pd;
private Button btn;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.tv);
btn = (Button)findViewById(R.id.btn);
tv.setText("initial text");
btn.setOnClickListener(this);
}
public void onClick(View v) {
pd = ProgressDialog.show(TestActivity.this, "Please wait...", "Details here", true, false);
Thread thread = new Thread(TestActivity.this);
thread.start();
}
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
pd.dismiss();
tv.setText("text after 5 sec passed");
}
};
}
I'm trying to make an activity that is asked for some result. This result is normally returned instantly (in the onCreate), however, sometimes it is nesesary to wait for some internet-content to download which causes the "loader"-activity to show. What I want is that the loader-activity don't display anything more than a progressdialog (and that you can still se the old activity calling the loader-activity in the background) and I'm wondering wheather or not this is possible.
The code I'm using as of now is:
//ListComicsActivity.java
public class ListComicsActivity extends Activity
{
private static final int REQUEST_COMICS = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list_comics);
Button button = (Button)findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intents.ACTION_GET_COMICS);
startActivityForResult(intent, REQUEST_COMICS);
}
});
}
/** Called when an activity called by using startActivityForResult finishes. */
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Toast toast = Toast.makeText(this, "The activity finnished", Toast.LENGTH_SHORT);
toast.show();
}
}
//LoaderActivity.java (answers to Intents.ACTION_GET_COMICS action-filter)
public class LoaderActivity extends Activity
{
private Intent result = null;
private ProgressDialog pg = null;
private Runnable returner = new Runnable()
{
public void run()
{
if(pg != null)
pg.dismiss();
LoaderActivity.this.setResult(Activity.RESULT_OK, result);
LoaderActivity.this.finish();
}
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String action = getIntent().getAction();
if(action.equals(Intents.ACTION_GET_COMICS))
{
Runnable loader = new Runnable()
{
public void run()
{
WebProvider.DownloadComicList();
Intent intent = new Intent();
intent.setDataAndType(ComicContentProvider.COMIC_URI, "vnd.android.cursor.dir/vnd.mymir.comic");
returnResult(intent);
}
};
pg = ProgressDialog.show(this, "Downloading", "Please wait, retrieving data....");
Thread thread = new Thread(null, loader, "LoadComicList");
thread.start();
}
else
{
setResult(Activity.RESULT_CANCELED);
finish();
}
}
private void returnResult(Intent intent)
{
result = intent;
runOnUiThread(returner);
}
}
It turns out you can do this by setting the activitys theme-attribute to #android:style/Theme.Dialog and calling this.setVisible(false); in the onCreate-method of the activity.