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?

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();

    EditText _UsernameText;
    EditText _PasswordText;
    Button _LoginButton;
    TextView _SignUpButton;
    CheckBox _chkRemember;
    TextView forgetpassword;

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

    private LinearLayout loginLinear;
    private boolean remember_me = false;

    protected void onCreate(Bundle savedInstanceState) {

        api = new API(this);

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



    public static void hideSoftKeyboard(Activity act) {
        InputMethodManager mInputMange = (InputMethodManager) act
                .getWindowToken(), 0);


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

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

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


    public void submitLogin(View view) {


    public void submitSingUp(View view) {

    public void submitForgetPassword(View view){

    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) ) {

        progressD = new ProgressDialog(this);


    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>() {
                    public void onResponse(JSONObject response) {
                        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.REMEMBER_ME , AppConstants.TRUE);
                                    api.setSetting(AppConstants.IS_LOGGEDIN, AppConstants.TRUE);

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



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

            public void onErrorResponse(VolleyError error) {
                // handle error



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


    private void SingInMethod() {

        if (!validate()) {

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

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

            } else {


    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;

  • \$\begingroup\$ 1. Is the part of the json request async? can you show it? 2. What is MySingleton class? \$\endgroup\$
    – Andrei T
    Commented Oct 5, 2015 at 20:51

2 Answers 2



As you can see here and here, the Butterknife library is advised to be used due to its simplicity that gets the job done with fewer lines of code.



  1. I would advise you to split your code into well-encapsulated parts.

    For example, I would put all my JSON request code in one class and pass a callback, in case you need to return something to the activity if you got a result from the network request. The starting of an activity after you got some results is a bit inefficient.

  2. Make smaller methods; they should not have more than 30-40 lines of code. Also, split the methods into independent logic. Do not create methods that have 100 lines of code and 3 kinds of logic inside.

    For example, if you want to filter a list and save the data to a cache, you have a method for filtering the list and another for saving the filtered list to the cache.

  3. Do not leave white spaces inside methods and between methods use max 1 line of space. Use some type of formatting.

Your code

I would create a callback that I can implement in my LoginActivity and pass this callback to the AsynTask/Thread that requests the data from the server. Once the server returns something I would return that via the callback you just passed to the asynctask (it can be a class that extends Thread, AsyncTask class or any other thread mechanism you may want to use).

The onResponse method should be split into 3 different methods:

  1. savingData or gettingData/parsingData.
  2. It should be createIntent or startIntent, etc.
  3. In case the response is false, showToast, etc.

it would be a better approach if you parse the data received in form of object via different method and you may also put these methods in a seperate class so they can be used for other activities needing the same adapter class

   //This method will get data from the web api
    private void getData(){
        //Showing a progress dialog
    final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);
Log.i("i m in","getdata()");
        //Creating a json array request
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,Config.DATA_URL,null,
                new Response.Listener<JSONObject>() {
                    public void onResponse(JSONObject response) {
                        Log.i("i m in","respinse()");

                        Log.d(String.valueOf(response),"check it");
                        //calling method to parse json array
                new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError error) {

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue
    private void parseData(JSONObject object){

        try {
            JSONObject data=object.getJSONObject("response");

            Log.d(String.valueOf(data),"data in response");
            JSONArray array= data.getJSONArray("docs");
            Log.d(String.valueOf(array.length()),"JSON ARRAY");

            for(int i = 0; i<array.length(); i++)

                JSONObject json = null;
                json = array.getJSONObject(Integer.parseInt(String.valueOf(i)));
                JavaBean superHero=new JavaBean();
               //getter and setter method class                

                    // add it to array list finally

             } catch (JSONException e) {e.printStackTrace();}

        //Finally initializing our adapter
        adapter = new CardAdapter(this,listSuperHeroes);

        //Adding adapter to recyclerview


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.