Monday, 8 February 2016

Android Programming Tutorial 57: Retrofit Example in Android

Creating a Project In Android Studio:

File >> New >> Android Application
Enter Project name: RetrofitExample
Package: com.ambilpursunil.retrofitexample
Keep other default selections, click Next until you reach Finish



  Simply Copy and Past the Code which is display below in the  MainActivity.java

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.retrofitexample.adapter.Adapter;
import com.retrofitexample.model.Flower;
import com.retrofitexample.network.api;

import java.util.List;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class MainActivity extends ListActivity {

    List<Flower> dataList;

    @Override    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final RestAdapter restAdapter = new RestAdapter.Builder()
   .setEndpoint("http://services.hanselandpetal.com/").build();
    api dataAPI = restAdapter.create(api.class);

    dataAPI.getData(new Callback<List<Flower>>() {
    @Override    public void success(List<Flower> dataTypes, Response response) {
    dataList = dataTypes;
    Adapter adapter = new Adapter(getApplicationContext(),
    R.layout.item_file, dataList);
    setListAdapter(adapter);
    }
    @Override    public void failure(RetrofitError error) {
    Toast.makeText(getApplicationContext(),
    "SomeThing Went Wrong....!", Toast.LENGTH_SHORT).show();
    }
    });
    }
}

Now Create an Interface Simply Copy and Past the Code which is display below in the  api.java (Interface)
import com.retrofitexample.model.Flower;
import java.util.List;
import retrofit.Callback;
import retrofit.http.GET;

public interface api {

@GET("/feeds/flowers.json")
public void getData(Callback<List<Flower>> response);
}

 Now Create a Data Type Class and Simply Copy and Past the Code which is display below in the  Flower.java 
import android.graphics.Bitmap;
   public class Flower {
    int protductID;
    String name;
    String price;
    String instructions;
    String photo;
    String category;
    Bitmap bitmap;

    public int getProtductID() {
        return protductID;
    }

    public void setProtductID(int protductID) {
        this.protductID = protductID;
    }

    public String getName() {
        return name;
    }

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

    public String getPrice() {
        return price;
    }

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

    public String getInstructions() {
        return instructions;
    }

    public void setInstructions(String instructions) {
        this.instructions = instructions;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }
}
 Now Create Adapter Class to attach Layout and Simply Copy and Past the Code which is display below in the  Adapter.java 
import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.retrofitexample.R; import com.retrofitexample.model.Flower; import java.util.List; public class Adapter extends ArrayAdapter<Flower> { String url = "http://services.hanselandpetal.com/photos/"; Context context; List<Flower> dataItems; TextView tv; ImageView imgV; public Adapter(Context context, int resource, List<Flower> objects) { super(context, resource, objects); this.context = context; this.dataItems = objects; } @Override public View getView(int position, View convertView, ViewGroup parent) { ayoutInflater inflater = (LayoutInflater)context.
    getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.item_file, parent, false);
    Flower data = dataItems.get(position);
    tv = (TextView) view.findViewById(R.id.name);
    tv.setText(data.getName());
    imgV = (ImageView) view.findViewById(R.id.imageView);
    Glide.with(getContext()).load(url + data.getPhoto()).into(imgV);
    return view;
    }
}
 Create a layout and Past the Code which is display below in the  activity_main.xml
<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"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@android:id/list" /> </RelativeLayout>
Create a layout and Past the Code which is display below in the  item_data.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">
    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:textColor="#ff010101" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2" />
</LinearLayout>
Add Following jar in build.gradle display below in the  
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.github.bumptech.glide:glide:+'
    compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0'
}
Copy and past the code for manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ambilpursunil.newapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="23" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            </application>
</manifest>
Run Your Project as the below by clicking on shift+10 or like the below-->Android Application and observe the output
      

Please Send Your Comments To ambilpura.sunil@gmail.com

Stay Tune For Next Tutorial... About RecyclerView and Example In Android Studio

Android Programming Tutorial 56: How To Import The Project From Outside To Android Studio.

Importing The Project In The Android Studio:

STEP 1: Right Click On the Projcet ==> choose Import Project As Show In The Window


STEP 2: Give The Location Where You Store the Project
For Example: I store a Sample Project on DESKTOP,Just Choose the project CardViewExample and click on  OK.



STEP  3:Once All Completed The finally screen looks like this


Please Send Your Comments To ambilpura.sunil@gmail.com


Stay Tune For Next Tutorial... Retrofit Example in AndroidTo Android Studio.


Sunday, 7 February 2016

Android Programming Tutorial 55 : APIs OR (Packages Installation PART-II) in Android Studio


APIs Or (Packages Installation PART-II) in Android Studio:

Click on the Android Device Monitor symbol from Top right side.




You See the new window like this



Now Click on the Down arrow symbol from top right.


You Will see



Now Mark the All The Packages APIs and click on the install package button.



Once you click on the install package button you will redirect to new window to accept license agreemen. click on Install button


You can see loading window...

 

Please Send Your Comments To ambilpura.sunil@gmail.com



Stay Tune For Next Tutorial...How To Import The Project From Out To Android Studio.

Android Programming Tutorial 54 : Guidelines In Creation Of First Projects In Android Studio

Create Android Application In Studio:

The first step is to create a simple Android Application using Eclipse IDE. Follow the option File -> New ->New Project.



Now Give Project Name And Select the project location and click on NEXT



Now Give the Minimum , Target and Compile SDK Version and click on NEXT



Choose Blank Activity as shown Below nad click on NEXT.



Now Give Application Name And Layout Name click on Finish.



Finally It Look Like This.


                                      

Please Send Your Comments To ambilpura.sunil@gmail.com



Stay Tune For Next Tutorial...  APIs Installed (Packages Installation PART-II) in Android Studio.



Saturday, 6 February 2016

Android Programming Tutorial 53 : Creating AVD Or Emulator For Android Studio

Android Virtual Devices(AVD Or Emulator):

An Android Virtual Device or AVD is an emulator configuration, allowing you to model an Android device. This makes running and testing applications on a wide range of devices much easier. With an Android Virtual Device, you can specify the hardware and software the Android Emulator needs to emulate.
The preferred way to create an Android Virtual Device is through the AVD Manager, which you can access in Android Studio by selecting Android > AVD Manager from the Tools menu.


                                    

If you're development environment is set up correctly, the Android Virtual Device Manager should look similar to the screenshot below.



To create a new AVD, click New... on the right, give the AVD a name, and configure the virtual device as shown below. Click OK to create your first AVD.




After Clicking On OK Button

It will look some thing like this



Now To Start the emulator Just Click on the AVD ,Then Start button After Few Minutes It will Look like this



Please Send Your Comments To ambilpura.sunil@gmail.com





Stay Tune For Next Tutorial...  Guidelines In Creation Of First Projects In Android Studio

Android Programming Tutorial 52 : How To Create New Activity(Java Class) and Layouts In Android Studio

Following Are The Steps To  Create Activities(Java Class) and Layouts For Our Project:

Step 1:
Right Click On the Project and Choose New ==> Activity ==>Blank Activity.



Give Activity Name and Layout Name as the below





Step 2:
Right Click On the Project and Choose New ==> Choose XML ==> Choose Layout XML File ==> Give Layout Name and Click On Finish. as shown below.








Please Send Your Comments To ambilpura.sunil@gmail.com





Stay Tune For Next Tutorial...  Creating AVD Or Emulator For Android Studio

Android Programming Tutorial 51 : How To Start Working With Android Studio and Installing APIs

To Work with Android Studio:
1. You have to install JAVA(1.7 or 1.8)
2. Then Goto http://developer.android.com/ and  click on the button as below Download Android Studio For Window.
Like the below:
 
                         

Click On the Download Android Studio for window button as shown in the figure.once you click on the button it will redirect to another window asking for the Accept License agreement mark the check to Ok and click on Download button as show below;



3.It will take few minutes to get download.
4.After completing the Android Studio Run the .exe file and followed by next button by reading all the windows.
5.When you click on the the .exe Android Studio will get installed once it complete the installation it will open the Android Studio for u like the below.


                                   


Once Android Studio get Installed Goto==>Start==>Choose Android Sudion and click on enter.Once you click on enter Android Studio will fired out like this



How To Install APIs in Android Studio:

To Install the Packages(APIs) We Have two options

1st: Goto the Default path of the Android Studio where you install the Studio software like

C:\Users\sunil.ambilpura\AppData\Local\Android\sdk  
 and choose the SDK Manager for the following files.



When you click on the Sdk Manager you will see the following window




Now Mark All the APIs and click on the install package button,


Next accept the license and click on the install option then all the APIs will be downloaded from site its will take 1-5 Hours or More Than 5 hours as your NET speed to get downlaod.

It Will Looks Like This while Downloading The APIs.





Please Send Your Comments To ambilpura.sunil@gmail.com





Stay Tune For Next Tutorial...  How To Create New Activity(Java Class) and Layouts in Android Studio