Saturday 28 September 2013

How to setup android developing environment

OverView:
In this tutorial we are showing that how to setup android developing environment with two different approach,One approach is very simple in which we only need to download a ADT Bundle,that not require any additional installation or setup.Other approach is also good but it requires a bit effort in setup.Actually before starting any android app we need a developing environment that provides us the API libraries and developer tools necessary to build, test, and debug apps for Android.


ADT Bundle(Android Developer Tools): 

If you're a new Android developer, we recommend you download the ADT Bundle to quickly start developing apps. It includes the essential Android SDK components and a version of the Eclipse IDE with built-in ADT (Android Developer Tools) to streamline your Android app development.

With a single download, the ADT Bundle includes everything you need to begin developing apps likes:

  • Eclipse + ADT plugin
  • Android SDK Tools
  • Android Platform-tools
  • The latest Android platform
  • The latest Android system image for the emulator
 Case 1: We recommends you to download the ADT Bundle from here: 

ADT Bundle Download

 So lets get started with your first basic setup for Android development.Go to your download folder where you saved your download,Extract the downloaded folder and now you will see the two folder's there one is eclipse and other is sdk folder,hit the eclipse folder and start your eclipse.


  • Go to Window > Preferences > Android and give path to where you have placed your sdk in empty box and click Ok.
  • It will set sdk manger in your eclipse.
  •  Take a better idea from below given image.
                                            


If you are enable to see the sdk manger or avd in your eclipse then follow the below given steps:
Now you can check sdk manager availability if not getting it by you on eclipse:

Click on Window > Customize perspective > Command Groups Availability tab. Check the "Android SDK and AVD Manager" option and hit the OK button




You can enable other things if you required it from above figure.

Once you have got the Android sdk manager and Avd,then congratulation.....now you can develop your own app.


Note: To add extra functionality to your app,and wants to install more packages then go to Android Sdk Manager,and install from there.For more reference see the below given figure.

                                               

you can install different API labels and many more by it.

  Case 2: when you have downloaded the sdk and need a eclipse plug-in to your                     sdk.
  • Installing Android Development Toolkit (ADT) plug-in
  • Open Eclipse s/w and under Help -> Install New Software… 
  • Now you will see a window which allows you to install new plug-in
  • Click on Add button and in Name and in Location give the link https://dl-ssl.google.com/android/eclipse/ and proceed with further steps.

                                                   

You will also see:
                                               
 
 Case 3:   How to start New Android Virtual Device(AVD):-
An Android Virtual Device (AVD) is an emulator configuration that lets you model an actual device by defining hardware and software options to be emulated by the Android Emulator.
The easiest way to create an AVD is to use the graphical avd manager, which you launch from Eclipse by clicking Window > AVD Manager. You can also start the AVD Manager from the command line by calling the android tool with the avd options, from the <sdk>/tools/ directory.

 AVD consists of:

  • A hardware profile: Defines the hardware features of the virtual device. For example, you can define whether the device has a camera, whether it uses a physical QWERTY keyboard or a dialing pad, how much memory it has, and so on.
  • A mapping to a system image: You can define what version of the Android platform will run on the virtual device. You can choose a version of the standard Android platform or the system image packaged with an SDK add-on.
  • Other options: You can specify the emulator skin you want to use with the AVD, which lets you control the screen dimensions, appearance, and so on. You can also specify the emulated SD card to use with the AVD.
  • A dedicated storage area on your development machine: the device's user data (installed applications, settings, and so on) and emulated SD card are stored in this area.
You can create as many AVDs as you need, based on the types of device you want to model. To thoroughly test your application, you should create an AVD for each general device configuration (for example, different screen sizes and platform versions) with which your application is compatible and test your application on each one.

Keep It in mind when create AVD:

You should create at least one AVD that uses a target whose API Level is greater than that required by your application, because it allows you to test the forward-compatibility of your application. Forward-compatibility testing ensures that, when users who have downloaded your application receive a system update, your application will continue to function normally.

  • Now Go to Android Virtual Device Manager and click on it:
You will see the following window:


                                                 

  • Now click on the new button situated in right hand side of open window.You will see the following avd window:
  •  Fill all the required details like target,Internal storage and sd card details etc.
  •   After it click on OK button. 
                                       
                                                  

  •  A new AVD has been created with the given specification and Close the Android SDK and AVD Manager.
  •  Now you have successfully created AVD you are ready to test your application.
  • By hitting the start button and after selecting avd,you have to wait for some moment,because avd takes some second in start-up process.  
  • Finally you will see the similar picture of avd.
                                                       
                                                                                                                                                       










Please Share this tutorial On:

                                               

Thursday 26 September 2013

Android alert dialog example

OverView:

 In this tutorial i will show that how to create a alert dialog with multiple choice(Single Button,Two Button,Three Button).

Dialog:  

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.Dialogs prompt the user for decisions or additional information required by the application to continue a task. Such requests can range from simple Cancel/OK decisions to more complex layouts asking the user to adjust settings or enter text.

Lets takes a simple example to show dialog concept:
  • Create a new project File -> New -> Android Application Project.Now go to “res/layout" and paste the following code under main.xml file.
  • You can give any name to your xml file,In my case it is main.xml.
  • This is a single xml file that will give you all view for alert dialog in this app.
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   

<Button
        android:id="@+id/btn_alert1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="40dip"
        android:padding="20dip"
        android:textSize="17sp"
        android:text="Show Alert With Single Button "
        android:textColor="#00008B" >
    </Button>

    <Button
        android:id="@+id/btn_alert2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:padding="20dip"
        android:textSize="17sp"
        android:text="Show Alert With Two Buttons"
        android:textColor="#00008B" >
    </Button>

    <Button
        android:id="@+id/btn_alert3"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:padding="20dip"
        android:textSize="17sp"
        android:text="Show Alert With Three Buttons"
        android:textColor="#00008B" >
    </Button>

</LinearLayout> 


This will be your main Application Screen:



Now the Java code for your App(Alert dialog with single button):

 This code will create a simple alert dialog with one button. Mainly here is three important methods one is setTitle() method used for set Title to alert dialog. setMessage() is used for setting message to alert dialog. setIcon() is to set icon to alert dialog.This code only show a single button name OK.

     AlertDialog alertDialog = new AlertDialog.Builder(
                        AlertDialogActivity.this).create();

                // Here i am setting Dialog Title
                alertDialog.setTitle("Alert Dialog");

                // now setting Dialog Message
                alertDialog.setMessage("Welcome to AndroidTutorialRkt");

                // here setting Icon to Dialog
                alertDialog.setIcon(R.drawable.tick);

                // here setting OK Button
                alertDialog.setButton("OK",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {

                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on OK", Toast.LENGTH_LONG)
                                        .show();
                            }
                        });

                // Showing Alert Message
                alertDialog.show();



Output for above Code:



Alert dialog with two button:

Below code create two button's in alert dialog,one is YES button and other is NO button.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                        AlertDialogActivity.this);

                // Setting Dialog Title
                alertDialog.setTitle("Confirm Delete.....");

                // Setting Dialog Message
                alertDialog.setMessage("Are you sure you want delete this?");

                // Setting Icon to Dialog
                alertDialog.setIcon(R.drawable.delete);

                // For "Yes" Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on YES",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Setting Negative "NO" Button
                alertDialog.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on NO", Toast.LENGTH_LONG)
                                        .show();
                                dialog.cancel();
                            }
                        });

                // To show the alert msg
                alertDialog.show();  


 Output for above Code:




Alert dialog with three button:

Below code create three button's in alert dialog,1st is YES button  2nd is NO button and 3rd is CANCEL button.



AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                        AlertDialogActivity.this);

                // Set Dialog Title here
                alertDialog.setTitle("Save File.....");

                // Set Dialog Message here
                alertDialog.setMessage("Do you want to save this file?");

                // Set Icon to Dialog here
                alertDialog.setIcon(R.drawable.save);

                // Setting Positive Yes Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on YES",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Setting Positive Yes Button
                alertDialog.setNeutralButton("NO",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on NO", Toast.LENGTH_LONG)
                                        .show();
                            }
                        });
                // Setting Positive "Cancel" Button
                alertDialog.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on Cancel",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Showing Alert Message
                alertDialog.show();
  



 Output for above Code: 




Now the complete code for this app: 

Open your main Java activity class and copy/paste below given code in it,You can assign any name to your Java class as you wants but here i am taking it AlertDialogActivity.java.

 package com.rkt.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AlertDialogActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btnAlertSingle = (Button) findViewById(R.id.btn_alert1);
        Button btnAlert_2Btns = (Button) findViewById(R.id.btn_alert2);
        Button btnAlert_3Btns = (Button) findViewById(R.id.btn_alert3);

        btnAlertSingle.setOnClickListener(new View.OnClickListener() {

            @SuppressWarnings("deprecation")
            public void onClick(View arg0) {
                // First step to create alert Dialog with one Button

                AlertDialog alertDialog = new AlertDialog.Builder(
                        AlertDialogActivity.this).create();

                // Here i am setting Dialog Title
                alertDialog.setTitle("Alert Dialog");

                // now setting Dialog Message
                alertDialog.setMessage("Welcome to AndroidTutorialRkt");

                // here setting Icon to Dialog
                alertDialog.setIcon(R.drawable.tick);

                // here setting OK Button
                alertDialog.setButton("OK",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {

                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on OK",
                                        Toast.LENGTH_LONG).show();
                            }
                        });

                // Showing Alert Message
                alertDialog.show();

            }
        });

        btnAlert_2Btns.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // Now for alert Dialog with two Buttons

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                        AlertDialogActivity.this);

                // Setting Dialog Title
                alertDialog.setTitle("Confirm Delete.....");

                // Setting Dialog Message
                alertDialog.setMessage("Are you sure you want delete this?");

                // Setting Icon to Dialog
                alertDialog.setIcon(R.drawable.delete);

                // For "Yes" Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on YES",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Setting Negative "NO" Button
                alertDialog.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on NO",
                                        Toast.LENGTH_LONG).show();
                                dialog.cancel();
                            }
                        });

                // To show the alert msg
                alertDialog.show();

            }
        });

        btnAlert_3Btns.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // Creating alert Dialog with three Buttons

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                        AlertDialogActivity.this);

                // Set Dialog Title here
                alertDialog.setTitle("Save File.....");

                // Set Dialog Message here
                alertDialog.setMessage("Do you want to save this file?");

                // Set Icon to Dialog here
                alertDialog.setIcon(R.drawable.save);

                // Setting Positive Yes Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on YES",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Setting Positive Yes Button
                alertDialog.setNeutralButton("NO",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on NO",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Setting Positive "Cancel" Button
                alertDialog.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on Cancel",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Showing Alert Message
                alertDialog.show();

            }
        });
    }
}







Please Share this tutorial On:


Wednesday 25 September 2013

Android ListView Example

Overview: 

 In this tutorial i will show how to build simple android ListView. This tutorial is all about creating listview.


What is the ListView & Why we used it?  


ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.So to show the content in list fashion we used ListView.


Now Creating a project in eclipse for ListView.
  •  Create a new project by going to File > New > Android Application Project. Fill all the details and name of your activity,I have taken it ListViewActivity.                                                                                                
  • Copy paste all code that i have given below under your main activity called ListViewActivity.
package com.rkt.listviewexample;

import java.util.ArrayList;
import java.util.Arrays;
import com.rkt.listviewexample.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListViewActivity extends Activity {

    private ListView mainListView;
    private ArrayAdapter<String> listAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        // Find the ListView resource.
        mainListView = (ListView) findViewById(R.id.Androidlist);

        // Create and populate a List of Country names.
        String[] countries = new String[] { "India", "Iran", "Canada",
                "Turkey", "Zimbabwe", "Iceland", "Bahrain", "Australia",
                "Afghanistan" };
        ArrayList<String> al = new ArrayList<String>();
        al.addAll(Arrays.asList(countries));

        // Create ArrayAdapter using the ArrayList al.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, al);



//here you can add items directly in listAdapter
        listAdapter.add("Malaysia");
        listAdapter.add("Mexico");
        listAdapter.add("France");
        listAdapter.add("Israel");
        listAdapter.add("Mongolia");

        // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter(listAdapter);
    }
}


  • Lets start your work on UI part,you need a xml file that show our item's in list manner. So create an XML file under res folder and name it as activity_list_view.xml and paste the following code.
( Right Click on res/layout ⇒ New ⇒ Android XML File)

 <LinearLayout 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"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/Androidlist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffff00" >
    </ListView>

</LinearLayout>


  •  Now you have to create one more xml file  to show the text in listview.Create an XML file under res/layout folder and name it as simplerow.xml and type the following code under this file.This file only contains the TextView to display the text.
(Again right click on res/layout ⇒ New ⇒ Android XML File

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="15dp"
    android:textColor="#191970"
    android:textColorHighlight="#ffff00"
    android:textSize="16sp"
    android:textStyle="bold" >

</TextView> 

  •  Ok,this is the time to run your project by right clicking on your project folder ⇒ Run As ⇒ Android Application.You should see the following result in your device.

Output:

 Please Share this tutorial On: