Advertisement
myalphb

ActivityRegister

Aug 28th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.45 KB | None | 0 0
  1. package sit.elderlylocate.Register;
  2.  
  3. import android.app.Activity;
  4. import android.app.DatePickerDialog;
  5. import android.app.Dialog;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.DatePicker;
  13. import android.widget.EditText;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17.  
  18. import sit.elderlylocate.Login.ActivityLogin;
  19. import sit.elderlylocate.R;
  20.  
  21.  
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.net.HttpURLConnection;
  26. import java.net.MalformedURLException;
  27. import java.net.URL;
  28.  
  29. import android.app.AlertDialog;
  30.  
  31.  
  32.  
  33.  
  34. public class ActivityRegister extends Activity {
  35.  
  36. EditText username, password, confirmpass, firstname, lastname;
  37. TextView birthday;
  38. String Username, Password, Confirmpass, Firstname, Lastname, Birthday;
  39. Context ctx = this;
  40.  
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.activity_register);
  45. username = (EditText) findViewById(R.id.txtUsername);
  46. password = (EditText) findViewById(R.id.txtPassword);
  47. confirmpass = (EditText) findViewById(R.id.txtConfirmPass);
  48. firstname = (EditText) findViewById(R.id.txtFirstname);
  49. lastname = (EditText) findViewById(R.id.txtLastname);
  50. birthday = (TextView) findViewById(R.id.txtBirthday);
  51.  
  52. showDialogOnButtonClick();
  53.  
  54. // btnClickRegister
  55. final Button btnRegister = (Button) findViewById(R.id.RegisterButton2);
  56. // Perform action on click
  57. btnRegister.setOnClickListener(new View.OnClickListener() {
  58. public void onClick(View v) {
  59. checkDada();
  60. RegisterButton2(v);
  61.  
  62. Intent i = new Intent(getApplicationContext(), ActivityLogin.class);
  63. startActivity(i);
  64.  
  65.  
  66. }
  67. });
  68.  
  69. }
  70.  
  71. // Get birthday data [choose date]
  72. Button bDate;
  73. int year_x,month_x,day_x;
  74. static final int DIALOG_ID = 0;
  75.  
  76. public void showDialogOnButtonClick(){
  77. bDate = (Button)findViewById(R.id.bDate);
  78.  
  79. bDate.setOnClickListener(
  80. new View.OnClickListener() {
  81. @Override
  82. public void onClick(View v) {
  83. showDialog(DIALOG_ID);
  84. }
  85. }
  86. );
  87. }
  88.  
  89. protected Dialog onCreateDialog(int id){
  90. if (id==DIALOG_ID){
  91. return new DatePickerDialog(this, dpickerListner, year_x, month_x, day_x);
  92. }
  93. return null;
  94. }
  95.  
  96. public DatePickerDialog.OnDateSetListener dpickerListner =
  97. new DatePickerDialog.OnDateSetListener() {
  98. @Override
  99. public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  100. year_x = year;
  101. month_x = monthOfYear+1;
  102. day_x = dayOfMonth;
  103. //Toast.makeText(ActivityRegister.this, year_x + "-" + month_x + "-" + day_x,Toast.LENGTH_LONG).show();
  104. birthday.setText(year_x + "-" + month_x + "-" + day_x);
  105. }
  106. };
  107.  
  108.  
  109. public boolean checkDada() {
  110. // Error Dialog
  111. final AlertDialog.Builder ad = new AlertDialog.Builder(this);
  112.  
  113. ad.setTitle("Error! ");
  114. ad.setIcon(android.R.drawable.btn_star_big_on);
  115. ad.setPositiveButton("Close", null);
  116.  
  117. // Check Username
  118. if (username.getText().length() == 0) {
  119. ad.setMessage("Please input [Username] ");
  120. ad.show();
  121. username.requestFocus();
  122. return false;
  123. }
  124. // Check Password
  125. if (password.getText().length() == 0 || confirmpass.getText().length() == 0) {
  126. ad.setMessage("Please input [Password/Confirm Password] ");
  127. ad.show();
  128. password.requestFocus();
  129. return false;
  130. }
  131. // Check Password and Confirm Password (Match)
  132. if (!password.getText().toString().equals(confirmpass.getText().toString())) {
  133. ad.setMessage("Password and Confirm Password Not Match! ");
  134. ad.show();
  135. confirmpass.requestFocus();
  136. return false;
  137. }
  138. // Check Name
  139. if (firstname.getText().length() == 0) {
  140. ad.setMessage("Please input [Firstname] ");
  141. ad.show();
  142. firstname.requestFocus();
  143. return false;
  144. }
  145. // Check lastname
  146. if (lastname.getText().length() == 0) {
  147. ad.setMessage("Please input [Lastname] ");
  148. ad.show();
  149. lastname.requestFocus();
  150. return false;
  151. }
  152. // Check birthday
  153. if (birthday.getText().length() == 0) {
  154. ad.setMessage("Please input [Birthday] ");
  155. ad.show();
  156. birthday.requestFocus();
  157. return false;
  158. }
  159.  
  160. return true;
  161. }
  162.  
  163. public void RegisterButton2(View v) {
  164. Username = username.getText().toString();
  165. Password = password.getText().toString();
  166. Confirmpass = confirmpass.getText().toString();
  167. Firstname = firstname.getText().toString();
  168. Lastname = lastname.getText().toString();
  169. Birthday = year_x + "-" + month_x + "-" + day_x;
  170. BackGround b = new BackGround();
  171. b.execute(Username, Password, Confirmpass, Firstname, Lastname, Birthday);
  172. }
  173.  
  174.  
  175.  
  176. class BackGround extends AsyncTask<String, String, String> {
  177.  
  178. @Override
  179. protected String doInBackground(String... params) {
  180. String username = params[0];
  181. String password = params[1];
  182. String firstname = params[2];
  183. String lastname = params[3];
  184. String birthday = params[4];
  185. String data = "";
  186. int tmp;
  187.  
  188. try {
  189. URL url = new URL("http://oldiefinder.esy.es/Android/register.php");
  190. String urlParams = "username=" + username + "&password=" + password + "&firstname=" + firstname + "&lastname=" + lastname + "&birthday=" + birthday;
  191.  
  192. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  193. httpURLConnection.setDoOutput(true);
  194. OutputStream os = httpURLConnection.getOutputStream();
  195. os.write(urlParams.getBytes());
  196. os.flush();
  197. os.close();
  198. InputStream is = httpURLConnection.getInputStream();
  199. while ((tmp = is.read()) != -1) {
  200. data += (char) tmp;
  201. }
  202. is.close();
  203. httpURLConnection.disconnect();
  204.  
  205. return data;
  206.  
  207. } catch (MalformedURLException e) {
  208. e.printStackTrace();
  209. return "Exception: " + e.getMessage();
  210. } catch (IOException e) {
  211. e.printStackTrace();
  212. return "Exception: " + e.getMessage();
  213. }
  214. }
  215.  
  216. @Override
  217. protected void onPostExecute(String s) {
  218. if (s.equals("")) {
  219. s = "Data saved successfully.";
  220. }
  221. Toast.makeText(ctx, s, Toast.LENGTH_LONG).show();
  222. }
  223. }
  224.  
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement