Advertisement
Guest User

activity

a guest
Apr 28th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.55 KB | None | 0 0
  1. package com.**********;
  2.  
  3.  
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.net.ConnectivityManager;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.provider.MediaStore;
  12. import android.support.v4.app.Fragment;
  13. import android.support.v4.app.FragmentManager;
  14. import android.util.Base64;
  15. import android.util.Log;
  16. import android.view.LayoutInflater;
  17. import android.view.MotionEvent;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.widget.Button;
  21. import android.widget.EditText;
  22. import android.widget.Toast;
  23.  
  24. import com.android.volley.Request;
  25. import com.android.volley.RequestQueue;
  26. import com.android.volley.Response;
  27. import com.android.volley.VolleyError;
  28. import com.android.volley.toolbox.StringRequest;
  29. import com.novalnvall.memeunfaedah.Controller.AppController;
  30. import com.novalnvall.memeunfaedah.R;
  31.  
  32. import org.json.JSONException;
  33. import org.json.JSONObject;
  34.  
  35. import java.io.ByteArrayInputStream;
  36. import java.io.ByteArrayOutputStream;
  37. import java.io.IOException;
  38. import java.util.HashMap;
  39. import java.util.Map;
  40.  
  41. import de.hdodenhof.circleimageview.CircleImageView;
  42.  
  43. import static android.app.Activity.RESULT_OK;
  44.  
  45.  
  46. /**
  47. * A simple {@link Fragment} subclass.
  48. */
  49. public class Daftar extends Fragment {
  50. private View view;
  51. private FragmentManager fragmentManager;
  52. private RequestQueue requestQueue;
  53. private Button daftar, ikon;
  54. private EditText user, pas, nama, deskripsi, confirm;
  55. ProgressDialog progressDialog;
  56. Intent intent;
  57. private String TAG_KEY= "image";
  58. int success;
  59. ConnectivityManager connectivityManager;
  60. CircleImageView contoh;
  61.  
  62. private static final String TAG = Daftar.class.getSimpleName();
  63.  
  64. private String url = "http://192.*****/meme/registerandro.php";
  65. Bitmap bitmap, decoded;
  66. private static final String TAG_SUCCESS = "success";
  67. private static final String TAG_MESSAGE = "message";
  68.  
  69. int PICK_IMAGE_REQUEST=1;
  70. int bitmap_size = 60;
  71. String tag_json_obj= "json_obj_req";
  72.  
  73.  
  74. public void setFragmentManager(FragmentManager fragmentManager) {
  75. this.fragmentManager = fragmentManager;
  76. }
  77.  
  78. public static Daftar newInstance(FragmentManager fragmentManager) {
  79. Daftar fragment = new Daftar();
  80. fragment.setFragmentManager(fragmentManager);
  81. return fragment;
  82. }
  83.  
  84. @Override
  85. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  86. Bundle savedInstanceState) {
  87. // Inflate the layout for this fragment
  88. view = inflater.inflate(R.layout.fragment_daftar, container, false);
  89. connectivityManager = (ConnectivityManager)getActivity().getSystemService(getContext().CONNECTIVITY_SERVICE);
  90. {
  91. if(connectivityManager.getActiveNetworkInfo()!=null
  92. && connectivityManager.getActiveNetworkInfo().isAvailable()
  93. && connectivityManager.getActiveNetworkInfo().isConnected()){
  94. }else{
  95. Toast.makeText(getContext(), "Tak ada koneksi internet", Toast.LENGTH_SHORT).show();
  96. }
  97. }
  98.  
  99. contoh= view.findViewById(R.id.preview);
  100. daftar = view.findViewById(R.id.bdaftar);
  101. user = view.findViewById(R.id.username);
  102. pas = view.findViewById(R.id.password);
  103. ikon = view.findViewById(R.id.uploadprofil);
  104. nama = view.findViewById(R.id.nick);
  105. confirm = view.findViewById(R.id.confirm);
  106. deskripsi = view.findViewById(R.id.deskripsi_kamu);
  107.  
  108. View.OnTouchListener test = new View.OnTouchListener() {
  109. @Override
  110. public boolean onTouch(View view, MotionEvent motionEvent) {
  111. if ( motionEvent.getAction() == MotionEvent.ACTION_DOWN ) {
  112. ((Button) view).setTextColor(getResources().getColor(R.color.white));
  113. (view).setBackground(getResources().getDrawable(R.drawable.border_sec));
  114. } else {
  115. ((Button) view).setTextColor(getResources().getColor(R.color.colorAccent));
  116. (view).setBackground(getResources().getDrawable(R.drawable.edittextborder));
  117.  
  118. }
  119. return false;
  120. }
  121. };
  122. daftar.setOnTouchListener(test);
  123. ikon.setOnTouchListener(test);
  124.  
  125.  
  126. ikon.setOnClickListener(new View.OnClickListener() {
  127. @Override
  128. public void onClick(View view) {
  129. showFileChooser();
  130. }
  131. });
  132.  
  133. daftar.setOnClickListener(new View.OnClickListener() {
  134. @Override
  135. public void onClick(View view) {
  136. String username = user.getText().toString();
  137. String password = pas.getText().toString();
  138. String confirm_password = confirm.getText().toString();
  139. String nama_admin = nama.getText().toString();
  140. String info = deskripsi.getText().toString();
  141. String profil = ikon.getText().toString();
  142.  
  143. if(connectivityManager.getActiveNetworkInfo()!=null
  144. && connectivityManager.getActiveNetworkInfo().isAvailable()
  145. && connectivityManager.getActiveNetworkInfo().isConnected()){
  146. checkRegister(username, password, confirm_password, nama_admin, info, profil);
  147. }else{
  148. Toast.makeText(getContext(), "Tak ada internet", Toast.LENGTH_SHORT).show();
  149. }
  150. }
  151. });
  152. return view;
  153. }
  154.  
  155. public String getStringImage(Bitmap bmp){
  156. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  157. bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, baos);
  158. byte[] imageBytes = baos.toByteArray();
  159. String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
  160. return encodedImage;
  161. }
  162. private void showFileChooser(){
  163. Intent intent = new Intent();
  164. intent.setType("image/*");
  165. intent.setAction(Intent.ACTION_GET_CONTENT);
  166. startActivityForResult(Intent.createChooser(intent, "Select Picture"),PICK_IMAGE_REQUEST);
  167. }
  168.  
  169. @Override
  170. public void onActivityResult(int requestCode, int resultCode, Intent data){
  171. super.onActivityResult(requestCode, resultCode, data);
  172.  
  173. if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData()!=null){
  174. Uri filePath = data.getData();
  175. try{
  176. //ambil file dari gallery
  177. bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
  178. setToImageView(getResizedBitmap(bitmap, 512));
  179.  
  180.  
  181. }catch (IOException e){
  182. e.printStackTrace();
  183. }
  184. }
  185. }
  186.  
  187. private void setToImageView(Bitmap bmp){
  188. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  189. bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, bytes);
  190. decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bytes.toByteArray()));
  191. contoh.setImageBitmap(decoded);
  192. }
  193.  
  194. public Bitmap getResizedBitmap (Bitmap image, int maxSize){
  195. int width = image.getWidth();
  196. int height = image.getHeight();
  197.  
  198. float bitmapRatio = (float) width/ (float) height;
  199. if(bitmapRatio>1){
  200. width = maxSize;
  201. height = (int) (width/bitmapRatio);
  202. }else{
  203. height= maxSize;
  204. width = (int)(height*bitmapRatio);
  205. }
  206. return Bitmap.createScaledBitmap(image, width, height, true);
  207. }
  208.  
  209. private void checkRegister(final String username, final String password, final String confirm_password, final String nama_admin, final String info, final String profil){
  210. progressDialog = new ProgressDialog(getContext());
  211. progressDialog.setCancelable(true);
  212. progressDialog.setMessage("Mendaftar...");
  213. showDialog();
  214.  
  215. StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
  216. @Override
  217. public void onResponse(String response) {
  218. Log.e(TAG, "Register Response: " + response.toString());
  219. hideDialog();
  220. try {
  221. JSONObject jsonObject = new JSONObject(response);
  222. success = jsonObject.getInt(TAG_SUCCESS);
  223.  
  224. if (success == 1) {
  225. Log.e("Successfully Register!", jsonObject.toString());
  226. Toast.makeText(getContext(), jsonObject.getString(TAG_MESSAGE), Toast.LENGTH_SHORT).show();
  227. kosong();
  228. } else {
  229. Toast.makeText(getContext(), jsonObject.getString(TAG_MESSAGE), Toast.LENGTH_SHORT).show();
  230. }
  231. } catch (JSONException e) {
  232. e.printStackTrace();
  233. }
  234. }
  235. }, new Response.ErrorListener() {
  236. @Override
  237. public void onErrorResponse(VolleyError error) {
  238. Log.e(TAG, "Login Error: "+error.getMessage());
  239. Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
  240. }
  241. }){
  242. @Override
  243. protected Map<String, String> getParams(){
  244. //kirim parameter ke url login
  245. Map<String, String> params = new HashMap<String, String>();
  246. params.put("username", username);
  247. params.put("password", password);
  248. params.put("confirm_password", confirm_password);
  249. params.put("nama_admin", nama_admin);
  250. params.put("info",info);
  251. params.put("profil", profil);
  252. params.put(TAG_KEY, getStringImage(decoded));
  253.  
  254. return params;
  255. }
  256. };
  257. AppController.getInstance().addToRequestQueue(stringRequest, tag_json_obj);
  258. }
  259. private void showDialog(){
  260. if(!progressDialog.isShowing())
  261. progressDialog.show();
  262. }
  263. private void hideDialog(){
  264. if(progressDialog.isShowing())
  265. progressDialog.dismiss();
  266. }
  267.  
  268. private void kosong(){
  269. contoh.setImageResource(0);
  270. nama.setText(null);
  271. user.setText(null);
  272. pas.setText(null);
  273. confirm.setText(null);
  274. deskripsi.setText(null);
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement