Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.innovare.scrollhorizontal;
- import android.annotation.TargetApi;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.PorterDuff;
- import android.graphics.PorterDuffXfermode;
- import android.graphics.Rect;
- import android.graphics.RectF;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.provider.Contacts;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.ViewGroup;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.view.animation.TranslateAnimation;
- import android.widget.Button;
- import android.widget.Gallery;
- import android.widget.HorizontalScrollView;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.ListAdapter;
- import android.widget.RelativeLayout;
- import android.widget.SimpleAdapter;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.w3c.dom.Text;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.logging.Handler;
- public class MainActivity extends AppCompatActivity implements ScrollViewListener{
- ObservableScrollView soriscroll;
- JSONArray contacts = null;
- ArrayList<HashMap<String, String>> contactList;
- JSONObject jsonobject;
- JSONArray jsonarray;
- TextView text;
- LinearLayout linear_view;
- TextView scroll_point;
- LinearLayout linear3;
- Button addcontacts;
- //private static String url = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";
- //private static String url = "http://www.mocky.io/v2/59a94ceb100000200c3e0a78";
- //private static String url = "https://pastebin.com/raw/bPzqq2Kd";
- //private static String url = "https://pastebin.com/raw/e4ZZmwuC";
- private static String url = "https://pastebin.com/raw/rzj39Smd";
- // JSON Node names
- private static final String TAG_CONTACTS = "contacts";
- private static final String TAG_ID = "id";
- private static final String TAG_NAME = "name";
- private static final String TAG_EMAIL = "email";
- private static final String TAG_ADDRESS = "address";
- private static final String TAG_GENDER = "gender";
- private static final String TAG_PHONE = "phone";
- private static final String TAG_PHONE_MOBILE = "mobile";
- private static final String TAG_PHONE_HOME = "home";
- private static final String TAG_PHONE_OFFICE = "office";
- TextView tv ;
- boolean count=true;
- RelativeLayout relative = null;
- @TargetApi(Build.VERSION_CODES.M)
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- text = (TextView) findViewById(R.id.text);
- new downloadData().execute();
- linear3 = (LinearLayout) findViewById(R.id.linear3);
- relative = (RelativeLayout) findViewById(R.id.relative);
- relative.setOnTouchListener(new RelativeLayoutTouchListener(this));
- linear_view = (LinearLayout) findViewById(R.id.linear_view);
- soriscroll = (ObservableScrollView) findViewById(R.id.hori_scroll);
- soriscroll.setScrollViewListener(this);
- scroll_point = (TextView) findViewById(R.id.scroll_point);
- addcontacts = (Button) findViewById(R.id.add_contacts_button);
- addcontacts.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent addContactIntent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
- addContactIntent.putExtra(Contacts.Intents.Insert.NAME, "Jean-Claude"); // an example, there is other data available
- startActivity(addContactIntent);
- }
- });
- soriscroll.setSmoothScrollingEnabled(true);
- /* soriscroll.setOnScrollChangeListener(new HorizontalScrollView.OnScrollChangeListener() {
- @Override
- public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
- int a = linear_view.getWidth();
- int b = soriscroll.getWidth();
- if ((a - b) == scrollX) {
- new downloadData().execute();
- }
- scroll_point.setText("linear_view" + a + "\nscroll_view" + b + "\n" + scrollX + " : " + scrollY);
- }
- });*/
- /* Bitmap bm = BitmapFactory.decodeResource(getResources(),
- R.drawable.mahesh);
- Bitmap bm2 = BitmapFactory.decodeResource(getResources(),
- R.drawable.alluarjun);
- Bitmap bm3 = BitmapFactory.decodeResource(getResources(),
- R.drawable.nikhil);
- Bitmap bm4 = BitmapFactory.decodeResource(getResources(),
- R.drawable.ntr);
- Bitmap bm5 = BitmapFactory.decodeResource(getResources(),
- R.drawable.prabhas);
- Bitmap bm6 = BitmapFactory.decodeResource(getResources(),
- R.drawable.rock);
- Bitmap bm7 = BitmapFactory.decodeResource(getResources(),
- R.drawable.salman);
- Bitmap bm8 = BitmapFactory.decodeResource(getResources(),
- R.drawable.shahid);
- Bitmap bm9 = BitmapFactory.decodeResource(getResources(),
- R.drawable.varun);
- // set circle bitmap
- ImageView one = (ImageView) findViewById(R.id.one);
- ImageView two = (ImageView) findViewById(R.id.two);
- ImageView three = (ImageView) findViewById(R.id.three);
- ImageView four = (ImageView) findViewById(R.id.four);
- ImageView five = (ImageView) findViewById(R.id.five);
- ImageView six = (ImageView) findViewById(R.id.six);
- ImageView seven = (ImageView) findViewById(R.id.seven);
- ImageView eight = (ImageView) findViewById(R.id.eight);
- ImageView nine = (ImageView) findViewById(R.id.nine);
- one.setImageBitmap(getCircleBitmap(bm));
- two.setImageBitmap(getCircleBitmap(bm2));
- three.setImageBitmap(getCircleBitmap(bm3));
- four.setImageBitmap(getCircleBitmap(bm4));
- five.setImageBitmap(getCircleBitmap(bm5));
- six.setImageBitmap(getCircleBitmap(bm6));
- seven.setImageBitmap(getCircleBitmap(bm7));
- eight.setImageBitmap(getCircleBitmap(bm8));
- nine.setImageBitmap(getCircleBitmap(bm9));*/
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- }
- @Override
- public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
- int a = linear_view.getWidth();
- int b = soriscroll.getWidth();
- if ((a - b) == x) {
- new downloadData().execute();
- }
- scroll_point.setText("linear_view" + a + "\nscroll_view" + b + "\n" + x + " : " + y);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- private Bitmap getCircleBitmap(Bitmap bitmap) {
- final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
- bitmap.getHeight(), Bitmap.Config.ARGB_8888);
- final Canvas canvas = new Canvas(output);
- final int color = Color.RED;
- final Paint paint = new Paint();
- final Rect rect = new Rect(0, 0, 500, 500);
- final RectF rectF = new RectF(rect);
- paint.setAntiAlias(true);
- canvas.drawARGB(0, 0, 0, 0);
- paint.setColor(color);
- canvas.drawOval(rectF, paint);
- paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
- canvas.drawBitmap(bitmap, rect, rect, paint);
- if (bitmap != null && !bitmap.isRecycled()) {
- bitmap.recycle();
- bitmap = null;
- }
- return output;
- }
- private class downloadData extends AsyncTask<Void,Void,Void>{
- ProgressDialog pDialog = null;
- String text1="";
- ArrayList<HashMap<String,String>> arrayList = new ArrayList<>();
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- // Showing progress dialog
- if(count) {
- pDialog = new ProgressDialog(MainActivity.this);
- pDialog.setMessage("Please wait...");
- pDialog.setCancelable(false);
- pDialog.show();
- }
- }
- @Override
- protected Void doInBackground(Void... params) {
- ServiceHandler sh = new ServiceHandler();
- // Making a request to url and getting response
- String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
- Log.d("Response: ", "> " + jsonStr);
- if (jsonStr != null) {
- try {
- JSONObject jsonObj = new JSONObject(jsonStr);
- // Getting JSON Array node
- jsonarray = jsonObj.getJSONArray("worldpopulation");
- //JSONArray jsonarray= new JSONArray();
- for (int i = 0; i < jsonarray.length(); i++) {
- HashMap<String, String> map = new HashMap<String, String>();
- jsonobject = jsonarray.getJSONObject(i);
- //String rank = jsonobject.getString("rank");
- String author = jsonobject.getString("author");
- //String population = jsonobject.getString("population");
- String photo = jsonobject.getString("photo");
- //String dummy = "\n\n\n"+rank+"\t"+country+"\n"+population+"\n"+flag;
- //text1 = text1+dummy;
- addToLinearLayout("",author,"",photo);
- // final TextView rowTextView = new TextView(getApplicationContext());
- // linear_view
- // Retrive JSON Objects
- //map.put("rank", rank);
- //map.put("country", country);
- //map.put("population",population );
- //map.put("flag", flag);
- // Set the JSON Objects into the array
- //arrayList.add(map);
- }
- // looping through All Contacts
- // for (int i = 0; i < contacts.length(); i++) {
- /* JSONObject c = contacts.getJSONObject(i);
- String id = c.getString(TAG_ID);
- String name = c.getString(TAG_NAME);
- String email = c.getString(TAG_EMAIL);
- String address = c.getString(TAG_ADDRESS);
- String gender = c.getString(TAG_GENDER);
- // Phone node is JSON Object
- JSONObject phone = c.getJSONObject(TAG_PHONE);
- String mobile = phone.getString(TAG_PHONE_MOBILE);
- String home = phone.getString(TAG_PHONE_HOME);
- String office = phone.getString(TAG_PHONE_OFFICE);
- // tmp hashmap for single contact
- HashMap<String, String> contact = new HashMap<String, String>();
- // adding each child node to HashMap key => value
- contact.put(TAG_ID, id);
- contact.put(TAG_NAME, name);
- contact.put(TAG_EMAIL, email);
- contact.put(TAG_PHONE_MOBILE, mobile);
- // adding contact to contact list
- contactList.add(contact);*/
- // }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- } else {
- Log.e("ServiceHandler", "Couldn't get any data from the url");
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
- // Dismiss the progress dialog
- if(count) {
- if (pDialog.isShowing())
- pDialog.dismiss();
- count= false;
- }
- /**
- * Updating parsed JSON data into ListView
- * */
- /* ListAdapter adapter = new SimpleAdapter(
- MainActivity.this, contactList,
- R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
- TAG_PHONE_MOBILE }, new int[] { R.id.name,
- R.id.email, R.id.mobile });
- setListAdapter(adapter);*/
- }
- }
- private void addToLinearLayout(String text1, final String text2,String text3,String text4) {
- LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
- final View v = inflater.inflate(R.layout.scroll_element, linear_view, false);
- TextView tv = (TextView) v.findViewById(R.id.rank);
- final TextView tv2 = (TextView) v.findViewById(R.id.name);
- TextView tv3 = (TextView) v.findViewById(R.id.population);
- // TextView tv4 = (TextView) v.findViewById(R.id.imagelink);
- ImageView iv = (ImageView) v.findViewById(R.id.imagelink);
- InputStream input = null;
- Bitmap bitmap = null;
- try {
- input = new java.net.URL(text4).openStream();
- bitmap = BitmapFactory.decodeStream(input);
- } catch (IOException e) {
- e.printStackTrace();
- }
- // Decode Bitmap
- tv.setText(text1);
- tv2.setText(text2);
- tv3.setText(text3);
- iv.setImageBitmap(bitmap);
- v.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- scroll_point.setText(text2);
- // Toast.makeText(getApplicationContext(),text2,Toast.LENGTH_SHORT).show();
- }
- });
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- linear_view.addView(v);
- }
- });
- }
- private void doAddText(String rank) {
- text.setText(rank);
- }
- public void addViewToLinearfromRight(View v){
- Log.d("CONTEXT", "View is Adding");
- Animation animation;
- animation = new TranslateAnimation(
- Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
- Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
- );
- animation.setDuration(400);
- Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_fade_in);
- linear3.setAnimation(anim);
- linear3.removeAllViews();
- linear3.addView(v);
- v.setAnimation(animation);
- }
- public void addViewToLinearfromLeft(View v){
- Animation animation2 = new TranslateAnimation(
- Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
- Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
- );
- animation2.setDuration(400);
- linear3.startAnimation(animation2);
- new Thread(new Task(v)).start();
- }
- public void addNewView(View v){
- //linear3.setAnimation(null);
- // new Thread(new Task(View v)).start();
- }
- private class Task implements Runnable{
- View v;
- Task(View v){
- this.v = v;
- }
- @Override
- public void run() {
- try {
- Thread.sleep(400);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- linear3.post(new Runnable() {
- @Override
- public void run() {
- Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_fade_in);
- // linear3.addView(v);
- linear3.removeAllViews();
- v.setAnimation(anim);
- linear3.addView(v);
- }
- });
- /* addNewView(v);*/
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment