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.