Skip to main content
added 24 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Login activity androidin Android

thisThis is a complete login activity class ,

What I need to review ?

1- I need simplify my code as much as possible

2- is it wise to use butterknife library or normal way better

3- hide keyboard when user touch out away from edit text , and when press login.

4- best practise for volley library , should I use it individual in each class (login , sign up) orWhat I should make general function in api classneed reviewed:

  1. I need simplify my code as much as possible.
  2. Is it wise to use the butterknife library, or is the normal way better?
  3. Hide keyboard when user touches out away from edit text, and when selecting "login."
  4. Best practise for volley library. Should I use it individual in each class (login, sign up) or I should make a general function in an API class?

Login activity android

this complete login activity class ,

What I need to review ?

1- I need simplify my code as much as possible

2- is it wise to use butterknife library or normal way better

3- hide keyboard when user touch out away from edit text , and when press login

4- best practise for volley library , should I use it individual in each class (login , sign up) or I should make general function in api class

Login activity in Android

This is a complete login activity class.

What I need reviewed:

  1. I need simplify my code as much as possible.
  2. Is it wise to use the butterknife library, or is the normal way better?
  3. Hide keyboard when user touches out away from edit text, and when selecting "login."
  4. Best practise for volley library. Should I use it individual in each class (login, sign up) or I should make a general function in an API class?
Tweeted twitter.com/#!/StackCodeReview/status/649248797793624064
Source Link
Mina Fawzy
  • 527
  • 2
  • 10
  • 26

Login activity android

this complete login activity class ,

What I need to review ?

1- I need simplify my code as much as possible

2- is it wise to use butterknife library or normal way better

3- hide keyboard when user touch out away from edit text , and when press login

4- best practise for volley library , should I use it individual in each class (login , sign up) or I should make general function in api class

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.imaadv.leaynik.api.API;
import com.imaadv.leaynik.api.ConnectionExector.HTTPResponse;
import com.imaadv.leaynik.api.ConnectionExector.ResponseType;
import com.imaadv.leaynik.api.ConnectionExector.onHttpRequestListener;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.imaadv.leaynik.api.JsonParsing;
import com.imaadv.leaynik.api.MySingleton;
import com.imaadv.leaynik.defs.AppConstants;
import com.imaadv.leaynik.defs.userdata;
import com.imaadv.leaynik.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;


public class LoginActivity extends AppCompatActivity {


    private static final String TAG = LoginActivity.class.getSimpleName();

    @Bind(R.id.input_username)
    EditText _UsernameText;
    @Bind(R.id.input_password)
    EditText _PasswordText;
    @Bind(R.id.btn_login)
    Button _LoginButton;
    @Bind(R.id.tv_singup)
    TextView _SignUpButton;
    @Bind(R.id.checkbox_remember)
    CheckBox _chkRemember;
    @Bind(R.id.tv_forgetpassword)
    TextView forgetpassword;


    private static final int FIRST_ITEM_INDEX = 0;
    private API api;
    private ProgressDialog progressD;

    private LinearLayout loginLinear;
    private boolean remember_me = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signin);
        ButterKnife.bind(this);

        api = new API(this);

        loginLinear = (LinearLayout) findViewById(R.id.loginView);
        HideKeyboardFromView(loginLinear);


        init();



    }

    public static void hideSoftKeyboard(Activity act) {
        InputMethodManager mInputMange = (InputMethodManager) act
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMange.hideSoftInputFromWindow(act.getCurrentFocus()
                .getWindowToken(), 0);

    }

    public void HideKeyboardFromView(View view) {
        if (!(view instanceof EditText)) {
            view.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    hideSoftKeyboard(LoginActivity.this);
                    return false;
                }
            });
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

                View innerView = ((ViewGroup) view).getChildAt(i);
                HideKeyboardFromView(innerView);
            }
        }

    }


    @OnClick(R.id.btn_login)
    public void submitLogin(View view) {
        SingInMethod();

    }

    @OnClick(R.id.tv_singup)
    public void submitSingUp(View view) {
        SingUpMethod();
    }

    @OnClick(R.id.tv_forgetpassword)
    public void submitForgetPassword(View view){
        Forget_Password_method();
    }

    @OnCheckedChanged(R.id.checkbox_remember)
    public void checkbox(CheckBox cb , boolean checked){
        remember_me = checked ;
    }

    private void init() {
        String rem = api.getSetting(AppConstants.REMEMBER_ME);

        if (rem != null && rem.equals(AppConstants.TRUE) ) {
                _UsernameText.setText(api.getSetting(AppConstants.TAG_username));
        }

        progressD = new ProgressDialog(this);
        progressD.setCancelable(false);
        progressD.setMessage(getResources().getString(R.string.loading));
        progressD.setProgressStyle(ProgressDialog.STYLE_SPINNER);

    }

    private void get_user_data(userdata user) {
        final JsonParsing jsonParser = new JsonParsing();
        Map<String, String> params = new HashMap<>();
        params.put(AppConstants.TAG_username, user.username);
        params.put(AppConstants.TAG_userpassword, user.password);

        String URL = AppConstants.GeneralURL + AppConstants.ACTION + AppConstants.LOGIN;

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(URL, new JSONObject(params),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        progressD.hide();
                        try {
                            String res = response.getString(AppConstants.RESULT);

                            if (res.equals(AppConstants.FALSE)) {
                                String error = (String) response.get(AppConstants.MESSAGE);
                                Toast.makeText(LoginActivity.this, error, Toast.LENGTH_LONG).show();
                            } else if (res.equals(AppConstants.TRUE)) {
                                userdata user = jsonParser.getUserDataObject((JSONObject) response.get(AppConstants.MESSAGE));
                                api.setSetting(AppConstants.USER_ID, user.userid);
                                api.setSetting(AppConstants.TAG_username, user.username);
                                api.setSetting(AppConstants.TAG_age, user.age);
                                api.setSetting(AppConstants.TAG_useremail, user.email);
                                api.setSetting(AppConstants.TAG_user_photo,
                                        user.userimageURL);
                                if(remember_me){
                                    api.setSetting(AppConstants.REMEMBER_ME , AppConstants.TRUE);
                                    api.setSetting(AppConstants.IS_LOGGEDIN, AppConstants.TRUE);
                                }

                                Intent intent = new Intent(LoginActivity.this, Home.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(intent);

                                finish();


                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                progressD.hide();
                // handle error
            }
        });

        MySingleton.getInstance(this.getApplicationContext()).getResquestQueue().add(jsonObjectRequest);

    }

    private void SingUpMethod() {
        startActivity(new Intent(LoginActivity.this, Signup.class));

    }

    private void SingInMethod() {

        if (!validate()) {
            return;
        }

        String sPassword = _PasswordText.getText().toString();
        String sUserName = _UsernameText.getText().toString();

            progressD.show();
            userdata user = new userdata();
            user.password = sPassword;
            user.username = sUserName;
            if (api.isNetworkConnected()) {

                get_user_data(user);
            } else {
                Toast.makeText(LoginActivity.this,
                        getResources().getString(R.string.nointernet),
                        Toast.LENGTH_LONG).show();
            }

    }

    private void Forget_Password_method(){
        startActivity(new Intent(LoginActivity.this, ForgetPassword.class));
    }


    private boolean validate() {

        boolean valid = true;

        String sPassword = _PasswordText.getText().toString();
        String sUserName = _UsernameText.getText().toString();

        if (sPassword.isEmpty() ){
            _PasswordText.setError("Please enter Password");
            valid = false;
        }

        if(sUserName.isEmpty() ){
            _UsernameText.setError("Please Enter Valid E-mail");
            valid = false;
        }

            return valid;

    }
}