0

I'm having a problem when I click on the button and the progress dialog doesn't show out and directly to other activity. What I want to do is, the progress dialog appear before I go to the other activity. I'm trying to get an nice popup dialog box that shows when the btn_create is clicked but it just isn't showing. Does anyone has solution?

Here's my code at the moment:

DateFormat formatDateTime = DateFormat.getDateTimeInstance();
Calendar mCurrentDate = Calendar.getInstance();

private EditText mEventTitle;
private EditText mEventLoc;
private EditText mEventDesc;
private TextView text_date;
private TextView text_time;
private ImageButton btn_date;
private ImageButton btn_time;
private Button btn_create;
private Button btn_discard;
private int day,month,year,hour,min;

private DatabaseReference mEventDatabase;
private DatabaseReference mUsersDatabase;
private FirebaseUser mCurrentUser;

private ProgressDialog mPDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_new_event);

    mEventTitle = (EditText) findViewById(R.id.eventTitle);
    mEventLoc=(EditText)findViewById(R.id.eventLocation);
    mEventDesc=(EditText)findViewById(R.id.eventDesc);
    text_date = (TextView) findViewById(R.id.eventDate);
    text_time = (TextView) findViewById(R.id.eventTime);
    btn_date = (ImageButton) findViewById(R.id.pickDateBtn);
    btn_time = (ImageButton) findViewById(R.id.pickTimeBtn);
    btn_create=(Button)findViewById(R.id.createEvent);
    btn_discard=(Button)findViewById(R.id.discardEvent);

    mPDialog = new ProgressDialog(AddNewEvent.this);

    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
    mEventDatabase = FirebaseDatabase.getInstance().getReference().child("Event");
    mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(mCurrentUser.getUid());



    btn_create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            updateEvent();
        }
    });


}



private void updateEvent() {

    mPDialog.setTitle("Posting...");
    mPDialog.setMessage("Please wait...");
    mPDialog.setCanceledOnTouchOutside(false);

    String d = text_date.getText().toString();
    String t = text_time.getText().toString();
    final String dt = d+"  "+t;

    final String title_val = mEventTitle.getText().toString().trim();
    final String loc_val = mEventLoc.getText().toString().trim();
    final String desc_val = mEventDesc.getText().toString().trim();
    final String current_uid = mCurrentUser.getUid();

    if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(loc_val) && !TextUtils.isEmpty(desc_val) && !TextUtils.isEmpty(dt)){

        mPDialog.show();

        final DatabaseReference newEvent = mEventDatabase.push();

        SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy 'at' hh:mma");
        Date date = new Date();
        final String currentDate = df.format(date);

        mUsersDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                newEvent.child("title").setValue(title_val);
                newEvent.child("location").setValue(loc_val);
                newEvent.child("desc").setValue(desc_val);
                newEvent.child("date&time").setValue(dt);
                newEvent.child("uid").setValue(current_uid);
                newEvent.child("postDate").setValue(currentDate);
                newEvent.child("username").setValue(dataSnapshot.child("name").getValue());

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        mPDialog.dismiss();

        Intent mainIntent = new Intent(AddNewEvent.this, UpcomingEvent.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(mainIntent);
        finish();
    }

}
nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
steven
  • 25
  • 9
  • you can hold it for few seconds by using postdelayed handler then start the activity – Samad Nov 20 '17 at 15:01
  • This would be because dialog displaying code is asynchronous. Showing dialog **will not** pause the code or do anything of the sort. You are dismissing the dialog in the same function it's created, which means it will happen right before device even had a chance to show it. – M. Prokhorov Nov 20 '17 at 15:01
  • Do you sure that `mPDialog.show();` get called? – nhoxbypass Nov 20 '17 at 15:02
  • @Delta7 can you sho me how to use postdelayed to handler then start the activity? – steven Nov 24 '17 at 05:29
  • https://stackoverflow.com/questions/42379301/how-to-use-postdelayed-correctly-in-android-studio – Samad Nov 25 '17 at 11:52
  • @Delta7 Where should i put the postdelayed ? – steven Nov 25 '17 at 12:20

2 Answers2

0

If you ONLY want to start other activity when dialog already dismissed. Use this inside updateEvent() before showing the dialog:

mPDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    public void onDismiss(DialogInterface dialog) {
        Intent mainIntent = new Intent(AddNewEvent.this, UpcomingEvent.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
        Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(mainIntent);
        finish();
    }
});

mPDialog.show();

Then whenever your dialog dismissed (by using mPDialog.dismiss() or by pressing cancel,..) it will call onDismiss(). Then it's the time to start new Activity.

nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
  • i have tried the code you posted, but i still cant get the progress dialog showed. – steven Nov 24 '17 at 05:33
  • @steven where did you put this code and do you sure that mPDialog.show(); get called? – nhoxbypass Nov 24 '17 at 06:33
  • i put the code inside the updateEvent(), inside the if statement, before the mUserdatabase. addValueListener – steven Nov 24 '17 at 07:05
  • where should i set the mPDialog.dismiss()? – steven Nov 24 '17 at 07:14
  • But first I want to ask you what are you want to do in AddNewEvent Activity and what is UpcomingEvent Activity and why you need `addValueEventListener` before dismiss the progress dialog. – nhoxbypass Nov 24 '17 at 07:19
  • In AddNewEvent Activity, i will allow user to key in the value and i need the addValueEventListener for me to add value into the mUserdatabase, after the value added into the database( which is after the addValueEventListener), the progress dialog will dismiss and go into the UpcomingEvent Activity. – steven Nov 24 '17 at 07:34
  • So the right place to put mPDialog.dismiss() is inside `onDataChange` and `onCancelled`. Try it. Btw, I don't think push data into firebase need `addValueEventListener` – nhoxbypass Nov 24 '17 at 09:35
  • i have tried to put mPDialog.dismiss() either inside onDataChange and onCancelled, it directly send me to the new UpcomingEvent Activity without showing the progress dialog :( I get the addValueEventListener from the developer page of Firebase, do you have any suggestion? – steven Nov 24 '17 at 14:22
  • @steven because `onDataChange` called too soon. The process that push data in Firebase is very fast, so if you want your progress dialog show I think you need a `postDelayed` that delay the `dismiss()` operation for a specific time. But it's not a good practices that FORCE user to wait when they don't have to – nhoxbypass Nov 24 '17 at 14:31
  • okay thanks for your suggestion, i will change it to a TOAST message – steven Nov 24 '17 at 14:44
  • Yeah you should do it. Btw about the WRITE operation, you could only set data to POJO then push to Firebase without `addValueEventListener`. See this link: https://firebase.google.com/docs/database/android/read-and-write at section "Basic write operations" – nhoxbypass Nov 24 '17 at 14:49
  • Thank you :) btw, because im writing different sets of data that's why im using addValueEventListener. Are you familiar with Firebase too? – steven Nov 24 '17 at 15:42
  • @steven yes I did implement Firebase in some of my project :D – nhoxbypass Nov 24 '17 at 15:44
  • That's great :) do you know how to select and upload a text file into Firebase and retrieve it showing in a list? Can you show me an example? – steven Nov 24 '17 at 17:15
  • @steven If you want to upload files to Firebase you need to use Firebase storage. But it's a text file so you cannot use `addValueListener` to retrieve data as list. The only way you can do is (1). Upload txt file, retrieve txt file, read and parse to list. (2). Read and parse txt file, write to Firebase database (instead of saving txt file). Then retrieve as normally. – nhoxbypass Nov 26 '17 at 03:02
  • I'm using putFile().addOnSuccessListener to upload my text file to FirebaseStorage, is it same as uploading an image to FirebaseStorage? I don't know how to retrieve the text file from FirebaseStorage and show it in list.. Could you please show me how to do it? – steven Nov 26 '17 at 04:28
  • @steven No you can't. The only way you can do is (1). Upload txt file, retrieve txt file to phone, read and parse to list. (2). Read and parse txt file in under your device, write to Firebase database (instead of saving txt file). Then retrieve as normally. – nhoxbypass Nov 26 '17 at 05:01
  • Could you please show me an example code on how to write it? Please:( I really need it to finish my assignment before tonight. Hope you can help me out – steven Nov 26 '17 at 06:22
  • compare to using Dropbox api, which one is easier? – steven Nov 26 '17 at 06:58
  • @steven you should ask another question. May be other guys have better solution for this – nhoxbypass Nov 26 '17 at 08:50
0

The problem in your code is the sequence of the operations: you perform a mPDialog.show(); and right after that you do mPDialog.dismiss(); and start the activity.

You should instead show the dialog, and on button pressed start the activity like this:

AlertDialog mPDialog = new AlertDialog.Builder(this)
    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
        Intent mainIntent = new Intent(AddNewEvent.this, UpcomingEvent.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(mainIntent);
        finish();)
.show();

Or put the code for starting the activity inside your button OnClickListener().

dalla92
  • 432
  • 2
  • 8
  • thanks for you reply, but what i need is Progress dialog instead of using Alert Dialog. I have added and tried the code you posted, but that's error with the code. – steven Nov 24 '17 at 05:29