3
\$\begingroup\$

I am completely unaware of all the Android features and available libraries.

I just designed a login system to practice it. I have referred to a few books and tutorials.

Layout

I am using relative layout. And have two EditText and one button for login and one button to start register activity.

Activity

public class LoginActivity extends AppCompatActivity {

    private EditText email, password;
    ProgressDialog progress;

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

        email = (EditText) findViewById(R.id.email);
        password = (EditText) findViewById(R.id.password);
        progress = new ProgressDialog(this);
    }

    public void checkLogin(View view){
        final String emailText = email.getText().toString().trim();
        final String passwordTex = password.getText().toString().trim();

        Response.Listener<String> successListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonResponse = new JSONObject(response);
                    if(jsonResponse.getInt("status") == 0){
                        Toast.makeText(getApplicationContext(), jsonResponse.getString("message"), Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(LoginActivity.this, SubjectActivity.class);
                        startActivity(intent);
                    }else if(jsonResponse.getInt("status") == 1 || jsonResponse.getInt("status") == -2){
                        Toast.makeText(getApplicationContext(), jsonResponse.getString("message"), Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(getApplicationContext(), "Serious error.", Toast.LENGTH_LONG).show();
                    }
                }catch(JSONException e){

                }
                progress.hide();
            }
        };

        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progress.hide();
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
            }
        };

        if(emailText.isEmpty() || passwordTex.isEmpty()){
            Toast.makeText(getApplicationContext(),"Please enter all the details",Toast.LENGTH_LONG).show();
        }else {
            progress.setMessage("Logging in");
            progress.show();
            StringRequest req = new StringRequest(Request.Method.POST, Constants.LOGIN_URL, successListener, errorListener) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("email", emailText);
                    params.put("password", passwordTex);
                    return params;
                }
            };

            ReqQueue.getInstance().getRequestQueue().add(req);
        }
    }

    public void goToRegisterLayout(View view){
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    } //It will start register activity if register button is clicked
}

I am using Volley to read from PHP.

My Queue class as follows

public class ReqQueue extends Application{
    private static ReqQueue instance;
    private RequestQueue requestQueue;
    private Context context;

    @Override
    public void onCreate(){
        super.onCreate();
        context = getApplicationContext();
        instance = this;
    }

    public static synchronized ReqQueue getInstance(){
        return instance;
    }

    public RequestQueue getRequestQueue(){
        if(requestQueue == null){
            requestQueue = Volley.newRequestQueue(context);
        }
        return  requestQueue;
    }

    public <T> void addToReqQueue(Request<T> req){
        getRequestQueue().add(req);
    }
}

If you legends suggest me some other way, I am open to accept it. Every single point from you will really help me. You can suggest new best way of doing the same.

\$\endgroup\$
2
\$\begingroup\$
  1. Variables names are a little misleading - I'd expect email to be a String, not a View. Try emailView or emailTextView, or even better, since they're private mEmailTextView.

  2. All the methods in LoginActivity are public. There's very little chance that any of those methods are going to be accessed outside of the instance itself, they can probably be private.

  3. Your ProgressDialog is package-private, just because it's default. If you want something package-private, that's fine, but you probably don't (and if you do, you probably want to comment it). Generally, go for as private as possible (private, protected, package-private, public, in that order).

  4. You have large blocks of code that aren't super easy to read. Try breaking your code up into bite-sized methods with descriptive names. Java can get very verbose, so modularity and organization goes a long way.

  5. There is a library called "Gson" which converts JSON to Java objects that's very nice and clean and eliminates tons of boilerplate.

  6. Why is getInstance synchronized?

  7. Is there a reason that addToReqQueue is generic? I can't see that it's doing anything (the generic type is not used in the method body).

  8. Convention is that most Strings are stored somewhere (either as constants, or in a file, or something). This isn't critical, though.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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