Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.14 KB | None | 0 0
  1. package com.myanmarmusicstore.mms;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.Dialog;
  6. import android.content.Intent;
  7. import android.graphics.Color;
  8. import android.graphics.Rect;
  9. import android.os.Bundle;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.view.ViewTreeObserver.OnGlobalLayoutListener;
  16. import android.view.Window;
  17. import android.widget.AdapterView;
  18. import android.widget.AdapterView.OnItemSelectedListener;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.LinearLayout;
  22. import android.widget.RadioButton;
  23. import android.widget.RadioGroup;
  24. import android.widget.Spinner;
  25. import android.widget.TextView;
  26. import android.widget.Toast;
  27.  
  28. import com.myanmarmusicstore.mms.adapter.SpinnerListAdapter;
  29. import com.myanmarmusicstore.mms.asyn.RegisterAsyn;
  30. import com.myanmarmusicstore.mms.base.BaseActivity;
  31. import com.myanmarmusicstore.mms.db.CountryListDao;
  32. import com.myanmarmusicstore.mms.db.DatabaseManager;
  33. import com.myanmarmusicstore.mms.obj.ObjCountryList;
  34. import com.myanmarmusicstore.mms.util.Constant;
  35. import com.myanmarmusicstore.mms.util.NetworkListener;
  36. import com.myanmarmusicstore.mms.util.Util;
  37.  
  38. public class RegisterActivity extends BaseActivity {
  39.  
  40. private TextView tvRegisterTitle;
  41. private EditText etUsername, etPassword, etEmail, etPhone;
  42. private Button btnRegister;
  43. private LinearLayout backgroundView ;
  44. private Spinner spinnerCountry;
  45. private String shortCountryName;
  46. private TextView isvalidMessage_tv;
  47.  
  48. private ArrayList<ObjCountryList> list;
  49.  
  50. private RadioGroup radioGroup;
  51. private RadioButton radioMale, radioFemale;
  52.  
  53. private String strGender;
  54.  
  55. @Override
  56. public void onCreate(Bundle savedInstanceState) {
  57. // TODO Auto-generated method stub
  58. super.onCreate(savedInstanceState);
  59. setContentView(R.layout.activity_register);
  60.  
  61. tvRegisterTitle = (TextView) findViewById(R.id.tvRegisterTitle);
  62. etUsername = (EditText) findViewById(R.id.etRegisterUsername);
  63. etPassword = (EditText) findViewById(R.id.etRegisterPassword);
  64. etEmail = (EditText) findViewById(R.id.etRegisterEmail);
  65. etPhone = (EditText)findViewById(R.id.etRegisterPhone);
  66.  
  67. btnRegister = (Button) findViewById(R.id.btnRegisterRegister);
  68. backgroundView = (LinearLayout)findViewById(R.id.register_background_image);
  69. spinnerCountry = (Spinner)findViewById(R.id.spinnerCountryList);
  70. isvalidMessage_tv = (TextView)findViewById(R.id.message_tv);
  71.  
  72. radioGroup = (RadioGroup) findViewById(R.id.registerRadioGroup);
  73. radioMale = (RadioButton) findViewById(R.id.male);
  74. radioFemale = (RadioButton) findViewById(R.id.female);
  75.  
  76. etEmail.addTextChangedListener(filterTextWatcher );
  77.  
  78. if (pref.getBoolean(Constant.isFontEmbeded, false)) {
  79. tvRegisterTitle.setTypeface(Util.userChooseFont(ctx));
  80. etUsername.setTypeface(Util.userChooseFont(ctx));
  81. etPassword.setTypeface(Util.userChooseFont(ctx));
  82. etEmail.setTypeface(Util.userChooseFont(ctx));
  83.  
  84. btnRegister.setTypeface(Util.userChooseFont(ctx));
  85.  
  86. } else {
  87. tvRegisterTitle.setTypeface(null);
  88. etUsername.setTypeface(null);
  89. etPassword.setTypeface(null);
  90. etEmail.setTypeface(null);
  91.  
  92.  
  93. btnRegister.setTypeface(null);
  94. }
  95.  
  96. btnRegister.setOnClickListener(new OnClickListener() {
  97.  
  98. @Override
  99. public void onClick(View arg0) {
  100. // TODO Auto-generated method stub
  101.  
  102. int selectedId = radioGroup.getCheckedRadioButtonId();
  103. if(selectedId == radioMale.getId()) {
  104. strGender = "Male";
  105. } else if(selectedId == radioFemale.getId()) {
  106. strGender = "Female";
  107. }
  108. Log.i("kglay","GENDER >>> " + strGender);
  109.  
  110.  
  111.  
  112. if(checkForm()) {
  113. if (NetworkListener.isOnline(ctx)) {
  114. new RegisterAsyn(RegisterActivity.this, ctx, etUsername
  115. .getText().toString(), etPassword.getText()
  116. .toString(), etEmail.getText().toString(),
  117. shortCountryName, etPhone.getText().toString(),strGender).execute();
  118. } else {
  119. Toast.makeText(ctx, "You are offline.", Toast.LENGTH_SHORT)
  120. .show();
  121. }
  122. }
  123.  
  124. }
  125. });
  126.  
  127.  
  128. detectKeyboard();
  129. setSpinnerAdapter();
  130. }
  131.  
  132. private boolean checkForm() {
  133.  
  134. boolean isCheckOK = false;
  135. if(etUsername.getText().toString().equalsIgnoreCase("")) {
  136. Toast.makeText(ctx, "Username can't be blank", Toast.LENGTH_SHORT).show();
  137. isCheckOK = false;
  138. }
  139. else if(etPassword.getText().toString().equalsIgnoreCase("")) {
  140. Toast.makeText(ctx, "Password can't be blank", Toast.LENGTH_SHORT).show();
  141. isCheckOK = false;
  142. }
  143. else if(etEmail.getText().toString().equalsIgnoreCase("")) {
  144. Toast.makeText(ctx, "Email can't be blank", Toast.LENGTH_SHORT).show();
  145. isCheckOK = false;
  146. }
  147. else if(etPhone.getText().toString().equalsIgnoreCase("")) {
  148. Toast.makeText(ctx, "PhoneNumber can't be blank", Toast.LENGTH_SHORT).show();
  149. isCheckOK = false;
  150. }
  151. else {
  152. isCheckOK = true;
  153. }
  154. return isCheckOK ;
  155. }
  156.  
  157. private void setSpinnerAdapter(){
  158. list = new ArrayList<ObjCountryList>();
  159. CountryListDao dao = new CountryListDao(new DatabaseManager(ctx));
  160. list = dao.getAll();
  161.  
  162. SpinnerListAdapter dataAdapter = new SpinnerListAdapter(ctx, list);
  163. // ArrayAdapter<ObjCountryList> dataAdapter = new ArrayAdapter<ObjCountryList>(this,android.R.layout.simple_spinner_item, list);
  164. // dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  165. spinnerCountry.setAdapter(dataAdapter);
  166.  
  167. spinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
  168.  
  169. public void onItemSelected(AdapterView<?> parent, View arg1,
  170. int arg2, long arg3) {
  171. // TODO Auto-generated method stub
  172. // ((TextView)parent.getChildAt(0)).setTextSize(5);
  173. Log.i("Spinner Choose", list.get(arg2).getCountryNameFull()
  174. + " = " + list.get(arg2).getCountryNameShort());
  175.  
  176. shortCountryName = list.get(arg2).getCountryNameShort();
  177. }
  178. public void onNothingSelected(AdapterView<?> arg0) {
  179. // TODO Auto-generated method stub
  180. }
  181. });
  182. }
  183.  
  184.  
  185. public void registerState(boolean state , String message) {
  186. if (state) {
  187. registerSuccess(message);
  188. } else if (!state) {
  189. Util.showAlertDialog(message, RegisterActivity.this);
  190. }
  191. }
  192.  
  193. private void registerSuccess(String message) {
  194. final Dialog dialog = new Dialog(RegisterActivity.this);
  195. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  196. dialog.setContentView(R.layout.kglay_alert);
  197.  
  198. TextView tvMessage = (TextView) dialog.findViewById(R.id.tvAlertMessage);
  199. tvMessage.setText(message);
  200.  
  201. Button btnOK = (Button) dialog.findViewById(R.id.btnAlertOK);
  202. btnOK.setText("Login");
  203.  
  204. btnOK.setOnClickListener(new OnClickListener() {
  205. @Override
  206. public void onClick(View v) {
  207. dialog.dismiss();
  208. // Toast.makeText(ctx, text, Toast.LENGTH_LONG)
  209. Intent it = new Intent(getApplicationContext(),
  210. LoginActivity.class);
  211. it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  212. startActivity(it);
  213. }
  214. });
  215.  
  216. dialog.show();
  217. }
  218.  
  219. // public void showErrorAlertDialog(String Message) {
  220. // final Dialog dialog = new Dialog(RegisterActivity.this);
  221. // dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  222. // dialog.setContentView(R.layout.kglay_alert);
  223. //
  224. // TextView message = (TextView) dialog.findViewById(R.id.tvAlertMessage);
  225. // message.setText("Fail !.");
  226. //
  227. // Button btnOK = (Button) dialog.findViewById(R.id.btnAlertOK);
  228. //
  229. // btnOK.setOnClickListener(new OnClickListener() {
  230. // @Override
  231. // public void onClick(View v) {
  232. // dialog.dismiss();
  233. // // Toast.makeText(ctx, text, Toast.LENGTH_LONG)
  234. // }
  235. // });
  236. //
  237. // dialog.show();
  238. // }
  239.  
  240. private void detectKeyboard() {
  241. final View activityRootView = findViewById(R.id.registerActivityLayout);
  242. activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  243. @Override
  244. public void onGlobalLayout() {
  245. Rect r = new Rect();
  246. //r will be populated with the coordinates of your view that area still visible.
  247. activityRootView.getWindowVisibleDisplayFrame(r);
  248.  
  249. int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
  250. if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
  251. //... do something here
  252. Log.i("Kglay","Keyboard showing ...");
  253. backgroundView.setVisibility(View.GONE);
  254. }
  255. else {
  256. backgroundView.setVisibility(View.VISIBLE);
  257. }
  258. }
  259. });
  260. }
  261.  
  262. private TextWatcher filterTextWatcher = new TextWatcher() {
  263.  
  264. public void afterTextChanged(Editable s) {
  265. isvalidMessage_tv.setVisibility(View.VISIBLE);
  266. if(Util.isEmailValid(s.toString())){
  267.  
  268. isvalidMessage_tv.setTextColor(Color.GREEN);
  269. isvalidMessage_tv.setText("Valid email address");
  270. }else{
  271. isvalidMessage_tv.setTextColor(Color.RED);
  272. isvalidMessage_tv.setText("Invalid email address");
  273. }
  274. }
  275.  
  276. public void beforeTextChanged(CharSequence s, int start, int count,
  277. int after) {
  278. }
  279.  
  280. public void onTextChanged(CharSequence s, int start, int before,
  281. int count) {
  282. }
  283.  
  284. };
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement