mhnds

Untitled

Oct 23rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. package com.example.innovare.scrollhorizontal;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Canvas;
  9. import android.graphics.Color;
  10. import android.graphics.Paint;
  11. import android.graphics.PorterDuff;
  12. import android.graphics.PorterDuffXfermode;
  13. import android.graphics.Rect;
  14. import android.graphics.RectF;
  15. import android.os.AsyncTask;
  16. import android.os.Build;
  17. import android.os.Bundle;
  18. import android.provider.Contacts;
  19. import android.support.design.widget.FloatingActionButton;
  20. import android.support.design.widget.Snackbar;
  21. import android.support.v7.app.AppCompatActivity;
  22. import android.support.v7.widget.Toolbar;
  23. import android.util.Log;
  24. import android.view.LayoutInflater;
  25. import android.view.View;
  26. import android.view.Menu;
  27. import android.view.MenuItem;
  28. import android.view.ViewGroup;
  29. import android.view.animation.Animation;
  30. import android.view.animation.AnimationUtils;
  31. import android.view.animation.TranslateAnimation;
  32. import android.widget.Button;
  33. import android.widget.Gallery;
  34. import android.widget.HorizontalScrollView;
  35. import android.widget.ImageView;
  36. import android.widget.LinearLayout;
  37. import android.widget.ListAdapter;
  38. import android.widget.RelativeLayout;
  39. import android.widget.SimpleAdapter;
  40. import android.widget.TextView;
  41. import android.widget.Toast;
  42.  
  43. import org.json.JSONArray;
  44. import org.json.JSONException;
  45. import org.json.JSONObject;
  46. import org.w3c.dom.Text;
  47.  
  48. import java.io.IOException;
  49. import java.io.InputStream;
  50. import java.util.ArrayList;
  51. import java.util.HashMap;
  52. import java.util.logging.Handler;
  53.  
  54. public class MainActivity extends AppCompatActivity implements ScrollViewListener{
  55.  
  56.  
  57. ObservableScrollView soriscroll;
  58. JSONObject jsonobject;
  59. JSONArray jsonarray;
  60. TextView text;
  61. LinearLayout linear_view;
  62. TextView scroll_point;
  63. LinearLayout linear3;
  64.  
  65.  
  66.  
  67. //private static String url = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";
  68. //private static String url = "http://www.mocky.io/v2/59a94ceb100000200c3e0a78";
  69. //private static String url = "https://pastebin.com/raw/bPzqq2Kd";
  70. //private static String url = "https://pastebin.com/raw/e4ZZmwuC";
  71. private static String url = "https://pastebin.com/raw/MWTj6rzZ";
  72.  
  73. boolean count=true;
  74.  
  75. RelativeLayout relative = null;
  76.  
  77. @Override
  78. protected void onCreate(Bundle savedInstanceState) {
  79. super.onCreate(savedInstanceState);
  80. setContentView(R.layout.activity_main);
  81. new downloadData().execute();
  82. relative = (RelativeLayout) findViewById(R.id.relative);
  83. linear_view = (LinearLayout) findViewById(R.id.linear_view);
  84. }
  85.  
  86. @Override
  87. public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
  88. int a = linear_view.getWidth();
  89. int b = soriscroll.getWidth();
  90. if ((a - b) == x) {
  91. new downloadData().execute();
  92. }
  93. scroll_point.setText("linear_view" + a + "\nscroll_view" + b + "\n" + x + " : " + y);
  94.  
  95. }
  96.  
  97. private class downloadData extends AsyncTask<Void,Void,Void>{
  98. ProgressDialog pDialog = null;
  99. String text1="";
  100. ArrayList<HashMap<String,String>> arrayList = new ArrayList<>();
  101. @Override
  102. protected void onPreExecute() {
  103. super.onPreExecute();
  104. // Showing progress dialog
  105. if(count) {
  106. pDialog = new ProgressDialog(MainActivity.this);
  107. pDialog.setMessage("Please wait...");
  108. pDialog.setCancelable(false);
  109. pDialog.show();
  110. }
  111.  
  112. }
  113. @Override
  114. protected Void doInBackground(Void... params) {
  115. ServiceHandler sh = new ServiceHandler();
  116.  
  117. String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
  118.  
  119. Log.d("Response: ", "> " + jsonStr);
  120.  
  121. if (jsonStr != null) {
  122. try {
  123. JSONObject jsonObj = new JSONObject(jsonStr);
  124.  
  125. jsonarray = jsonObj.getJSONArray("worldpopulation");
  126. for (int i = 0; i < jsonarray.length(); i++) {
  127. HashMap<String, String> map = new HashMap<String, String>();
  128. jsonobject = jsonarray.getJSONObject(i);
  129. String author = jsonobject.getString("author");
  130. String photo = jsonobject.getString("photo");
  131. addToLinearLayout("",author,"",photo);
  132. }
  133.  
  134. } catch (JSONException e) {
  135. e.printStackTrace();
  136. }
  137. } else {
  138. Log.e("ServiceHandler", "Couldn't get any data from the url");
  139. }
  140.  
  141. return null;
  142. }
  143. @Override
  144. protected void onPostExecute(Void result) {
  145. super.onPostExecute(result);
  146. // Dismiss the progress dialog
  147. if(count) {
  148. if (pDialog.isShowing())
  149. pDialog.dismiss();
  150. count= false;
  151. }
  152. }
  153. }
  154.  
  155. private void addToLinearLayout(String text1, final String text2,String text3,String text4) {
  156.  
  157.  
  158. LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
  159. final View v = inflater.inflate(R.layout.scroll_element, linear_view, false);
  160.  
  161. TextView tv = (TextView) v.findViewById(R.id.rank);
  162. final TextView tv2 = (TextView) v.findViewById(R.id.name);
  163. TextView tv3 = (TextView) v.findViewById(R.id.population);
  164. ImageView iv = (ImageView) v.findViewById(R.id.imagelink);
  165. InputStream input = null;
  166. Bitmap bitmap = null;
  167. try {
  168. input = new java.net.URL(text4).openStream();
  169. bitmap = BitmapFactory.decodeStream(input);
  170. } catch (IOException e) {
  171. e.printStackTrace();
  172. }
  173. // Decode Bitmap
  174.  
  175. tv.setText(text1);
  176. tv2.setText(text2);
  177. tv3.setText(text3);
  178. iv.setImageBitmap(bitmap);
  179.  
  180. v.setOnClickListener(new View.OnClickListener() {
  181. @Override
  182. public void onClick(View v) {
  183. scroll_point.setText(text2);
  184. // Toast.makeText(getApplicationContext(),text2,Toast.LENGTH_SHORT).show();
  185. }
  186. });
  187.  
  188.  
  189. runOnUiThread(new Runnable() {
  190. @Override
  191. public void run() {
  192. linear_view.addView(v);
  193. }
  194. });
  195.  
  196. }
  197.  
  198. private void doAddText(String rank) {
  199. text.setText(rank);
  200.  
  201. }
  202. public void addViewToLinearfromRight(View v){
  203. Log.d("CONTEXT", "View is Adding");
  204. Animation animation;
  205. animation = new TranslateAnimation(
  206. Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
  207. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
  208. );
  209.  
  210. animation.setDuration(400);
  211.  
  212.  
  213. Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_fade_in);
  214. linear3.setAnimation(anim);
  215. linear3.removeAllViews();
  216.  
  217. linear3.addView(v);
  218. v.setAnimation(animation);
  219.  
  220. }
  221. public void addViewToLinearfromLeft(View v){
  222. Animation animation2 = new TranslateAnimation(
  223. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
  224. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
  225. );
  226. animation2.setDuration(400);
  227. linear3.startAnimation(animation2);
  228. new Thread(new Task(v)).start();
  229.  
  230.  
  231. }
  232. public void addNewView(View v){
  233.  
  234.  
  235. }
  236.  
  237. private class Task implements Runnable{
  238. View v;
  239. Task(View v){
  240. this.v = v;
  241. }
  242. @Override
  243. public void run() {
  244. try {
  245. Thread.sleep(400);
  246. } catch (InterruptedException e) {
  247. e.printStackTrace();
  248. }
  249. linear3.post(new Runnable() {
  250. @Override
  251. public void run() {
  252. Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_fade_in);
  253. linear3.removeAllViews();
  254. v.setAnimation(anim);
  255. linear3.addView(v);
  256. }
  257. });
  258.  
  259. }
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment