Wednesday 5 July 2017

Reveal Animation And Ripple Effect in Android Studio.

Creating New Project.
Open android studio and create a new project.


File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.


First we need to add Library to our project.

compile 'com.android.support:design:25.3.1'

Create Xml file in project.
Open => app => res => layout - activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

       <include layout="@layout/app_bar" />

    <LinearLayout
        android:id="@+id/layView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="?attr/actionBarSize"
        android:background="#b3e5fc"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:gravity="center"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageButton
                    android:id="@+id/one"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@mipmap/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="One" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageButton
                    android:id="@+id/two"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@mipmap/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="Two" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageButton
                    android:id="@+id/three"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@mipmap/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="Three" />
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageButton
                    android:id="@+id/four"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@mipmap/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="Four" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageButton
                    android:id="@+id/five"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@mipmap/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="Five" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageButton
                    android:id="@+id/six"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@mipmap/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="Six" />

            </LinearLayout>

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/black" />
    </LinearLayout>

</FrameLayout>


Create Xml file in project.
Open => app => res => layout - app_bar.xml.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />



Create the Java file in project.

Open app => main => src = MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Toolbar toolbar;
    boolean hidden = true;
    LinearLayout viewLinearLayout;
    ImageButton oneImageButton, twoImageButton, threeImageButton, fourImageButton, fiveImageButton, sixImageButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        toolbar = (Toolbar) findViewById(R.id.toolbar);
        viewLinearLayout = (LinearLayout) findViewById(R.id.layView);
        oneImageButton = (ImageButton) findViewById(R.id.one);
        twoImageButton = (ImageButton) findViewById(R.id.two);
        threeImageButton = (ImageButton) findViewById(R.id.three);
        fourImageButton = (ImageButton) findViewById(R.id.four);
        fiveImageButton = (ImageButton) findViewById(R.id.five);
        sixImageButton = (ImageButton) findViewById(R.id.six);
        //Using the click event here...
        oneImageButton.setOnClickListener(this);
        twoImageButton.setOnClickListener(this);
        threeImageButton.setOnClickListener(this);
        fourImageButton.setOnClickListener(this);
        fiveImageButton.setOnClickListener(this);
        sixImageButton.setOnClickListener(this);

        setSupportActionBar(toolbar);

        viewLinearLayout.setVisibility(View.INVISIBLE);
    }

    // Using the click even here...
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.one:
                Snackbar.make(v, "One Clicked", Snackbar.LENGTH_SHORT).show();
                viewLinearLayout.setVisibility(View.INVISIBLE);
                hidden = true;
                break;
            case R.id.two:

                viewLinearLayout.setVisibility(View.INVISIBLE);
                hidden = true;
                break;
            case R.id.three:

                viewLinearLayout.setVisibility(View.INVISIBLE);
                hidden = true;
                break;
            case R.id.four:

                viewLinearLayout.setVisibility(View.INVISIBLE);
                hidden = true;
                break;
            case R.id.five:
                viewLinearLayout.setVisibility(View.INVISIBLE);
                hidden = true;
                break;
            case R.id.six:
                viewLinearLayout.setVisibility(View.INVISIBLE);
                hidden = true;
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.attachment) {// attachment icon click event

            // finding X and Y co-ordinates
            int cx = (viewLinearLayout.getLeft() + viewLinearLayout.getRight());
            int cy = (viewLinearLayout.getTop());

            // to find  radius when icon is tapped for showing layout
            int startradius = 0;
            int endradius = Math.max(viewLinearLayout.getWidth(), viewLinearLayout.getHeight());

            // performing circular reveal when icon will be tapped
            Animator animator = ViewAnimationUtils.createCircularReveal(viewLinearLayout, cx, cy, startradius, endradius);
            animator.setInterpolator(new AccelerateDecelerateInterpolator());
            animator.setDuration(400);

            //reverse animation
            // to find radius when icon is tapped again for hiding layout
            //  starting radius will be the radius or the extent to which circular reveal animation is to be shown

            int reverse_startradius = Math.max(viewLinearLayout.getWidth(), viewLinearLayout.getHeight());

            //endradius will be zero
            int reverse_endradius = 0;

            // performing circular reveal for reverse animation
            Animator animate = ViewAnimationUtils.createCircularReveal(viewLinearLayout, cx, cy, reverse_startradius, reverse_endradius);
            if (hidden) {

                // to show the layout when icon is tapped
                viewLinearLayout.setVisibility(View.VISIBLE);
                animator.start();
                hidden = false;
            } else {
                viewLinearLayout.setVisibility(View.VISIBLE);

                // to hide layout on animation end
                animate.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        viewLinearLayout.setVisibility(View.INVISIBLE);
                        hidden = true;
                    }
                });
                animate.start();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


May this code help you. Thanks!!!!!!


Screen Shot:-








Saturday 1 July 2017

Implement the Custom Expandable ListView in Android.


Creating New Project.

Open android studio and create a new project.


File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.


First we need to add Library to our project.

compile 'com.mcxiaoke.volley:library-aar:1.0.0'


Create Xml file in project.

Open => app => res => layout - activity_main.xml.


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

    <ExpandableListView

        android:id="@+id/exp_list"

        android:layout_width="match_parent"

        android:layout_height="fill_parent" >

    </ExpandableListView>

    

</RelativeLayout>


Open => app => res => layout - child_item.xml.


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_marginLeft="50dp"

    android:paddingBottom="4dp"

    android:paddingTop="4dp" >

    <TextView

        android:id="@+id/country_name"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="TextView"

        android:textSize="20dp" />

    

</RelativeLayout>

Open => app => res => layout - group_item.xml.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:padding="10dp" >

    <TextView

        android:id="@+id/group_name"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:paddingLeft="25dp"

        android:textSize="25dp" />

</LinearLayout>


Create the Java file in project.

Open app => main => src = MainActivity.java


public class MainActivity extends Activity {

    String url = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";

    ProgressDialog dialog;

    private ExpandListAdapter ExpAdapter;

    private ExpandableListView ExpandList;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ExpandList = (ExpandableListView) findViewById(R.id.exp_list);

        dialog = new ProgressDialog(this);

        dialog.setMessage("Loading.....");

        dialog.setCancelable(false);

        getSpinnerData();

    }

    //Get the country data here...

    private void getSpinnerData() {

        dialog.show();

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url,

                null, new Response.Listener<JSONObject>() {

            @Override

            public void onResponse(JSONObject response) {

                ArrayList<Group> list = new ArrayList<Group>();

                ArrayList<Child> ch_list;

                try {

                    Iterator<String> key = response.keys();

                    while (key.hasNext()) {

                        String k = key.next();

                        Group gru = new Group();

                        gru.setName(k);

                        ch_list = new ArrayList<Child>();

                        JSONArray ja = response.getJSONArray(k);

                        for (int i = 0; i < ja.length(); i++) {

                            JSONObject jo = ja.getJSONObject(i);

                            Child ch = new Child();

                            ch.setName(jo.getString("population"));

                            ch_list.add(ch);

                        } // for loop end

                        gru.setItems(ch_list);

                        list.add(gru);

                    } // while loop end

                    ExpAdapter = new ExpandListAdapter(MainActivity.this, list);

                    ExpandList.setAdapter(ExpAdapter);

                    dialog.dismiss();

                } catch (JSONException e) {

                    e.printStackTrace();

                }

            }

        },

                new Response.ErrorListener() {

                    @Override

                    public void onErrorResponse(VolleyError error) {

                        dialog.dismiss();

                    }

                });

        //Creating a request queue

        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue

        requestQueue.add(jsonObjReq);

    }

    //**********END**

}


Create the Java file in project.

Open app => main => src = ExpandListAdapter.java


public class ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;

    private ArrayList<Group> groups;

    public ExpandListAdapter(Context context, ArrayList<Group> groups) {

        this.context = context;

        this.groups = groups;

    }

    @Override

    public Object getChild(int groupPosition, int childPosition) {

        ArrayList<Child> chList = groups.get(groupPosition).getItems();

        return chList.get(childPosition);

    }

    @Override

    public long getChildId(int groupPosition, int childPosition) {

        return childPosition;

    }

    @Override

    public View getChildView(int groupPosition, int childPosition,

                             boolean isLastChild, View convertView, ViewGroup parent) {

        Child child = (Child) getChild(groupPosition, childPosition);

        if (convertView == null) {

            LayoutInflater infalInflater = (LayoutInflater) context

                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);

            convertView = infalInflater.inflate(R.layout.child_item, null);

        }

        TextView tv = (TextView) convertView.findViewById(R.id.country_name);

        tv.setText(child.getName().toString());

        return convertView;

    }

    @Override

    public int getChildrenCount(int groupPosition) {

        ArrayList<Child> chList = groups.get(groupPosition).getItems();

        return chList.size();

    }

    @Override

    public Object getGroup(int groupPosition) {

        return groups.get(groupPosition);

    }

    @Override

    public int getGroupCount() {

        return groups.size();

    }

    @Override

    public long getGroupId(int groupPosition) {

        return groupPosition;

    }

    @Override

    public View getGroupView(int groupPosition, boolean isExpanded,

                             View convertView, ViewGroup parent) {

        Group group = (Group) getGroup(groupPosition);

        if (convertView == null) {

            LayoutInflater inf = (LayoutInflater) context

                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);

            convertView = inf.inflate(R.layout.group_item, null);

        }

        TextView tv = (TextView) convertView.findViewById(R.id.group_name);

        tv.setText(group.getName());

        return convertView;

    }

    @Override

    public boolean hasStableIds() {

        return true;

    }

    @Override

    public boolean isChildSelectable(int groupPosition, int childPosition) {

        return true;

    }

}


Create the Java file in project.

Open app => main => src = Group.java


public class Group {

    private String Name;

    private ArrayList<Child> Items;

    public String getName() {

        return Name;

    }

    public void setName(String name) {

        this.Name = name;

    }

    public ArrayList<Child> getItems() {

        return Items;

    }

    public void setItems(ArrayList<Child> Items) {

        this.Items = Items;

    }

}

Create the Java file in project.

Open app => main => src = Child.java


public class Child {

    private String Name;

    private String Image;

    public String getName() {

        return Name;

    }

    public void setName(String Name) {

        this.Name = Name;

    }

    public String getImage() {

        return Image;

    }

    public void setImage(String Image) {

        this.Image = Image;

    }

}


Add Internet permission in your manifest.

<uses-permission android:name="android.permission.INTERNET" />

Friday 5 May 2017

How to Implement Google Place API  in Android Studio.

Creating New Project.
Open android studio and create a new project.


File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.

First we need to add Library to our project.

compile 'com.google.android.gms:play-services:8.4.0'

Create Xml file in project.
Open => app => res => layout - activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="24dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/icons"
        android:elevation="2dp"
        android:orientation="vertical"
        android:padding="8dp">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_from"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/edtFrom"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:editable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:hint="From" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_to"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp">

            <EditText
                android:id="@+id/edtTo"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:editable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:hint="To" />
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

    <Button
        android:id="@+id/find"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Find Now"
        android:textColor="@color/icons" />
</LinearLayout>



Create the Java file in project.
Open app => main => src =
FragmentFind.java
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.conn.findroute.R;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocomplete;


public class FragmentFind extends Fragment {

    Button mFind;
    EditText mFrom, mTo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bus, container, false);

        init(view);
        eventListener();

        return view;
    }

    /*
    * Initialize UI elements
    * */
    void init(View v) {


        mFind = (Button) v.findViewById(R.id.find);
        mFrom = (EditText) v.findViewById(R.id.edtFrom);
        mTo = (EditText) v.findViewById(R.id.edtTo);
    }

    /*
    * Click event handler
    * */
    private void eventListener() {


        mFind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Snackbar.make(v, "Coming Soon", Snackbar.LENGTH_LONG).show();
                Intent in = new Intent(getActivity(), RouteListActivity.class);
                startActivity(in);
            }
        });

        mFrom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent =
                            new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                                    .build(getActivity());
                    startActivityForResult(intent, 1);
                } catch (GooglePlayServicesRepairableException e) {
                    // TODO: Handle the error.
                } catch (GooglePlayServicesNotAvailableException e) {
                    // TODO: Handle the error.
                }
            }
        });

        mTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent =
                            new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                                    .build(getActivity());
                    startActivityForResult(intent, 2);
                } catch (GooglePlayServicesRepairableException e) {
                    // TODO: Handle the error.
                } catch (GooglePlayServicesNotAvailableException e) {
                    // TODO: Handle the error.
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == getActivity().RESULT_OK) {
                Place place = PlaceAutocomplete.getPlace(getActivity(), data);
                Log.i("FIND", "Place: " + place.getName());
                mFrom.setText(place.getName());
            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(getActivity(), data);
                // TODO: Handle the error.
                Log.i("MAP", status.getStatusMessage());

            } else if (resultCode == getActivity().RESULT_CANCELED) {
                // The user canceled the operation.
            }
        }
        if (requestCode == 2) {
            if (resultCode == getActivity().RESULT_OK) {
                Place place = PlaceAutocomplete.getPlace(getActivity(), data);
                Log.i("FIND", "Place: " + place.getName());
                mTo.setText(place.getName());
            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(getActivity(), data);
                // TODO: Handle the error.
                Log.i("MAP", status.getStatusMessage());

            } else if (resultCode == getActivity().RESULT_CANCELED) {
                // The user canceled the operation.
            }
        }
    }
}


Add Internet permission in your manifest.

<uses-permission android:name="android.permission.INTERNET" />
 <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="key"/>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true"/>

Wednesday 3 May 2017

 JSON Parsing in array with Retrofit in Android Studio.

[
    {
        "Id":"101",
        "Name":"Arun",
        "Marks":"112"
    },
  
    {
        "Id":"102",
        "Name":"Kamal",
        "Marks":"116"
    },
   
]


Creating New Project.
Open android studio and create a new project.


File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.


First we need to add Library to our project.

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.google.code.gson:gson:1.7.2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:okhttp:2.4.0'




Create Xml file in project.
Open => app => res => layout - activity_main.xml.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txtIdOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="150dp" />

    <TextView
        android:id="@+id/txtIdTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="300dp" />

    <TextView
        android:id="@+id/txtNameOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp" />

    <TextView
        android:id="@+id/txtNameTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="270dp" />

    <TextView
        android:id="@+id/txtMarkOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="240dp" />

    <TextView
        android:id="@+id/txtMarkTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp" />

</RelativeLayout>



Create the Java file in project.
Open app => main => src = MainActivity.java



import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import java.util.List;

import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;

public class MainActivity extends AppCompatActivity {

    String url = "http://www.androidtutorialpoint.com/";
    TextView idOneTextView, nameOneTextView, markOneTextView;
    TextView idTwoTextView, nameTwoTextView, markTwoTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        idOneTextView = (TextView) findViewById(R.id.txtIdOne);
        nameOneTextView = (TextView) findViewById(R.id.txtNameOne);
        markOneTextView = (TextView) findViewById(R.id.txtMarkOne);

        idTwoTextView = (TextView) findViewById(R.id.txtIdTwo);
        nameTwoTextView = (TextView) findViewById(R.id.txtNameTwo);
        markTwoTextView = (TextView) findViewById(R.id.txtMarkTwo);

        //Getting the Data here....
        getRetrofitArray();


    }

    void getRetrofitArray() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);

        Call<List<Student>> call = service.getStudentDetails();

        call.enqueue(new Callback<List<Student>>() {
            @Override
            public void onResponse(Response<List<Student>> response, Retrofit retrofit) {

                try {

                    List<Student> StudentData = response.body();

                    for (int i = 0; i < StudentData.size(); i++) {

                        if (i == 0) {
                            idOneTextView.setText("Id  :  " + StudentData.get(i).getStudentId());
                            nameOneTextView.setText("Name  :  " + StudentData.get(i).getStudentName());
                            markOneTextView.setText("Marks  : " + StudentData.get(i).getStudentMarks());
                        } else if (i == 1) {
                            idTwoTextView.setText("Id  :  " + StudentData.get(i).getStudentId());
                            nameTwoTextView.setText("Name  :  " + StudentData.get(i).getStudentName());
                            markTwoTextView.setText("Marks  : " + StudentData.get(i).getStudentMarks());
                        }
                    }


                } catch (Exception e) {
                    Log.d("onResponse", "There is an error");
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Throwable t) {
                Log.d("onFailure", t.toString());
            }
        });
    }


}


Create the Java file in project.
Open app => main => src = RetrofitArrayAPI.java


import java.util.List;
import retrofit.Call;
import retrofit.http.GET;

/**
 * Created by Saify on 5/3/2017.
 **/

public interface RetrofitArrayAPI {

    /*
     * Retrofit get annotation with our URL
     * And our method that will return us details of student.
    */
    @GET("api/RetrofitAndroidArrayResponse")
    Call<List<Student>> getStudentDetails();

}


Create the Java file in project.
Open app => main => src = Student.java


/**
 * Created by Saify on 5/3/2017.
 **/

public class Student {
    //Variables that are in our json
    private int StudentId;
    private String StudentName;
    private String StudentMarks;

    //Getters and setters
    public int getStudentId() {
        return StudentId;
    }

    public void setStudentId(int bookId) {
        this.StudentId = StudentId;
    }

    public String getStudentName() {
        return StudentName;
    }

    public void setStudentName(String name) {
        this.StudentName = StudentName;
    }

    public String getStudentMarks() {
        return StudentMarks;
    }

    public void setStudentMarks(String price) {
        this.StudentMarks = StudentMarks;
    }
}


Add Internet permission in your manifest.
<uses-permission android:name="android.permission.INTERNET" />