Advertisement
Guest User

Untitled

a guest
Aug 28th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.00 KB | None | 0 0
  1. //pada Graddle, silakan ditambahkan barus perintah ini:
  2. compile 'com.jakewharton:butterknife:8.8.1'
  3. annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
  4.  
  5. //Daftar.java
  6.  
  7. package id.co.imastudio.ojekonlinecourse.View;
  8.  
  9. import android.content.Intent;
  10. import android.os.Bundle;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17.  
  18. import id.co.imastudio.ojekonlinecourse.Helper.HeroHelper;
  19. import id.co.imastudio.ojekonlinecourse.R;
  20. import id.co.imastudio.ojekonlinecourse.Response.ResponseDaftar;
  21. import retrofit2.Call;
  22. import retrofit2.Callback;
  23. import retrofit2.Response;
  24.  
  25. public class Daftar extends AppCompatActivity {
  26.  
  27. EditText daftarusername;
  28. EditText daftaremail;
  29. EditText daftarhp;
  30. EditText daftarpassword;
  31.  
  32. EditText daftarconfirmasipass;
  33.  
  34.  
  35. Button btnSignUp;
  36.  
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_daftar);
  42. //ButterKnife.bind(this);
  43. daftarusername = (EditText) findViewById(R.id.daftarusername);
  44. daftaremail = (EditText) findViewById(R.id.daftaremail);
  45. daftarhp = (EditText) findViewById(R.id.daftarhp);
  46. daftarpassword = (EditText) findViewById(R.id.daftarpassword);
  47. daftarconfirmasipass = (EditText) findViewById(R.id.daftarconfirmasipass);
  48. btnSignUp = (Button) findViewById(R.id.btnSignUp);
  49. btnSignUp.setOnClickListener(new View.OnClickListener() {
  50. @Override
  51. public void onClick(View view) {
  52. /*membuat sebuah kondisional untuk memastikan kalau pengguna sudah mengisi sebuah textfield yang ada */
  53. Log.d("Button diclick","register");
  54. //get semua inputan user
  55.  
  56. String tampungnama = daftarusername.getText().toString();
  57. String tampungemail = daftaremail.getText().toString();
  58. String tampungphone = daftarhp.getText().toString();
  59. String tampungpassword = daftarpassword.getText().toString();
  60. String tampungconfirm = daftarconfirmasipass.getText().toString();
  61.  
  62. if(!tampungpassword.equals(tampungconfirm)) {
  63. Toast.makeText(Daftar.this, "Password tidak cocok", Toast.LENGTH_SHORT).show();
  64. }
  65.  
  66. // ! = negasi
  67. else if (!tampungnama.isEmpty() && !tampungemail.isEmpty() && !tampungpassword.isEmpty() && !tampungphone.isEmpty() && !tampungconfirm.isEmpty()){
  68. Log.d("Button diclick","data valid");
  69. //get init retrofit yang sudh dibikin class initlibrary
  70. ApiService api = InitLibrary.getInstance();
  71.  
  72. //get request
  73. Call<ResponseDaftar> call = api.request_daftar(tampungnama,tampungemail,tampungpassword,tampungphone);
  74.  
  75. //get response
  76. call.enqueue(new Callback<ResponseDaftar>() {
  77. @Override
  78. public void onResponse(Call<ResponseDaftar> call, Response<ResponseDaftar> response) {
  79. Log.d("Button diclick","ada response " + response.toString());
  80. //response success
  81. if(response.isSuccessful()) {
  82. String result = response.body().getResult();
  83. String pesan = response.body().getMsg();
  84. if (result.equals("true")) {
  85. //pindah halaman ke halaman lain
  86. Intent intent = new Intent(Daftar.this, login.class);
  87. startActivity(intent);
  88. } else {
  89.  
  90. //bikin toast kalau seandainya hasilnya gak true
  91. HeroHelper.pesan(Daftar.this, pesan);
  92. }
  93. }
  94. }
  95.  
  96. @Override
  97. public void onFailure(Call<ResponseDaftar> call, Throwable t) {
  98.  
  99. }
  100. });
  101.  
  102. } else {
  103. Toast.makeText(Daftar.this, "Maaf, harus diisi semua", Toast.LENGTH_SHORT).show();
  104. }
  105. }
  106. });
  107. }
  108. }
  109.  
  110. //ApiService.java
  111. package id.co.imastudio.ojekonlinecourse.View;
  112.  
  113. import id.co.imastudio.ojekonlinecourse.Response.ResponseDaftar;
  114. import retrofit2.Call;
  115. import retrofit2.http.Field;
  116. import retrofit2.http.FormUrlEncoded;
  117. import retrofit2.http.POST;
  118.  
  119. public interface ApiService {
  120. @FormUrlEncoded
  121. @POST("daftar")
  122. Call<ResponseDaftar> request_daftar(
  123. @Field("nama") String name,
  124. @Field("email") String email,
  125. @Field("password") String pass,
  126. @Field("phone") String phone
  127. );
  128.  
  129. }
  130.  
  131.  
  132. //ResponseDaftar.java
  133.  
  134. package id.co.imastudio.ojekonlinecourse.Response;
  135.  
  136. import com.google.gson.annotations.SerializedName;
  137.  
  138.  
  139. public class ResponseDaftar {
  140.  
  141. @SerializedName("msg")
  142. private String mMsg;
  143. @SerializedName("result")
  144. private String mResult;
  145.  
  146. public String getMsg() {
  147. return mMsg;
  148. }
  149.  
  150. public void setMsg(String msg) {
  151. mMsg = msg;
  152. }
  153.  
  154. public String getResult() {
  155. return mResult;
  156. }
  157.  
  158. public void setResult(String result) {
  159. mResult = result;
  160. }
  161.  
  162. }
  163.  
  164. //InitLibrary.java
  165. package id.co.imastudio.ojekonlinecourse.View;
  166.  
  167. import retrofit2.Retrofit;
  168. import retrofit2.converter.gson.GsonConverterFactory;
  169.  
  170. /**
  171. * Created by macbookpro on 8/27/17.
  172. */
  173.  
  174.  
  175. public class InitLibrary {
  176.  
  177. public static Retrofit setInit() {
  178. return new Retrofit.Builder().baseUrl("http://192.168.20.14/ojeg_server/api/")
  179. .addConverterFactory(GsonConverterFactory.create())
  180. .build();
  181. }
  182.  
  183. public static ApiService getInstance() {
  184. return setInit().create(ApiService.class);
  185. }
  186. }
  187.  
  188.  
  189. //login.java
  190. package id.co.imastudio.ojekonlinecourse.View;
  191.  
  192. import android.content.Intent;
  193. import android.os.Bundle;
  194. import android.support.v7.app.AppCompatActivity;
  195. import android.view.View;
  196. import android.widget.Button;
  197. import android.widget.EditText;
  198. import android.widget.TextView;
  199.  
  200. import butterknife.ButterKnife;
  201. import butterknife.OnClick;
  202. import id.co.imastudio.ojekonlinecourse.R;
  203.  
  204. public class login extends AppCompatActivity {
  205.  
  206. // @BindView(R.id.loginemail)
  207. EditText loginemail;
  208. // @BindView(R.id.loginpassword)
  209. EditText loginpassword;
  210. // @BindView(R.id.signin)
  211. Button signin;
  212. //// @BindView(R.id.textlink)
  213. TextView textlink;
  214.  
  215. @Override
  216. protected void onCreate(Bundle savedInstanceState) {
  217. super.onCreate(savedInstanceState);
  218. setContentView(R.layout.activity_login);
  219. ButterKnife.bind(this);
  220.  
  221. loginemail = (EditText) findViewById(R.id.loginemail);
  222. loginpassword = (EditText) findViewById(R.id.loginpassword);
  223. textlink= (TextView) findViewById(R.id.textlink);
  224.  
  225.  
  226. textlink.setOnClickListener(new View.OnClickListener() {
  227. @Override
  228. public void onClick(View view) {
  229. Intent intent = new Intent(login.this, Daftar.class);
  230. startActivity(intent);
  231. }
  232. });
  233. }
  234.  
  235. @OnClick({R.id.signin, R.id.textlink})
  236. public void onViewClicked(View view) {
  237. switch (view.getId()) {
  238. case R.id.signin:
  239. break;
  240. case R.id.textlink:
  241.  
  242.  
  243.  
  244. break;
  245. }
  246. }
  247. }
  248.  
  249. //activity_daftar.xml
  250. <?xml version="1.0" encoding="utf-8"?>
  251. <LinearLayout
  252. xmlns:android="http://schemas.android.com/apk/res/android"
  253. xmlns:app="http://schemas.android.com/apk/res-auto"
  254. xmlns:tools="http://schemas.android.com/tools"
  255. android:layout_width="match_parent"
  256. android:layout_height="match_parent"
  257. android:background="@color/colorPrimary"
  258. android:orientation="vertical"
  259. android:padding="40dp"
  260. tools:context="id.co.imastudio.ojekonlinecourse.View.Daftar">
  261.  
  262.  
  263. <EditText
  264. android:id="@+id/daftarusername"
  265. android:layout_width="match_parent"
  266. android:layout_height="35dp"
  267. android:layout_marginTop="5dp"
  268. android:fontFamily="sans-serif-condensed"
  269. android:background="@drawable/backgroundedittext"
  270. android:gravity="center"
  271. android:hint="Name"
  272. android:textColor="@android:color/black"
  273. android:textColorHint="@android:color/black"/>
  274.  
  275. <EditText
  276. android:id="@+id/daftaremail"
  277. android:layout_width="match_parent"
  278. android:layout_height="35dp"
  279. android:layout_marginTop="5dp"
  280. android:fontFamily="sans-serif-condensed"
  281. android:background="@drawable/backgroundedittext"
  282. android:gravity="center"
  283. android:textColor="@android:color/black"
  284. android:textColorHint="@android:color/black"
  285. android:hint="Email"/>
  286.  
  287. <EditText
  288. android:id="@+id/daftarhp"
  289. android:layout_width="match_parent"
  290. android:layout_height="35dp"
  291. android:layout_marginTop="5dp"
  292. android:fontFamily="sans-serif-condensed"
  293. android:background="@drawable/backgroundedittext"
  294. android:gravity="center"
  295. android:inputType="number"
  296. android:textColor="@android:color/black"
  297. android:textColorHint="@android:color/black"
  298. android:hint="Handphone"/>
  299.  
  300. <EditText
  301. android:id="@+id/daftarpassword"
  302. android:layout_width="match_parent"
  303. android:layout_height="35dp"
  304. android:inputType="textPassword"
  305. android:layout_marginTop="5dp"
  306. android:fontFamily="sans-serif-condensed"
  307. android:background="@drawable/backgroundedittext"
  308. android:gravity="center"
  309. android:textColor="@android:color/black"
  310. android:textColorHint="@android:color/black"
  311. android:hint="Password"/>
  312.  
  313. <EditText
  314. android:id="@+id/daftarconfirmasipass"
  315. android:layout_width="match_parent"
  316. android:layout_height="35dp"
  317. android:layout_marginTop="5dp"
  318. android:background="@drawable/backgroundedittext"
  319. android:gravity="center"
  320. android:fontFamily="sans-serif-condensed"
  321. android:textColorHint="@android:color/black"
  322. android:textColor="@android:color/black"
  323. android:hint="Confirm Password"/>
  324. <Button
  325. android:layout_width="match_parent"
  326. android:layout_height="35dp"
  327. android:text="Sign Up"
  328. android:textAllCaps="false"
  329. android:layout_marginTop="20dp"
  330. android:textColor="@android:color/white"
  331. android:id="@+id/btnSignUp"
  332. android:background="@drawable/groundbutton"/>
  333. </LinearLayout>
  334.  
  335. //activity_login.xml
  336. <?xml version="1.0" encoding="utf-8"?>
  337. <LinearLayout
  338. xmlns:android="http://schemas.android.com/apk/res/android"
  339. xmlns:app="http://schemas.android.com/apk/res-auto"
  340. xmlns:tools="http://schemas.android.com/tools"
  341. android:layout_width="match_parent"
  342. android:layout_height="match_parent"
  343. android:background="@color/colorPrimary"
  344. android:orientation="vertical"
  345. android:padding="40dp"
  346. tools:context="id.co.imastudio.ojekonlinecourse.View.Daftar">
  347.  
  348.  
  349. <EditText
  350. android:id="@+id/loginemail"
  351. android:layout_width="match_parent"
  352. android:layout_height="35dp"
  353. android:layout_marginTop="40dp"
  354. android:background="@drawable/backgroundedittext"
  355. android:fontFamily="sans-serif-condensed"
  356. android:gravity="center"
  357. android:textColorHint="@android:color/black"
  358. android:hint="Email"/>
  359.  
  360.  
  361. <EditText
  362. android:id="@+id/loginpassword"
  363. android:layout_width="match_parent"
  364. android:layout_height="35dp"
  365. android:layout_marginTop="5dp"
  366. android:background="@drawable/backgroundedittext"
  367. android:fontFamily="sans-serif-condensed"
  368. android:gravity="center"
  369. android:hint="Password"
  370. android:textColorHint="@android:color/black"
  371. android:inputType="textPassword"/>
  372.  
  373.  
  374. <Button
  375. android:layout_width="match_parent"
  376. android:layout_height="35dp"
  377. android:layout_marginTop="20dp"
  378. android:background="@drawable/groundbutton"
  379. android:text="Sign in"
  380. android:id="@+id/signin"
  381. android:textAllCaps="false"
  382. android:textColor="@android:color/white"/>
  383.  
  384. <TextView
  385. android:layout_width="match_parent"
  386. android:layout_height="wrap_content"
  387. android:id="@+id/textlink"
  388. android:text="Have account ? Sign Up"
  389. android:layout_marginTop="10dp"
  390. android:textColor="#fff"
  391. android:gravity="right"/>
  392. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement