1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| public class Login_Fragment extends Fragment implements View.OnClickListener {
private static View view;
private static EditText emailid, password;
private static Button loginButton;
private static TextView signUp;
private static CheckBox show_hide_password;
private static LinearLayout loginLayout;
private static Animation shakeAnimation;
private static FragmentManager fragmentManager;
public Login_Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.login_layout, container, false);
initViews();
setListeners();
return view;
}
// Initiate Views
private void initViews() {
fragmentManager = getActivity().getSupportFragmentManager();
emailid = (EditText) view.findViewById(R.id.login_emailid);
password = (EditText) view.findViewById(R.id.login_password);
loginButton = (Button) view.findViewById(R.id.loginBtn);
signUp = (TextView) view.findViewById(R.id.createAccount);
show_hide_password = (CheckBox) view
.findViewById(R.id.show_hide_password);
loginLayout = (LinearLayout) view.findViewById(R.id.login_layout);
// Load ShakeAnimation
shakeAnimation = AnimationUtils.loadAnimation(getActivity(),
R.anim.shake);
// Setting text selector over textviews
XmlResourceParser xrp = getResources().getXml(R.drawable.text_selector);
try {
ColorStateList csl = ColorStateList.createFromXml(getResources(),
xrp);
show_hide_password.setTextColor(csl);
signUp.setTextColor(csl);
} catch (Exception e) {
}
}
// Set Listeners
private void setListeners() {
loginButton.setOnClickListener(this);
signUp.setOnClickListener(this);
// Set check listener over checkbox for showing and hiding password
show_hide_password
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button,
boolean isChecked) {
// If it is checkec then show password else hide
// password
if (isChecked) {
show_hide_password.setText(R.string.hide_pwd);// change
// checkbox
// text
password.setInputType(InputType.TYPE_CLASS_TEXT);
password.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());// show password
} else {
show_hide_password.setText(R.string.show_pwd);// change
// checkbox
// text
password.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setTransformationMethod(PasswordTransformationMethod
.getInstance());// hide password
}
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginBtn:
checkValidation();
break;
// Replace forgot password fragment with animation
case R.id.createAccount:
// Replace signup frgament with animation
fragmentManager
.beginTransaction()
.setCustomAnimations(R.anim.right_enter, R.anim.left_out)
.replace(R.id.frameContainer, new SignUp_Fragment(),
Utils.SignUp_Fragment).commit();
break;
}
}
// Check Validation before login
private void checkValidation() {
// Get email id and password
String getEmailId = emailid.getText().toString();
String getPassword = password.getText().toString();
// Check patter for email id
Pattern p = Pattern.compile(Utils.regEx);
Matcher m = p.matcher(getEmailId);
// Check for both field is empty or not
if (getEmailId.equals("") || getEmailId.length() == 0
|| getPassword.equals("") || getPassword.length() == 0) {
loginLayout.startAnimation(shakeAnimation);
new CustomToast().Show_Toast(getActivity(), view,
"Enter both credentials.");
}
// Check if email id is valid or not
else if (!m.find())
new CustomToast().Show_Toast(getActivity(), view,
"Your Email is Invalid.");
// Else do login and do your stuff
else {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(LoginInterface.LOGINURL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
LoginInterface api = retrofit.create(LoginInterface.class);
Call<String> call = api.getUserLogin(getEmailId, getPassword);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
Log.i("Responsestring", response.body().toString());
Log.i("onSuccess", response.body().toString());
String jsonresponse = response.body().toString();
try {
JSONObject jsonObject = new JSONObject(jsonresponse);
if (jsonObject.getString("success").equals("true")) {
Toast.makeText(getActivity(), "Login Successfully!", Toast.LENGTH_SHORT)
.show();
Intent intent;
intent = new Intent(getActivity(), activity_list_events.class);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} else {
loginLayout.startAnimation(shakeAnimation);
new CustomToast().Show_Toast(getActivity(), view,
"Your Password is Invalid.");
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(getContext(), "Server invalid \n" + t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
} |
Partager