Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends AppCompatActivity {
- private static final String TAG = LoginActivity.class.getSimpleName();
- private Button button;
- private EditText editText;
- private EditText editText2;
- private ProgressDialog pDialog;
- private SessionManager session;
- private SQLiteHandler db;
- CheckBox show_password;
- String user_fullname, email, user_id,user_mobile, session_id, module;
- public LoginActivity(){
- }
- @Override
- public void onCreate(final Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- editText = (EditText) findViewById(R.id.editText);
- editText2 = (EditText) findViewById(R.id.editText2);
- button = (Button) findViewById(R.id.button);
- show_password = (CheckBox) findViewById(R.id.show_hide_password);
- // Progress dialog
- pDialog = new ProgressDialog(this);
- pDialog.setCancelable(false);
- // SQLite database handler
- db = new SQLiteHandler(getApplicationContext());
- // Session manager
- session = new SessionManager(getApplicationContext());
- // get the show/hide password Checkbox
- // Check if user is already logged in or not
- //if (session.isLoggedIn()) {
- // User is already logged in. Take him to main activity
- // Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- // startActivity(intent);
- // finish();
- // }
- // Login button Click Event
- show_password.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // checkbox status is changed from uncheck to checked.
- if (!isChecked) {
- // show password
- editText2.setTransformationMethod(PasswordTransformationMethod.getInstance());
- } else {
- // hide password
- editText2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
- }
- }
- });
- button.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- String username = editText.getText().toString().trim();
- String password = editText2.getText().toString().trim();
- // Check for empty data in the form
- if (!username.isEmpty() && !password.isEmpty()) {
- // login user
- checkLogin(username, password);
- } else {
- // Prompt user to enter credentials
- Toast.makeText(getApplicationContext(),
- "Please enter the credentials!", Toast.LENGTH_LONG)
- .show();
- }
- }
- });
- }
- /**
- * function to verify login details in mysql db
- */
- private void checkLogin(final String username, final String password) {
- pDialog.setMessage("Logging in ...");
- showDialog();
- RequestQueue requestQueue = Volley.newRequestQueue(this);
- String URL= "http://crm.sparshnow.com/api/index.php?username="+username+"&password="+password;
- final JsonObjectRequest strReq = new JsonObjectRequest(URL,null, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- Log.d(TAG, "Login Response: " + response);
- hideDialog();
- try {
- String success = response.getString("success");
- Log.d("response_value", success);
- if (success.equals("TRUE")) {
- user_fullname=response.getString("user_fullname");
- email=response.getString("email");
- user_id=response.getString("user_id");
- user_mobile=response.getString("user_mobile");
- session_id=response.getString("session_id");
- Intent intent = new Intent(LoginActivity.this,
- MainActivity.class);
- intent.putExtra("user_fullname",user_fullname);
- intent.putExtra("email",email);
- intent.putExtra("user_mobile",user_mobile);
- intent.putExtra("session_id", session_id);
- session.setLogin(true);
- startActivity(intent);
- finish();
- } else {
- Toast.makeText(getApplicationContext(), "Enter Correct Detail ", Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- },
- new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError success) {
- Log.e(TAG, "Login Error: " + success.getMessage());
- Toast.makeText(getApplicationContext(),
- success.getMessage(), Toast.LENGTH_LONG).show();
- hideDialog();
- }
- });
- requestQueue.add(strReq);
- }
- private void showDialog() {
- if (!pDialog.isShowing())
- pDialog.show();
- }
- private void hideDialog() {
- if (pDialog.isShowing())
- pDialog.dismiss();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement