Monday 5 May 2014

Android Development Guide

1. What is Android?

                 Android is an operating system based on the Linux kernel, and designed primarily for touchscreen mobile devices such as smartphones and tablet computers. Initially developed by Android, Inc., which Google backed financially and later bought in 2005, Android was unveiled in 2007 along with the founding of the Open Handset Alliance—​a consortium of hardware, software, and telecommunication companies devoted to advancing open standards for mobile devices.
                 Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating system developed by Google and use it in their mobile devices (more on that later). The distinguishing factor of this brand is a kernel. Android hosts a central core, which essentially is a strip code that helps the software operate.

2. Development Tools for Android?

           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:
  • Eclipse + ADT plugin
  • Android SDK Tools
  • Android Platform-tools
  • The latest Android platform
  • The latest Android system image for the emulator
They provide ADT Bundle for different platforms like Windows, Mac, Linux. You can Download the Android SDK from here

3. Android Installation Guide?
           
  1. Unpack the ZIP file (named adt-bundle-windows-x86-20140321) and save it to an appropriate location, such as a "AndroidDevelopment" directory in your home directory.
  2. Open the AndroidDevelopment/adt-bundle-windows-x86-20140321/eclipse directory and launch eclipse.
That's it! The IDE is already loaded with the Android Developer Tools plugin and the SDK is ready to go. To start developing, read Building Your First App.
4. Android Eclipse Folder Structure?
Android projects are the projects that eventually get built into an .apk file that you install onto a device. They contain things such as application source code and resource files. Some are generated for you by default, while others should be created if required. The following directories and files comprise an Android project:
src/
Contains your stub Activity file, which is stored at src/your/package/namespace/ActivityName.java. All other source code files (such as .java or .aidl files) go here as well.
bin/
Output directory of the build. This is where you can find the final .apk file and other compiled resources.
jni/
Contains native code sources developed using the Android NDK. For more information, see the Android NDK documentation.
gen/
Contains the Java files generated by ADT, such as your R.java file and interfaces created from AIDL files.
assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
res/
Contains application resources, such as drawable files, layout files, and string values. See Application Resources for more information.
anim/
For XML files that are compiled into animation objects. See the Animation resource type.
color/
For XML files that describe colors. See the Color Values resource type.
drawable/
For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or focused). See the Drawable resource type.
layout/
XML files that are compiled into screen layouts (or part of a screen). See the Layout resource type.
menu/
For XML files that define application menus. See the Menus resource type.
raw/
For arbitrary raw asset files. Saving asset files here instead of in the assets/ directory only differs in the way that you access them. These files are processed by aapt and must be referenced from the application using a resource identifier in the R class. For example, this is a good place for media, such as MP3 or Ogg files.
values/
For XML files that are compiled into many kinds of resource. Unlike other resources in the res/ directory, resources written to XML files in this folder are not referenced by the file name. Instead, the XML element type controls how the resources is defined within them are placed into the R class.
xml/
For miscellaneous XML files that configure application components. For example, an XML file that defines aPreferenceScreenAppWidgetProviderInfo, or Searchability Metadata. See Application Resources for more information about configuring these application components.
libs/
Contains private libraries.
AndroidManifest.xml
The control file that describes the nature of the application and each of its components. For instance, it describes: certain qualities about the activities, services, intent receivers, and content providers; what permissions are requested; what external libraries are needed; what device features are required, what API Levels are supported or required; and others. See the AndroidManifest.xml documentation for more information
project.properties
This file contains project settings, such as the build target. This file is integral to the project, so maintain it in a source revision control system. To edit project properties in Eclipse, right-click the project folder and selectProperties.
local.properties
Customizable computer-specific properties for the build system. If you use Ant to build the project, this contains the path to the SDK installation. Because the content of the file is specific to the local installation of the SDK, thelocal.properties should not be maintained in a source revision control system. If you use Eclipse, this file is not used.
ant.properties
Customizable properties for the build system. You can edit this file to override default build settings used by Ant and also provide the location of your keystore and key alias so that the build tools can sign your application when building in release mode. This file is integral to the project, so maintain it in a source revision control system. If you use Eclipse, this file is not used.
build.xml
The Ant build file for your project. This is only applicable for projects that you build with Ant.

5. Creating  A New Application On Eclipse?
  1. Click New  in the toolbar.
  2. In the window that appears, open the Android folder, select Android Application Project, and click Next.
  3. Figure 1. The New Android App Project wizard in Eclipse.
  4. Fill in the form that appears:
    • Application Name is the app name that appears to users. For this project, use "My First App."
    • Project Name is the name of your project directory and the name visible in Eclipse.
    • Package Name is the package namespace for your app (following the same rules as packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. For this reason, it's generally best if you use a name that begins with the reverse domain name of your organization or publisher entity. For this project, you can use something like "com.example.myfirstapp." However, you cannot publish your app on Google Play using the "com.example" namespace.
    • Minimum Required SDK is the lowest version of Android that your app supports, indicated using the API level. To support as many devices as possible, you should set this to the lowest version available that allows your app to provide its core feature set. If any feature of your app is possible only on newer versions of Android and it's not critical to the app's core feature set, you can enable the feature only when running on the versions that support it (as discussed in Supporting Different Platform Versions). Leave this set to the default value for this project.
    • Target SDK indicates the highest version of Android (also using the API level) with which you have tested with your application.
      As new versions of Android become available, you should test your app on the new version and update this value to match the latest API level in order to take advantage of new platform features.
    • Compile With is the platform version against which you will compile your app. By default, this is set to the latest version of Android available in your SDK. (It should be Android 4.1 or greater; if you don't have such a version available, you must install one using the SDK Manager). You can still build your app to support older versions, but setting the build target to the latest version allows you to enable new features and optimize your app for a great user experience on the latest devices.
    • Theme specifies the Android UI style to apply for your app. You can leave this alone.
    Click Next.
  5. On the next screen to configure the project, leave the default selections and click Next.
  6. The next screen can help you create a launcher icon for your app.
    You can customize an icon in several ways and the tool generates an icon for all screen densities. Before you publish your app, you should be sure your icon meets the specifications defined in the Iconography design guide.
    Click Next.
  7. Now you can select an activity template from which to begin building your app.
    For this project, select BlankActivity and click Next.
  8. Leave all the details for the activity in their default state and click Finish.
6. How  to Run The  Android Application?
1.Create Android Virtual Device
          Define a new Android Virtual Device (AVD) by opening the AVD Manager via Window → Android Virtual Device Manager and by pressing the New button.
  1. In the Android Virtual Device Manager panel, click New.
  2. Fill in the details for the AVD. Give it a name, a platform target, an SD card size, and a skin (HVGA is default).
  3. Click Create AVD.
  4. Select the new AVD from the Android Virtual Device Manager and click Start.
  5. After the emulator boots up, unlock the emulator screen.
To run the app from Eclipse:
  1. Open one of your project's files and click Run  from the toolbar.
  2. In the Run as window that appears, select Android Application and click OK.
7. Android Basic Components

                 7.1. Activity
                 7.2. BroadCast Reciever
                 7.3. Service
                 7.4. Content Provider

7.1.Activity

An activity is the visual representation of an Android application. An Android application can have several activities.
Activities use views and fragments to create the user interface and to interact with the user. Both elements are described in the next sections.

7.2. BroadcastReceiver

broadcast receiver (receiver) can be registered to listen to system messages and intents. A receiver gets notified by the Android system if the specified event occurs.
For example, you can register a receiver for the event that the Android system finished the boot process. Or you can register for the event that the state of the phone changes, e.g., someone is calling.

7.3. Service

service performs tasks without providing an user interface. They can communicate with other Android components, for example, via broadcast receivers and notify the user via the notification framework in Android.

7.4. ContentProvider

content provider (provider) defines a structured interface to application data. A provider can be used for accessing data within one application, but can also be used to share data with other applications.
Android contains an SQLite database which is frequently used in conjunction with a content provider. The SQLite database would store the data, which would be accessed via the provider.

Monday 20 January 2014

ANDROID SIMPLE FORM VALIDATION USING EDITTEXT

In this post i am going to cover how to validate edittext in android. Many android beginners struggling to understand validate the form. The following example has one edittext and two textview. Edittext value set for textview.

Layout Form Design File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#99ccff">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Enter your name:"
    android:textSize="25sp"
    android:textColor="#660000"/>

 <EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="number"
android:hint="Enter Number"
android:id="@+id/enterdata"/>

<Button
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"
 android:layout_width="200sp"
 android:onClick="check"
 android:textSize="20sp"
 android:id="@+id/submit"/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#660000"
android:text="Entered name is : "
android:id="@+id/display"/>
</LinearLayout>



In above edittext accept only numbers by using attribute inputtype="number".

Classfile MainActivity.java

package com.example.zformm;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 EditText enternumber, ee;
 Button submit;
 TextView display;  
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        enternumber = (EditText)findViewById(R.id.enterdata);
        display = (TextView)findViewById(R.id.display);
        submit = (Button)findViewById(R.id.submit);
    }  
  
    public void check(View v){
       
        //check if edittext value is empty
         if (enternumber.getText().length() == 0) {
             Toast.makeText(this, "Enter the value", Toast.LENGTH_LONG).show();
         } else {
            //check if edittext value is below 10
             Double number = Double.parseDouble(enternumber.getText().toString());
             if(number > 10 || number == 0){
                 Toast.makeText(this, "Enter the number below 10", Toast.LENGTH_LONG).show();
             } else {
                 //Finally set edittext value to textview
                 display.setText("Entered Value is : "+number);
             }
         }

    }
}


In above example If editext value is empty or above 10 then display toast error

I hope this example would help you. Thanks for read this example.






Friday 17 January 2014

Android Simple Form Design Example

Step 0: Creating a Simple Android Project

Begin by creating a new Android project. You can also follow along using the source code provided as a supplement to this tutorial.

Step 1: Designing the Form

First, you need to give some thought to want kind of data you want to collect from the user. The form may have any number of fields. Consider the types of data you want to collect and choose the appropriate type of control. For example:
  • To collect text input, use EditText controls
  • To limit the user to a fixed set of responses, use Spinner controls, similar to a drop-down menu
  • To collect boolean (yes/no) input, use CheckBox controls
  • To allow the user to trigger events, use Button controls
For this tutorial, you will be designing a feedback form. This form collects five pieces of data from the user:
  • The user’s name (a string)
  • The user’s email (a string)
  • The type of feedback (options: Praise, Gripe, Suggestion or Bug)
  • The feedback message (a string)
  • Whether or not the user wants an email response (a boolean)

Step 2: Creating the Layout Resource

Begin by creating a layout resource for the form screen. The form will have a bunch of fields, which could span more than a single screen (depending on the device screen size), so you should consider wrapping the entire form within a ScrollView control to enable scrollbars.
The ScrollView control must have exactly one child view, so consider which layout control is most appropriate for the form you want to create. Forms are often contained within a vertically oriented LinearLayout control, so that the form fields cascade down the page vertically, one after another. This also helps the user’s focus move from field to field naturally.
A simple form layout resource might look like this:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/ScrollView01"  
  5.     android:layout_width="wrap_content"  
  6.     android:layout_height="wrap_content"  
  7.     android:scrollbars="vertical">  
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:orientation="vertical"  
  11.         android:layout_height="fill_parent">  
  12. <!--Put form controls here-->  
  13.     </LinearLayout>  
  14. </ScrollView>  

Step 3: Add a TextView Control (Form Description)

Next, you need to add a TextView control within the LinearLayout control. The TextView control called TextViewTitle displays the form description and purpose to the user. This control displays a string resource called @string/feedbacktitle, which must be defined within the /res/values/strings.xml string resource file.
Here is the XML to add to your form layout resource file:
  1. <TextView  
  2.     android:id="@+id/TextViewTitle"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:text="@string/feedbacktitle"  
  6.     android:textSize="10pt">  
  7. </TextView>  

Step 4: Add an EditText Control (Name)

Now you need to add your first EditText control just below the TextView control you just created. This EditText control called EditTextName acts as a form field for the user’s name. You can use the hint attribute to supply a string to display in the EditText control when it’s empty (e.g. “Type your name here…”). You can also set the inputType attribute of the EditText control to apply name entering logic.
Here is the XML to add to your form layout resource file:
  1. <EditText  
  2.     android:id="@+id/EditTextName"  
  3.     android:layout_height="wrap_content"  
  4.     android:hint="@string/feedbackname"  
  5.     android:inputType="textPersonName"  
  6.     android:layout_width="fill_parent">  
  7. </EditText>  

Step 5: Add another EditText Control (Email)

Next, you need to add your second EditText control just below the EditText control called EditTextName. This EditText control called EditTextEmail acts as a form field for the user’s email address. Again, set the hint attribute to supply a string to display in the EditText control when it’s empty. This time, set the inputType attribute of the EditText control to textEmailAddress, which will make entering emails easier on the user.
Here is the XML to add to your form layout resource file:
  1. <EditText  
  2.     android:id="@+id/EditTextEmail"  
  3.     android:layout_height="wrap_content"  
  4.     android:hint="@string/feedbackemail"  
  5.     android:inputType="textEmailAddress"  
  6.     android:layout_width="fill_parent">  
  7. </EditText>  

Step 6: Add a Spinner Control (Feedback Type)

Next, you need to add a Spinner control just below the EditText control you just created. This Spinner control called SpinnerFeedbackType allows the user to select the type of feedback from a fixed list of options (Praise, Gripe, Suggestion, or Bug).
Android SDK: Creating Forms
First, you need to define these choices as individual string resources in the strings.xml resource file.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <!--Other string resources also defined in this file… -->  
  4.     <string name="feedbacktype1">Praise</string>  
  5.     <string name="feedbacktype2">Gripe</string>  
  6.     <string name="feedbacktype3">Suggestion</string>  
  7.     <string name="feedbacktype4">Bug</string>  
  8. </resources>  
Next, create a string array resource using the individual string resources as follows in /res/values/arrays.xml:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="feedbacktypelist">  
  4.         <item>@string/feedbacktype1</item>  
  5.         <item>@string/feedbacktype2</item>  
  6.         <item>@string/feedbacktype3</item>  
  7.         <item>@string/feedbacktype4</item>  
  8.     </string-array>  
  9. </resources>  
Now you are ready to configure the Spinner control in your form layout. Begin by supplying the prompt attribute, which will provide a helpful string at the top of the Spinner control. Next, specify the list of string choices using the entries attribute—specifically, set the entries attribute to the string array you just defined: @array/feedbacktypelist.
Here is the XML to add to your form layout resource file:
  1. <Spinner  
  2.     android:id="@+id/SpinnerFeedbackType"  
  3.     android:layout_height="wrap_content"  
  4.     android:prompt="@string/feedbacktype"  
  5.     android:layout_width="fill_parent"  
  6.     android:entries="@array/feedbacktypelist">  
  7. </Spinner>  

Step 7: Add a Multi-Line EditText Control (Feedback)

Next, you need to add one more EditText control just below the Spinner control. This EditText control called EditTextFeedbackBody acts as a form field for the feedback text. Again, set the hint attribute to supply a string to display in the EditText control when it’s empty. This time you want to give the user ample space to write praise, gripes, suggestions, or describe bugs in the application. Therefore, you may want to set the inputType attribute of the EditText control to textMultiLine and specify the number of lines to draw using the lines attribute.
Here is the XML to add to your form layout resource file:
  1. <EditText  
  2.     android:id="@+id/EditTextFeedbackBody"  
  3.     android:layout_height="wrap_content"  
  4.     android:hint="@string/feedbackbody"  
  5.     android:inputType="textMultiLine"  
  6.     android:lines="5"  
  7.     android:layout_width="fill_parent">  
  8. </EditText>  

