dawood rizwan
dawood rizwan

Reputation: 193

how to add progress dialog inside button

i am creating login functionality, usually user type their credentials and check their credentials to server whether it is authenticated or not. if it is correct, it will redirect to another page else it will show error message. so i am expecting to put one progress dialog loading for communication between server and client. could you please help me. this is my login button onclick function here actually i tried async task it is working for successfully login, but if enter wrong username password, i need to display this message.setText(s); how can do this?

@SuppressLint("NewApi")

  public void onClick(View arg0) {

    pd = new ProgressDialog(ParentLogin.this);
                pd.setMessage("Please wait...");
                pd.show(); 

                new AsyncTask<Void, Void, Void>()
                {
                    @Override
                    protected Void doInBackground(Void... params)
                    {
                        loginformation();
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result)
                    {
                        Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_SHORT).show();
                        Intent nextScreen = new Intent(getApplicationContext(), ParentPage.class);
                        startActivity(nextScreen);
                        finish();
                    }
                }.execute();
} 


public void loginformation(){


          un = username.getText().toString();
                        String pwd = password.getText().toString();
                        StringBuffer buffer = new StringBuffer();
                        StringBuffer checkBox2 = buffer.append(checkBox1.isChecked());
                        String checkBox=checkBox2.toString();
                        //String checkBox =ms_remember.getText().toString();                
                        if(un.trim().length() > 0 && pwd.trim().length() > 0){

                        try {
                            if (currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD){
                                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                                StrictMode.setThreadPolicy(policy);
                                }

                            HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());                   
                            urlc.setRequestProperty("User-Agent", "Test");                  
                            urlc.setRequestProperty("Connection", "close");                 
                            urlc.setConnectTimeout(15000);                  
                            urlc.connect();                 

                            if(urlc.getResponseCode() == 200) {                         

                                    HttpClient httpclient = new DefaultHttpClient();
                                    HttpPost httppost = new HttpPost(Urllink.url+"parent_login.php?key=agile89rise98&username="+un+"&password="+pwd);
                                    HttpResponse response = null;
                                    try {
                                        response = httpclient.execute(httppost);
                                        HttpEntity entity = response.getEntity();
                                        InputStream is = entity.getContent();
                                        //new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                                        String result, line = reader.readLine();
                                        result = line;
                                        while((line = reader.readLine()) != null) {
                                            result += line;
                                        }
                                         s = result;                                                            
                                        if(s.equals("1")) {

                                            session.createLoginSession(un);

                                             preference = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
                                             Editor editor1 = preference.edit();
                                             editor1.putString(UName, un);
                                             editor1.putString(Type, "p");
                                             editor1.commit();



                                            //preference activate of check box is enable
                                            if(checkBox.equals("true")) {
                                                Editor editor = sharedpreferences.edit();
                                                editor.putString(UserName, un);
                                                editor.putString(Password, pwd);                        
                                                editor.commit();
                                            }                                                                                                         
                                            if (sharedpreference.contains(GCMID))
                                            {
                                              gcm = sharedpreference.getString(GCMID, "");

                                                if(!prefs.getBoolean("secondTimes", false)) {                                       
                                                    //exceute SummaryAsyncTask()
                                                    new SummaryAsyncTask().execute((Void) null);                                        

                                                    SharedPreferences.Editor editors = prefs.edit();
                                                    editors.putBoolean("secondTimes", true);
                                                    editors.commit();
                                                }

                                            } else {                   
                                            if(!prefs.getBoolean("secondTime", false)) {                                        

                                                execute();                                      

                                                SharedPreferences.Editor editors = prefs.edit();
                                                editors.putBoolean("secondTime", true);
                                                editors.commit();
                                            }                                                                               
                                            }

                                            pd.dismiss;


                                        } else if(s.equals("0")) {
                                            message.setText("Incorrect Username/Passsword");
                                        } else {
                                            pd.dismiss();
                                            message.setText(s);
                                        }

                                    } catch (ClientProtocolException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                            } else {
                                    Toast.makeText(getApplicationContext(), "Please Connect Internet", Toast.LENGTH_SHORT).show();
                            }                   

                    } catch (IOException e) {

                        Toast.makeText(getApplicationContext(), "Please Connect Internet", Toast.LENGTH_SHORT).show();              
                        Log.e(TAG, "Error checking internet connection", e);
                    }          
                    } else {
                        Toast.makeText(getApplicationContext(), "Please enter Username & password.", Toast.LENGTH_SHORT).show();
                    }


             }

