Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. public class CycleColors extends AppCompatActivity {
  2.  
  3. private static final String TAG = CycleColors.class.getName();
  4.  
  5. static List<String> colorsList = new ArrayList<>();
  6.  
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10.  
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_cycle_colors);
  13.  
  14. Button doneBtn = (Button)findViewById(R.id.btnDone);
  15.  
  16. doneBtn.setOnClickListener(new View.OnClickListener() {
  17. public void onClick(View v) {
  18. clickDoneButton();
  19. }
  20. });
  21.  
  22. getData();
  23. Intent intent = getIntent();
  24. new cycle(this).execute();
  25.  
  26. }
  27.  
  28. public void clickDoneButton()
  29. {
  30. finish();
  31. }
  32.  
  33. class cycle extends AsyncTask<Void, Void, Void>
  34. {
  35. private Activity activity;
  36. View view;
  37.  
  38. public cycle(Activity a)
  39. {
  40. activity = a;
  41. view = activity.getWindow().getDecorView();
  42. }
  43.  
  44. protected Void doInBackground(Void... param)
  45. {
  46. activity.runOnUiThread(new Runnable()
  47. {
  48. @Override
  49. public void run()
  50. {
  51. while(true)
  52. {
  53. for (String c: colorsList)
  54. {
  55. int color = Color.parseColor(c);
  56. Log.d(TAG, color+"");
  57. view.setBackgroundColor(color);
  58. SystemClock.sleep(500);
  59. }
  60. }
  61. }
  62. });
  63. Log.d(TAG, "returned null");
  64. return null;
  65. }
  66. }
  67.  
  68.  
  69.  
  70. private void getData() {
  71.  
  72. final String serverURL;
  73. final JsonArrayRequest request;
  74. final RequestQueue queue;
  75.  
  76. serverURL = "https://api.mlab.com/api/1/databases/comp3717final/collections/colours?apiKey=qR2ag5UaRrHBxDm6KEyg95EESmfY5Bcf";
  77. queue = Volley.newRequestQueue(this);
  78. request = new JsonArrayRequest(serverURL,
  79. new onJSONResponse(),
  80. new onJSONError());
  81. queue.add(request);
  82. }
  83.  
  84. private class onJSONResponse implements Response.Listener<JSONArray>
  85. {
  86. @Override
  87. public void onResponse(JSONArray response)
  88. {
  89. final int length;
  90. int i;
  91. i = 0;
  92. length = response.length();
  93.  
  94. try {
  95. for (; i < length; i++) {
  96. final JSONObject colorObject;
  97. final JSONObject hexObject;
  98. final String colorName;
  99. final String hexCode;
  100.  
  101. colorObject = response.getJSONObject(i);
  102. colorName = colorObject.getString("color");
  103. hexCode = colorObject.getString("value");
  104. Log.d(TAG, colorName + " => " + hexCode);
  105. colorsList.add(colorName);
  106. }
  107. } catch (final JSONException ex) {
  108. Log.d(TAG, "Error getting json object: " + i, ex);
  109. colorsList.clear();
  110. }
  111. Log.d(TAG, "working");
  112. }
  113. }
  114.  
  115. private static class onJSONError implements Response.ErrorListener
  116. {
  117. @Override
  118. public void onErrorResponse(VolleyError error)
  119. {
  120. Log.d(TAG, "JSON ERROR");
  121. }
  122. }
  123. }
  124.  
  125. <?xml version="1.0" encoding="utf-8"?>
  126. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  127. xmlns:tools="http://schemas.android.com/tools"
  128. android:layout_width="match_parent"
  129. android:layout_height="match_parent"
  130. android:paddingBottom="@dimen/activity_vertical_margin"
  131. android:paddingLeft="@dimen/activity_horizontal_margin"
  132. android:paddingRight="@dimen/activity_horizontal_margin"
  133. android:paddingTop="@dimen/activity_vertical_margin"
  134. tools:context="com.example.peymantp.androidfinal.CycleActivity">
  135.  
  136. <Button
  137. android:layout_width="wrap_content"
  138. android:layout_height="wrap_content"
  139. android:text="Done"
  140. android:id="@+id/button6"
  141. android:layout_alignParentBottom="true"
  142. android:layout_alignParentRight="true"
  143. android:layout_alignParentEnd="true"
  144. android:layout_marginBottom="32dp" />
  145. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement