Last active
February 9, 2019 05:03
-
-
Save donrokzon/14e6e7ed37b699da54086393c37061a7 to your computer and use it in GitHub Desktop.
login xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:background="@color/colorPrimary" | |
android:fitsSystemWindows="true"> | |
<LinearLayout | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:focusable="true" | |
android:focusableInTouchMode="true" | |
android:paddingTop="56dp" | |
android:paddingLeft="24dp" | |
android:paddingRight="24dp"> | |
<ImageView android:src="@android:drawable/ic_menu_gallery" | |
android:layout_width="130dp" | |
android:layout_height="130dp" | |
android:layout_marginBottom="24dp" | |
android:layout_gravity="center_horizontal" /> | |
<!-- Email Label --> | |
<android.support.design.widget.TextInputLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="8dp" | |
android:layout_marginBottom="8dp"> | |
<EditText android:id="@+id/input_email" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:inputType="textEmailAddress" | |
android:hint="Email" /> | |
</android.support.design.widget.TextInputLayout> | |
<!-- Password Label --> | |
<android.support.design.widget.TextInputLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="8dp" | |
android:layout_marginBottom="8dp"> | |
<EditText android:id="@+id/input_password" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:inputType="textPassword" | |
android:hint="Password"/> | |
</android.support.design.widget.TextInputLayout> | |
<android.support.v7.widget.AppCompatButton | |
android:id="@+id/btn_login" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="24dp" | |
android:layout_marginBottom="24dp" | |
android:padding="12dp" | |
android:text="Login"/> | |
<TextView android:id="@+id/link_signup" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="24dp" | |
android:text="No account yet? Create one" | |
android:gravity="center" | |
android:textSize="16dip"/> | |
</LinearLayout> | |
</ScrollView> | |
///////Java Code///////// | |
import android.app.ProgressDialog; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.util.Patterns; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.toolbox.JsonObjectRequest; | |
import com.android.volley.toolbox.Volley; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import in.prayaninfotech.mynotes.NotesActivity; | |
import in.prayaninfotech.mynotes.R; | |
public class LoginActivity extends AppCompatActivity { | |
private EditText input_email, input_password; | |
private ProgressDialog progressDialog; | |
private String email = ""; | |
private String password = ""; | |
private String TAG=LoginActivity.class.getSimpleName(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
progressDialog = new ProgressDialog(this); | |
progressDialog.setMessage("Please wait.."); | |
input_email = findViewById(R.id.input_email); | |
input_password = findViewById(R.id.input_password); | |
findViewById(R.id.btn_login).setOnClickListener(view -> { | |
if (validated()) { | |
onclick(); | |
} | |
}); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
if (progressDialog != null && progressDialog.isShowing()) { | |
progressDialog.dismiss(); | |
} | |
} | |
boolean validated() { | |
email = input_email.getText().toString(); | |
password = input_password.getText().toString(); | |
if (TextUtils.isEmpty(email) || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) { | |
Toast.makeText(this, "Please enter a valid email", Toast.LENGTH_SHORT).show(); | |
return false; | |
} | |
if (TextUtils.isEmpty(password)) { | |
Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show(); | |
return false; | |
} | |
return true; | |
} | |
private void onclick() { | |
if (progressDialog != null) { | |
progressDialog.show(); | |
} | |
JSONObject jsonObject = new JSONObject(); | |
try { | |
jsonObject.put("email", email); | |
jsonObject.put("password", password); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, | |
Constants.URL, jsonObject, response -> { | |
Log.d(TAG, response.toString()); | |
if (progressDialog != null && progressDialog.isShowing()) { | |
progressDialog.dismiss(); | |
} | |
startActivity(new Intent(LoginActivity.this, NotesActivity.class).putExtra(Constants.KEY_EXTRA, String.valueOf(response))); | |
finish(); | |
}, error -> { | |
Log.d(TAG, error.toString()); | |
if (progressDialog != null && progressDialog.isShowing()) { | |
progressDialog.dismiss(); | |
} | |
}); | |
RequestQueue requestQueue = Volley.newRequestQueue(this); | |
requestQueue.add(jsonObjReq); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment