mhnds

Untitled

Oct 23rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.80 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. JSONArray contacts = null;
  59. ArrayList<HashMap<String, String>> contactList;
  60. JSONObject jsonobject;
  61. JSONArray jsonarray;
  62. TextView text;
  63. LinearLayout linear_view;
  64. TextView scroll_point;
  65. LinearLayout linear3;
  66. Button addcontacts;
  67.  
  68.  
  69.  
  70. //private static String url = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";
  71. //private static String url = "http://www.mocky.io/v2/59a94ceb100000200c3e0a78";
  72. //private static String url = "https://pastebin.com/raw/bPzqq2Kd";
  73. //private static String url = "https://pastebin.com/raw/e4ZZmwuC";
  74. private static String url = "https://pastebin.com/raw/rzj39Smd";
  75.  
  76. // JSON Node names
  77. private static final String TAG_CONTACTS = "contacts";
  78. private static final String TAG_ID = "id";
  79. private static final String TAG_NAME = "name";
  80. private static final String TAG_EMAIL = "email";
  81. private static final String TAG_ADDRESS = "address";
  82. private static final String TAG_GENDER = "gender";
  83. private static final String TAG_PHONE = "phone";
  84. private static final String TAG_PHONE_MOBILE = "mobile";
  85. private static final String TAG_PHONE_HOME = "home";
  86. private static final String TAG_PHONE_OFFICE = "office";
  87. TextView tv ;
  88. boolean count=true;
  89.  
  90. RelativeLayout relative = null;
  91.  
  92.  
  93.  
  94.  
  95. @TargetApi(Build.VERSION_CODES.M)
  96. @Override
  97. protected void onCreate(Bundle savedInstanceState) {
  98. super.onCreate(savedInstanceState);
  99. setContentView(R.layout.activity_main);
  100. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  101. setSupportActionBar(toolbar);
  102.  
  103.  
  104.  
  105.  
  106.  
  107. text = (TextView) findViewById(R.id.text);
  108. new downloadData().execute();
  109.  
  110. linear3 = (LinearLayout) findViewById(R.id.linear3);
  111.  
  112. relative = (RelativeLayout) findViewById(R.id.relative);
  113. relative.setOnTouchListener(new RelativeLayoutTouchListener(this));
  114.  
  115. linear_view = (LinearLayout) findViewById(R.id.linear_view);
  116.  
  117.  
  118. soriscroll = (ObservableScrollView) findViewById(R.id.hori_scroll);
  119. soriscroll.setScrollViewListener(this);
  120.  
  121.  
  122. scroll_point = (TextView) findViewById(R.id.scroll_point);
  123.  
  124. addcontacts = (Button) findViewById(R.id.add_contacts_button);
  125. addcontacts.setOnClickListener(new View.OnClickListener() {
  126. @Override
  127. public void onClick(View v) {
  128. Intent addContactIntent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
  129. addContactIntent.putExtra(Contacts.Intents.Insert.NAME, "Jean-Claude"); // an example, there is other data available
  130. startActivity(addContactIntent);
  131. }
  132. });
  133.  
  134.  
  135. soriscroll.setSmoothScrollingEnabled(true);
  136.  
  137. /* soriscroll.setOnScrollChangeListener(new HorizontalScrollView.OnScrollChangeListener() {
  138. @Override
  139. public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
  140. int a = linear_view.getWidth();
  141. int b = soriscroll.getWidth();
  142. if ((a - b) == scrollX) {
  143. new downloadData().execute();
  144. }
  145. scroll_point.setText("linear_view" + a + "\nscroll_view" + b + "\n" + scrollX + " : " + scrollY);
  146. }
  147. });*/
  148.  
  149. /* Bitmap bm = BitmapFactory.decodeResource(getResources(),
  150. R.drawable.mahesh);
  151. Bitmap bm2 = BitmapFactory.decodeResource(getResources(),
  152. R.drawable.alluarjun);
  153. Bitmap bm3 = BitmapFactory.decodeResource(getResources(),
  154. R.drawable.nikhil);
  155. Bitmap bm4 = BitmapFactory.decodeResource(getResources(),
  156. R.drawable.ntr);
  157. Bitmap bm5 = BitmapFactory.decodeResource(getResources(),
  158. R.drawable.prabhas);
  159. Bitmap bm6 = BitmapFactory.decodeResource(getResources(),
  160. R.drawable.rock);
  161. Bitmap bm7 = BitmapFactory.decodeResource(getResources(),
  162. R.drawable.salman);
  163. Bitmap bm8 = BitmapFactory.decodeResource(getResources(),
  164. R.drawable.shahid);
  165. Bitmap bm9 = BitmapFactory.decodeResource(getResources(),
  166. R.drawable.varun);
  167. // set circle bitmap
  168. ImageView one = (ImageView) findViewById(R.id.one);
  169. ImageView two = (ImageView) findViewById(R.id.two);
  170. ImageView three = (ImageView) findViewById(R.id.three);
  171. ImageView four = (ImageView) findViewById(R.id.four);
  172. ImageView five = (ImageView) findViewById(R.id.five);
  173. ImageView six = (ImageView) findViewById(R.id.six);
  174. ImageView seven = (ImageView) findViewById(R.id.seven);
  175. ImageView eight = (ImageView) findViewById(R.id.eight);
  176. ImageView nine = (ImageView) findViewById(R.id.nine);
  177. one.setImageBitmap(getCircleBitmap(bm));
  178. two.setImageBitmap(getCircleBitmap(bm2));
  179. three.setImageBitmap(getCircleBitmap(bm3));
  180. four.setImageBitmap(getCircleBitmap(bm4));
  181. five.setImageBitmap(getCircleBitmap(bm5));
  182. six.setImageBitmap(getCircleBitmap(bm6));
  183. seven.setImageBitmap(getCircleBitmap(bm7));
  184. eight.setImageBitmap(getCircleBitmap(bm8));
  185. nine.setImageBitmap(getCircleBitmap(bm9));*/
  186.  
  187. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  188. fab.setOnClickListener(new View.OnClickListener() {
  189. @Override
  190. public void onClick(View view) {
  191. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  192. .setAction("Action", null).show();
  193. }
  194. });
  195.  
  196. }
  197. @Override
  198. public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
  199. int a = linear_view.getWidth();
  200. int b = soriscroll.getWidth();
  201. if ((a - b) == x) {
  202. new downloadData().execute();
  203. }
  204. scroll_point.setText("linear_view" + a + "\nscroll_view" + b + "\n" + x + " : " + y);
  205.  
  206. }
  207.  
  208. @Override
  209. public boolean onCreateOptionsMenu(Menu menu) {
  210. // Inflate the menu; this adds items to the action bar if it is present.
  211. getMenuInflater().inflate(R.menu.menu_main, menu);
  212. return true;
  213. }
  214.  
  215. @Override
  216. public boolean onOptionsItemSelected(MenuItem item) {
  217. // Handle action bar item clicks here. The action bar will
  218. // automatically handle clicks on the Home/Up button, so long
  219. // as you specify a parent activity in AndroidManifest.xml.
  220. int id = item.getItemId();
  221.  
  222. //noinspection SimplifiableIfStatement
  223. if (id == R.id.action_settings) {
  224. return true;
  225. }
  226. return super.onOptionsItemSelected(item);
  227. }
  228.  
  229.  
  230. private Bitmap getCircleBitmap(Bitmap bitmap) {
  231. final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
  232. bitmap.getHeight(), Bitmap.Config.ARGB_8888);
  233. final Canvas canvas = new Canvas(output);
  234.  
  235. final int color = Color.RED;
  236. final Paint paint = new Paint();
  237. final Rect rect = new Rect(0, 0, 500, 500);
  238. final RectF rectF = new RectF(rect);
  239.  
  240. paint.setAntiAlias(true);
  241. canvas.drawARGB(0, 0, 0, 0);
  242. paint.setColor(color);
  243. canvas.drawOval(rectF, paint);
  244.  
  245. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  246. canvas.drawBitmap(bitmap, rect, rect, paint);
  247. if (bitmap != null && !bitmap.isRecycled()) {
  248. bitmap.recycle();
  249. bitmap = null;
  250. }
  251.  
  252. return output;
  253. }
  254.  
  255.  
  256.  
  257.  
  258. private class downloadData extends AsyncTask<Void,Void,Void>{
  259. ProgressDialog pDialog = null;
  260. String text1="";
  261. ArrayList<HashMap<String,String>> arrayList = new ArrayList<>();
  262. @Override
  263. protected void onPreExecute() {
  264. super.onPreExecute();
  265. // Showing progress dialog
  266. if(count) {
  267. pDialog = new ProgressDialog(MainActivity.this);
  268. pDialog.setMessage("Please wait...");
  269. pDialog.setCancelable(false);
  270. pDialog.show();
  271. }
  272.  
  273. }
  274. @Override
  275. protected Void doInBackground(Void... params) {
  276. ServiceHandler sh = new ServiceHandler();
  277.  
  278. // Making a request to url and getting response
  279. String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
  280.  
  281. Log.d("Response: ", "> " + jsonStr);
  282.  
  283. if (jsonStr != null) {
  284. try {
  285. JSONObject jsonObj = new JSONObject(jsonStr);
  286.  
  287. // Getting JSON Array node
  288.  
  289.  
  290.  
  291. jsonarray = jsonObj.getJSONArray("worldpopulation");
  292. //JSONArray jsonarray= new JSONArray();
  293. for (int i = 0; i < jsonarray.length(); i++) {
  294. HashMap<String, String> map = new HashMap<String, String>();
  295. jsonobject = jsonarray.getJSONObject(i);
  296. //String rank = jsonobject.getString("rank");
  297. String author = jsonobject.getString("author");
  298. //String population = jsonobject.getString("population");
  299. String photo = jsonobject.getString("photo");
  300. //String dummy = "\n\n\n"+rank+"\t"+country+"\n"+population+"\n"+flag;
  301. //text1 = text1+dummy;
  302. addToLinearLayout("",author,"",photo);
  303. // final TextView rowTextView = new TextView(getApplicationContext());
  304. // linear_view
  305. // Retrive JSON Objects
  306. //map.put("rank", rank);
  307. //map.put("country", country);
  308. //map.put("population",population );
  309. //map.put("flag", flag);
  310. // Set the JSON Objects into the array
  311. //arrayList.add(map);
  312. }
  313.  
  314. // looping through All Contacts
  315. // for (int i = 0; i < contacts.length(); i++) {
  316.  
  317. /* JSONObject c = contacts.getJSONObject(i);
  318.  
  319. String id = c.getString(TAG_ID);
  320. String name = c.getString(TAG_NAME);
  321. String email = c.getString(TAG_EMAIL);
  322. String address = c.getString(TAG_ADDRESS);
  323. String gender = c.getString(TAG_GENDER);
  324.  
  325. // Phone node is JSON Object
  326. JSONObject phone = c.getJSONObject(TAG_PHONE);
  327. String mobile = phone.getString(TAG_PHONE_MOBILE);
  328. String home = phone.getString(TAG_PHONE_HOME);
  329. String office = phone.getString(TAG_PHONE_OFFICE);
  330.  
  331. // tmp hashmap for single contact
  332. HashMap<String, String> contact = new HashMap<String, String>();
  333.  
  334. // adding each child node to HashMap key => value
  335. contact.put(TAG_ID, id);
  336. contact.put(TAG_NAME, name);
  337. contact.put(TAG_EMAIL, email);
  338. contact.put(TAG_PHONE_MOBILE, mobile);
  339.  
  340. // adding contact to contact list
  341. contactList.add(contact);*/
  342. // }
  343. } catch (JSONException e) {
  344. e.printStackTrace();
  345. }
  346. } else {
  347. Log.e("ServiceHandler", "Couldn't get any data from the url");
  348. }
  349.  
  350. return null;
  351. }
  352. @Override
  353. protected void onPostExecute(Void result) {
  354. super.onPostExecute(result);
  355. // Dismiss the progress dialog
  356. if(count) {
  357. if (pDialog.isShowing())
  358. pDialog.dismiss();
  359. count= false;
  360. }
  361. /**
  362. * Updating parsed JSON data into ListView
  363. * */
  364. /* ListAdapter adapter = new SimpleAdapter(
  365. MainActivity.this, contactList,
  366. R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
  367. TAG_PHONE_MOBILE }, new int[] { R.id.name,
  368. R.id.email, R.id.mobile });
  369.  
  370. setListAdapter(adapter);*/
  371. }
  372. }
  373.  
  374. private void addToLinearLayout(String text1, final String text2,String text3,String text4) {
  375.  
  376.  
  377. LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
  378. final View v = inflater.inflate(R.layout.scroll_element, linear_view, false);
  379.  
  380. TextView tv = (TextView) v.findViewById(R.id.rank);
  381. final TextView tv2 = (TextView) v.findViewById(R.id.name);
  382. TextView tv3 = (TextView) v.findViewById(R.id.population);
  383. // TextView tv4 = (TextView) v.findViewById(R.id.imagelink);
  384. ImageView iv = (ImageView) v.findViewById(R.id.imagelink);
  385. InputStream input = null;
  386. Bitmap bitmap = null;
  387. try {
  388. input = new java.net.URL(text4).openStream();
  389. bitmap = BitmapFactory.decodeStream(input);
  390. } catch (IOException e) {
  391. e.printStackTrace();
  392. }
  393. // Decode Bitmap
  394.  
  395. tv.setText(text1);
  396. tv2.setText(text2);
  397. tv3.setText(text3);
  398. iv.setImageBitmap(bitmap);
  399.  
  400. v.setOnClickListener(new View.OnClickListener() {
  401. @Override
  402. public void onClick(View v) {
  403. scroll_point.setText(text2);
  404. // Toast.makeText(getApplicationContext(),text2,Toast.LENGTH_SHORT).show();
  405. }
  406. });
  407.  
  408.  
  409. runOnUiThread(new Runnable() {
  410. @Override
  411. public void run() {
  412. linear_view.addView(v);
  413. }
  414. });
  415.  
  416. }
  417.  
  418. private void doAddText(String rank) {
  419. text.setText(rank);
  420.  
  421. }
  422. public void addViewToLinearfromRight(View v){
  423. Log.d("CONTEXT", "View is Adding");
  424. Animation animation;
  425. animation = new TranslateAnimation(
  426. Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
  427. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
  428. );
  429.  
  430. animation.setDuration(400);
  431.  
  432.  
  433. Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_fade_in);
  434. linear3.setAnimation(anim);
  435. linear3.removeAllViews();
  436.  
  437. linear3.addView(v);
  438. v.setAnimation(animation);
  439.  
  440. }
  441. public void addViewToLinearfromLeft(View v){
  442. Animation animation2 = new TranslateAnimation(
  443. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
  444. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
  445. );
  446. animation2.setDuration(400);
  447. linear3.startAnimation(animation2);
  448. new Thread(new Task(v)).start();
  449.  
  450.  
  451. }
  452. public void addNewView(View v){
  453. //linear3.setAnimation(null);
  454.  
  455. // new Thread(new Task(View v)).start();
  456.  
  457. }
  458.  
  459. private class Task implements Runnable{
  460. View v;
  461. Task(View v){
  462. this.v = v;
  463. }
  464. @Override
  465. public void run() {
  466. try {
  467. Thread.sleep(400);
  468. } catch (InterruptedException e) {
  469. e.printStackTrace();
  470. }
  471. linear3.post(new Runnable() {
  472. @Override
  473. public void run() {
  474. Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_fade_in);
  475. // linear3.addView(v);
  476. linear3.removeAllViews();
  477. v.setAnimation(anim);
  478. linear3.addView(v);
  479. }
  480. });
  481.  
  482. /* addNewView(v);*/
  483.  
  484. }
  485. }
  486. }
Advertisement
Add Comment
Please, Sign In to add comment