Monday, 10 September 2012

DAY 4 -''Hello Android" App - my first android app :)


About this App : This is a very simple app which has been given in  http://developer.android.com. I thought this would be the best one for a start.We will have a text box and button in this app. If we write 'Hello Android' in the text box, and click the button, it will get displayed in another window.


Step 1:Creating a new project
  • Open Eclipse
  • Go -File -New- Project
  • Expand the folder named 'Android'
  • And select  'Android Application Project'
Application name:App name that appears to the users.
Project Name: Name of project directory and that appears in Eclipse.
Package Name: Name of the app namespace. This should be unique among all the apps installed in the android directory. So usually the reverse order of the publisher or company is used as the suffix.
Build SDK : Platform version against which you will compile your app.By default it is set to the latest version.
Minimum Required SDK : Lowest version of Android that your App supports.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.
  • Click Next



  • This window is to customize the icon as we wish. Click Next after setting the required icon for the app.
  • Now we are asked to select an activity template for our app.Select 'Blank Activity' and click Next.
  • Leave all the activity details to default and click 'Finish'.
Step 2: Getting ready with the lay out



Did you get that?? Yes???Awesome..We are good to go :)
 Have a look at the left side package explorer window.You could see many folders created under the name of your app. I have no clue what all those are. We may get a better idea when we learn more.
I decided not to break my head for the time being and just to proceed with the next step of  the tutorial.

Now double click the activity_main.xml tab.







In this page we are gonna design the look of our application. As explained earlier we have to put a text box and a button in the first screen .

Delete the code already present and paste the code given below .

<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="horizontal"
    tools:context=".DisplayMessageActivity" >
  
 <EditText android:id="@+id/edit_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_message" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendMessage"
        android:text="@string/button_send" />

</LinearLayout>



Explanation

Linear Layout:
A Layout that arranges its children in a single column or a single row.' android:orientation="horizontal' is used to
 set the direction of the row.

Edit Text :
This is a user editable text field. 
android:id=@+id/edit_message :This provides a unique identifier for the view, which you can use to reference the object from your app code.
 (@) - required when you're referring to any resource object from XML.
id - resource type
edit_message - the resource name.
 android:hint :This is a default string to display when the text field is empty

Defining a string: 
For example : android:hint="@string/edit_message"

 Here the value is taken from a string whose name is 'edit_message'. Let us see how to 
define a string with a value.

Go to res - Values- Strings.xml


Click Add button.Give name -'edit_message'and value - 'Enter a message'.  
Create one more string with name as 'button_send' and value as 'Send'.

These strings are being used in the xml  above. Without this declarations, it will throw an error when compiled.
 
So after this, we will get a screen having a text box and a button.


Step 3:  Respond to the 'Send' Button

In the activity_main.xml ' android:onClick="sendMessage" ' is used to respond to the Send button. This specifies which method to be called(sendMessage) on the click event of the button.
Form the MainActivity dropdown (as shown in the image above) select 'Open Main Activity' which will open MainActivity.java file.

Add the following code at the end.

  /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        view.setBackgroundColor(0xFF00FF00 );
        startActivity(intent);
    }