Step 8: Add a CheckBox Control

Next, you need to add a CheckBox control just below the EditText control you just created. This CheckBox control called CheckBoxResponse allows the user to choose whether or not they want to request an email response from the app developer. You can use the text attribute to supply a string to display next to the CheckBox control.
Here is the XML to add to your form layout resource file:
  1. <CheckBox  
  2.     android:id="@+id/CheckBoxResponse"  
  3.     android:layout_height="wrap_content"  
  4.     android:text="@string/feedbackresponse"  
  5.     android:layout_width="fill_parent">  
  6. </CheckBox>  

Step 9: Add a Button Control

Finally, you are ready to finish off the form with a Button control. If you want to have a button with text on it, use the Button control; if you prefer a button with a picture on it, use an ImageButton control instead. We will use a Button control here. First, set the text on the Button control using the text attribute. Next, you can easily register a click handler (as opposed to registering it programmatically in your Activity) for your Button control using the onClick attribute.
Here is the XML to add to your form layout resource file:
  1. <Button  
  2.     android:id="@+id/ButtonSendFeedback"  
  3.     android:layout_height="wrap_content"  
  4.     android:text="@string/feedbackbutton"  
  5.     android:onClick="sendFeedback"  
  6.     android:layout_width="fill_parent">  
  7. </Button>  
Excellent! You’ve finished designing your form. Now, all you need to do is implement the sendFeedback() method in your Activity.
Android SDK: Creating Forms

Step 10: Implement a Button click handler

In the Button control, you specified the onClick attribute as sendFeedback. Now you will need to implement a method called sendFeedback() within your Activity class. For example:
  1. public void sendFeedback(View button) {  
  2.     // Do click handling here  
  3. }  

Step 11: Reading Input from EditText Controls

Now that your form is designed and the controls have been implemented, you next need to collect the form data from the individual fields when the Button control is clicked.
For an EditText control, you use the getText() method.
  1. final EditText nameField = (EditText) findViewById(R.id.EditTextName);  
  2. String name = nameField.getText().toString();  
  3. final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);  
  4. String email = emailField.getText().toString();  
  5. final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);  
  6. String feedback = feedbackField.getText().toString();  

Step 12: Reading Input From Spinner Controls

Your form included a Spinner control. You use the getSelectedItem() method to read the data from this form control.
  1. final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);  
  2. String feedbackType = feedbackSpinner.getSelectedItem().toString();  
In this case, the selected item in the Spinner control is the String chosen by the user of the selected item.

Step 13: Reading Input from CheckBox Controls

Finally, your form included a CheckBox control. In this case, the result is just a flag to tell your application if the box was checked or not.
  1. final CheckBox responseCheckbox = (CheckBox) findViewById(R.id.CheckBoxResponse);  
  2. boolean bRequiresResponse = responseCheckbox.isChecked();  
You can use this Boolean value however you want in your app.

Step 14: Generate the Appropriate Email Details

Now that you’ve got all your form data, you’re ready to craft a message. Simply process all the data fields and build an appropriate feedback message. For example, you might use some fields in the message subject, and others in the message body. You can use format strings to help build the appropriate strings, the specifics of which will be discussed in an upcoming quick tip.
Android SDK: Creating Forms

Conclusion

In this tutorial, you learned how to use various types of input controls to design a feedback form within an Android application. The EditText control is versatile and powerful, allowing for many different types of text and freeform input. The Spinner and Checkbox controls help limit the user’s input to a specific set of responses. The Button control is a simple way to generate an event to process the form input.
There are many other controls worth exploring for use within forms. There is a lot more we could cover regarding good form design, how form controls fit into the Activity lifecycle, and how input methods and such factor into things, but for now, focus on gaining a good handle on the basics of form controls and how to use them.
We hope you enjoyed this tutorial and look forward to your feedback!