Upvotes: 0

Views: 324

Answers (1)

IshRoid
IshRoid

Reputation: 3735

Please use below loginformation method , You you have to use runOnUithread to work on UI inside doInBackground

    public void loginformation(){


        un = username.getText().toString();
                      String pwd = password.getText().toString();
                      StringBuffer buffer = new StringBuffer();
                      StringBuffer checkBox2 = buffer.append(checkBox1.isChecked());
                      String checkBox=checkBox2.toString();
                      //String checkBox =ms_remember.getText().toString();                
                      if(un.trim().length() > 0 && pwd.trim().length() > 0){

                      try {
                          if (currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD){
                              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                              StrictMode.setThreadPolicy(policy);
                              }

                          HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());                   
                          urlc.setRequestProperty("User-Agent", "Test");                  
                          urlc.setRequestProperty("Connection", "close");                 
                          urlc.setConnectTimeout(15000);                  
                          urlc.connect();                 

                          if(urlc.getResponseCode() == 200) {                         

                                  HttpClient httpclient = new DefaultHttpClient();
                                  HttpPost httppost = new HttpPost(Urllink.url+"parent_login.php?key=agile89rise98&username="+un+"&password="+pwd);
                                  HttpResponse response = null;
                                  try {
                                      response = httpclient.execute(httppost);
                                      HttpEntity entity = response.getEntity();
                                      InputStream is = entity.getContent();
                                      //new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                                      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                                      String result, line = reader.readLine();
                                      result = line;
                                      while((line = reader.readLine()) != null) {
                                          result += line;
                                      }
                                       s = result;                                                            
                                      if(s.equals("1")) {

                                          session.createLoginSession(un);

                                           preference = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
                                           Editor editor1 = preference.edit();
                                           editor1.putString(UName, un);
                                           editor1.putString(Type, "p");
                                           editor1.commit();



                                          //preference activate of check box is enable
                                          if(checkBox.equals("true")) {
                                              Editor editor = sharedpreferences.edit();
                                              editor.putString(UserName, un);
                                              editor.putString(Password, pwd);                        
                                              editor.commit();
                                          }                                                                                                         
                                          if (sharedpreference.contains(GCMID))
                                          {
                                            gcm = sharedpreference.getString(GCMID, "");

                                              if(!prefs.getBoolean("secondTimes", false)) {                                       
                                                  //exceute SummaryAsyncTask()
                                                  new SummaryAsyncTask().execute((Void) null);                                        

                                                  SharedPreferences.Editor editors = prefs.edit();
                                                  editors.putBoolean("secondTimes", true);
                                                  editors.commit();
                                              }

                                          } else {                   
                                          if(!prefs.getBoolean("secondTime", false)) {                                        

                                              execute();                                      

                                              SharedPreferences.Editor editors = prefs.edit();
                                              editors.putBoolean("secondTime", true);
                                              editors.commit();
                                          }                                                                               
                                          }

                                          pd.dismiss;


                                      } else if(s.equals("0")) {

                                          runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                 message.setText("Incorrect Username/Passsword");

                                            }
                                        });



                                      } else {
                                          pd.dismiss();

                                          runOnUiThread(new Runnable() {

                                                @Override
                                                public void run() {
                                                     message.setText(s);

                                                }
                                            });

                                      }

                                  } catch (ClientProtocolException e) {
                                      // TODO Auto-generated catch block
                                      e.printStackTrace();
                                  } catch (IOException e) {
                                      // TODO Auto-generated catch block
                                      e.printStackTrace();
                                  }
                          } else {
                              runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), "Please Connect Internet", Toast.LENGTH_SHORT).show();

                                }
                            });


                          }                   

                  } catch (IOException e) {

                      runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "Please Connect Internet", Toast.LENGTH_SHORT).show();

                            }
                        });


                      Log.e(TAG, "Error checking internet connection", e);
                  }          
                  } else {

                      runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "Please enter Username & password.", Toast.LENGTH_SHORT).show();

                        }
                    });


                  }


           }

Upvotes: 1

Related Questions