0

I am designing a login screen and want to display a spinning wheel with "Please Wait..." text when clicked on Login button. If the username and password matches then next screen should be displayed else an error message should be displayed. My problem is that when the button is pressed process dialog is coming but next screen or the error message is not displayed after closing the dialog. I am having the following code. Please help me where i am doing wrong.

  package com.example.first_db_app;

  import java.util.List;

  import android.os.Bundle;
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.util.Log;
  import android.view.Menu;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;

 public class TestDbActivity extends Activity
 {




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


        DatabaseHandler db=new DatabaseHandler(this);


        //CRUD operations

        //inserting the records
        /*Log.d("Insert: ", "Inserting ..");
        db.addUser(new User("Ashwin","11111"));
        db.addUser(new User("Ravi","22222"));
        db.addUser(new User("Gopal","33333"));
        db.addUser(new User("Satish","44444"));*/


         // Reading all contacts
        Log.d("Reading: ", "Reading all contacts..");
        List<User> user = db.getAllContacts();       

        for (User us : user) {
            String log = "Name: " + us.getUname()  + " ,Password " + us.getUpwd();
                // Writing Contacts to log
        Log.d("Name: ", log);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test_db, menu);
        return true;
    }

    public void login(View v)
    {
        final  EditText edtuname=(EditText) findViewById(R.id.editTextUserNameToLogin);
        final  EditText edtpwd=(EditText) findViewById(R.id.editTextPasswordToLogin);
          Button btnlogin=(Button) findViewById(R.id.buttonSignIn);
         final DatabaseHandler db=new DatabaseHandler(this);

          btnlogin.setOnClickListener(new View.OnClickListener()
          {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub

                final ProgressDialog mypd=ProgressDialog.show(TestDbActivity.this, "", "Loading..",true);
                new Thread (new Runnable()
                {
                    @Override
                    public void run()
                    {
                        try
                        {

                            String uname= edtuname.getText().toString();
                            String pwd=edtpwd.getText().toString();
                            String stored_pwd=db.getUser(uname);
                            Thread.sleep(5000);

                            if(pwd.equals(stored_pwd))
                            {



                                //Toast.makeText(TestDbActivity.this,"successful", Toast.LENGTH_LONG).show();
                                mypd.dismiss();
                                setContentView(R.layout.activity_next);
                            }
                            else
                            {
                                mypd.dismiss();
                                Toast.makeText(TestDbActivity.this, "failed", Toast.LENGTH_LONG).show();

                            }
                        }
                        catch (Exception e)
                        {

                        }


                    }

                }).start();





            }
        } );

    }


}


Thanks in advance.
MAxeF
  • 139
  • 1
  • 10
user2179989
  • 109
  • 1
  • 1
  • 7
  • use async task class for this.http://stackoverflow.com/questions/9671546/asynctask-android-example – Qadir Hussain Apr 02 '13 at 13:19
  • I don't really like the scope of mypd to be within the onClickMethod and being shared with another thread. As the 2 posts below state, and asynctask is the elegant way to go about doing what you would like. – Brent Hronik Apr 02 '13 at 14:23
  • Thanks a lot @Qadir Hussain and Brent for the help.. – user2179989 Apr 03 '13 at 13:47

2 Answers2

2

You cannot display a Toast from a non-ui thread. Instead try displaying it as below:

TestDbActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(TestDbActivity.this, "failed", Toast.LENGTH_LONG).show();
    }
});

More preferably, I would suggest you to make use of AsyncTask here.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
1

I think the best option here will be to use AsyncTask, because it's running on background thread and while doing that you can show a progress dialog. Here is a simple example how can you achieve this :

private ProgressDialog progressDialog;

private class LoginUser extends AsyncTask<String, Void, Boolean>{

    private String username;
    private String password;

    @Override
    protected Boolean doInBackground(String... params) {

        if(params != null && params.length >= 1){
            username = params[0];
            password = params[1];

            // if everything is ok return true
            return true;

        }

        return false;
    }

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            progressDialog = new ProgressDialog(Test.this);
        } else {
            progressDialog = new ProgressDialog(Test.this,
                    AlertDialog.THEME_HOLO_LIGHT);
        }
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Logging in...");
        progressDialog.show();
    }

    @Override
    protected void onPostExecute(Boolean result){
        super.onPostExecute(result);
        progressDialog.dismiss();
        if(result){
            // open new activity
        } else {
            // show your error dialog.
        }

    }

}

and you can call it from your button's onClick like this :

btnlogin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) 
        {
          new LoginUser().execute("MyUsername", "MyPassword");
         }
});
hardartcore
  • 16,886
  • 12
  • 75
  • 101