So when button is clicked
  • sendMessage function is called.
  • An intent is created which will point to the activity that has to be started on send button's click event.
  • The message from edit_message string will be retrieved.
  • That message is also added to the intent.
  • And intent is passed to the activity.An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Step 4: Creating second Activity
 create a new activity using Eclipse:
  1. Click New in the toolbar.In the window that appears, open the Android folder and select Android Activity. Click Next.Select BlankActivity and click Next.Fill in the activity details:
    • Project: MyFirstApp
    • Activity Name: DisplayMessageActivity
    • Layout Name: activity_display_message
    • Navigation Type: None
    • Hierarchial Parent: com.example.myfirstapp.MainActivity
    • Title: My Message
    Click Finish.


 Step 5: Receive intent and display the message.


  •  From layout, select activity_display_messgae.xml.
  • From Displaymessage dropdrown select 'open DisplayMessage' which will open 'DisplayMessageActivity.java file.
  •  Paste the following code there to get the message from intent and to display it on screen.
  •  package com.example.my.first.app;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TextView;
    import android.support.v4.app.NavUtils;



        public class DisplayMessageActivity extends Activity {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                // Get the message from the intent
                Intent intent = getIntent();
                String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

                // Create the text view
                TextView textView = new TextView(this);
                textView.setTextSize(40);
                textView.setText(message);

                // Set the text view as the activity layout
                setContentView(textView);
            }
        }

    Thats it..Our App is ready . We can run that now.But before that we have to set a virtual device to run it.For that follow the steps below 
 Launch the Android Virtual Device Manager:In Eclipse, click Android Virtual Device Manager from the toolbar.

  
  • Click NEW
  • Fill in the details
  • Click Create AVD
  • Select the Virtual Device created
  • Click Start 
Step 6 :Running the Application 

To run the app from Eclipse, open one of your project's files and click Run from the toolbar. Eclipse installs the app on your AVD and starts it.Enter the message and get it displayed as shown below.

So thats it..Feel so happy to see this output. Got some idea on view,Viewgroup,Textview,edittext,linearlayout,activity,Android emulator etc. All together its a good day ..Bye bye..keep learning..see you with the next activity soon.

    Friday, 7 September 2012

    DAY 3 - The four quick steps to Set Up Android environment


    Android development can be done on Mac, Windows or Linux machines.
     What all do we need?
    • Java Development Kit
    • Android SDk
    • Eclipse
    • ADT Plugin for Eclipse
    Step 1: Installing Java JDK


    Do you have the JDK installed?
     
    If not ,you will have to get it that first form here- http://www.oracle.com/technetwork/java/javase/downloads/index.html

    Installation is simple.Just click next,next..till it ends ;). After Java, it will ask for JRE installation.Proceed with that as well.After the installation of JDK, we are good to go for Android SDK.


    Step 2: Installing Android SDK

    Get the latest Android SDK from http://developer.android.com/sdk/index.html

    The downloaded is an executable that starts with an installer. The installer checks the system requirements and installs the required tools. After this, the installer will start the Android SDK Manager. What we have downloaded is just SDK tools. To develop an application we have to download at least one Android platform and latest SDK platform tools. This can be done anytime using the Android SDK Manager.

     Android 4.0.3 (or any platform that is required for you),SDK-tools, SDK-platform tools are required to be selected. System Image,Android Support and SDK Samples are also recommended.  Install the selected packages.

    Step 3: Installing Eclipse

    You can get that from here http://www.eclipse.org/downloads/. 'Eclipse Classic Version is recommended.
    It will also be a zip file.Extract it and it does not need any installation. Double click the file 'Eclipse.exe' to start the application.

    Step 4 : ADT plugin installation

    ADT is a custom plugin for Eclipse which helps user to develop,run and debug android applications.

    • Start Eclipse, then select Help > Install New Software.
     
    • A new window appears. Click 'Add' button at the upper right. 

    • In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location: https://dl-ssl.google.com/android/eclipse/


    • Click OK.
    •  In the Available Software dialog, select the checkbox next to Developer Tools and click Next.
    • In the next window, you'll see a list of the tools to be downloaded. Click Next
    • Read and accept the license agreements, then click Finish.  
    • If you get a security warning saying that the authenticity or validity of the software can't be established, click OK.
    • When the installation completes, restart Eclipse. 
    And we are done. .We are ready to develop our first android App now ..Yippie !!! ..See you tomorrow with that in the next blog.

    Thursday, 6 September 2012

    DAY 2 - Features and Architecture

    Features :
      Storage - Uses SQLite, a lightweight relational database, for data storage.
      Connectivity - Supports GSM/EDGE, IDEN, CDMA, EV-DO, UMTS, Bluetooth          
           (includes  A2DP and AVRCP), Wi-Fi, LTE, and WiMAX.
      Messaging - Supports both SMS and MMS.
      Web browser  -Based on the open source WebKit, together with Chrome’s V8 JavaScript   
           engine.
      Media support - Includes support for the following media: H.263, H.264 (in 3GP or MP4
          container), MPEG-4 SP, AMR, AMR-WB (in 3GP container), AAC, HE-AAC (in MP4 or 3GP     
          container), MP3, MIDI, Ogg Vorbis,WAV,JPEG,PNG,GIF, and BMP.
      Hardware support - Accelerometer Sensor, Camera, Digital Compass, Proximity Sensor and   
           GPS.
      Multi-touch - Supports multi-touch screens.
      Multitasking - Supports multi-tasking applications.
      Flash support - Android 2.3 supports Flash 10.1.
      Tethering  - Supports sharing of Internet connections as a wired/wireless hotspot.

      (courtesy -Beginning Android 4 Application Development by Wei-Meng-Lee)

      Architecture:


       
          (courtesy - http://elinux.org/Android_Architecture)

      Linux Kernal - This is the base Kernal.The whole android is built out of Linux 2.6 Kernal with some architectural modifications by Google.As shown in the diagram the Linux contains all the drivers to run the hardware associated.  Linux layer also handles all Android core functionality which includes Memory management, process management, networking, security settings etc. 

       Libraries - These are the set of  reusable codes which has been written already to provide the  basic features of an Android OS.This makes android application development easier and less time consuming.Eg: SQLite which is the database engine used in android and WebKit which is the web browser.


       Android RunTime - It includes   the Core libraries that enable developers to write Android apps using Java Programming language. It also includes the Dalvik Virtual machine.

      Dalvik Machine is a flavour of JVM developed by Dan Bornstein of Google.Unlike JVM , Dalvik VM  runs .dex files which are processed out of .class files at the time of compilation. This has been specially designed  for Android and optimized  for battery- powered mobile devices with limited memory and CPU.Dalvik machine enables every Android application to run in its own process, with its own instance of the Dalvik VM.

       Application Framework - These are the blocks which interacts with our applications.These programs manages the basic functions of the phone. In short, this exposes the various capabilities of Android OS for the developers so that they can  make use of them  in their applications.

       Applications - That is  the top most layer of Android. An android application is written in Java programming language. An android SDK compiles the data and resource files into an Android package with suffix .apk thus forming an application. Users interacts with the applications. Some applications that comes with the android device are Phone, Contacts, Browser etc. Any application that you write or download from android market will come in this layer.

      Thats all about the architecture. Lets set up the environment tomorrow.


      Wednesday, 5 September 2012

      DAY 1 - What is this android?


      Note : Today is my first day to the world of androids. I have no idea where to start. So what i am gonna do is just google 'Introduction to Android development'  and dump here  all bits and pieces which i find interesting and relevant.

      Lets start with the meaning of the word 'Android'.
       And i got the following results
      • An automaton that resembles a human being
      • Possessing human features.
      • a robot resembling a human being 
      Interesting..What flashes through my mind now is this tagline -'Designed for Humans' ..So what about S2? designed for Aliens? :/ And my phone is S2 ;)

      Back to android..So What is Android ?

      Android is a Linux-based mobile operation system that was originally developed by Android.inc.In 2005,The Open Handset Alliance,a group led by Google, purchased Android. The Open Handset Alliance  is a group of 47 technology and mobile companies who have come together to accelerate innovation in mobile and offer consumers a richer, less expensive, and better mobile experience. Together, they have developed Android, the first complete, open, and free mobile platform - which means anyone who wants to use Android can download the full source code and customize or modify it to suit their requirements.

      Android has evolved from version 1.5 Cupcake to Jelly bean which is the latest. Below are the different flavors of android released till now.
        Android flavors

        (courtesy - http://developer.android.com/about/dashboards/index.html )


        Android devices in the market

        • Smartphones
        • Tablets
        • E-reader Devices
        • Notebooks
        • MP4 Players
        • Internet TVs

        That was just a very high level overview. We can discuss on the architecture and features in the next blog.