dmytrodanylyk / android-process-button

Android Buttons With Built-in Progress Meters.
http://dmytrodanylyk.com/pages/portfolio/portfolio-process-button.html
MIT License
3.01k stars 545 forks source link

How to use ActionProcessButton btnSignIn for Endless in AsyncTask #25

Closed 19rs39 closed 10 years ago

19rs39 commented 10 years ago
public class LoginActivity extends Activity implements ProgressGenerator.OnCompleteListener  {

   private ActionProcessButton btnSignIn;
    private FloatLabeledEditText mUserName, mPassword;
    private ArrayList<HashMap<String, String>> mLoginList = new ArrayList<HashMap<String,String>>();

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

        init();

        btnSignIn.setMode(ActionProcessButton.Mode.ENDLESS);

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new LoginTask().execute();
            }
        });
    }

    public class LoginTask extends AsyncTask<Void, Void, Void>
    {
        ProgressGenerator progressGenerator = new ProgressGenerator(LoginActivity.this);

        @Override
        protected void onPreExecute()
        {
//            progressGenerator.start(btnSignIn);
//
            btnSignIn.setEnabled(false);
            mUserName.setEnabled(false);
            mPassword.setEnabled(false);
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            String json_string = "";

            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("email", mUserName.getText().toString().trim()));
            postParameters.add(new BasicNameValuePair("password", mPassword.getText().toString()));

            WebServiceHandler wshObj = new WebServiceHandler();

                json_string = wshObj.getWebServiceData(Constants.login_url, postParameters);
                Parser parser = new Parser();
                mLoginList = parser.getLoginStatus(json_string);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
//            btnSignIn.setProgress(100);
            progressGenerator.dismiss();
//            ProgressGenerator.running = false;
            Log.e("done", "running false");

            if(mLoginList.size()>0)
            {
                if(mLoginList.get(0).get("status").contains("success")) {

                    SharedPreferences settings = getSharedPreferences(Constants.PREFS_LOGIN_DETAILS, 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("email", mUserName.getText().toString());
                    editor.putString("password", mPassword.getText().toString());
                    editor.commit();

                }
                else {
                    Toast.makeText(getApplicationContext(), "Invalid username or password.", Toast.LENGTH_SHORT).show();
                }
            }

            else {

                Toast.makeText(getApplicationContext(), mLoginList.get(0).get("status"), Toast.LENGTH_LONG).show();
                mUserName.setText("");
                mPassword.setText("");
            }
        }

    }

    public void init() {
        btnSignIn = (ActionProcessButton) findViewById(R.id.btnSignIn);
        mUserName = (FloatLabeledEditText) findViewById(R.id.activity_login_userName);
        mPassword = (FloatLabeledEditText) findViewById(R.id.activity_login_password);
    }

    @Override
    public void onComplete() {
        Toast.makeText(this, R.string.Loading_Complete, Toast.LENGTH_LONG).show();
    }
}
dmytrodanylyk commented 10 years ago

You don't need ProgressGenerator class. Please read User Guide

// you can display endless google like progress indicator
btnSignIn.setMode(ActionProcessButton.Mode.ENDLESS);
// set progress > 0 to start progress indicator animation
button.setProgress(1);