Sunday, December 18, 2011


Android Tutorial Part1 - Introduction to android programming

Hi,

The main idea behind this blog is to help you to start coding in android. I think you may have already selected the IDE for android development and added the android sdk and ADT plugin. For those who hadn't installed the above items, please check the android developer site. This blog is mainly based on the latest ADT Plugin.(ADT-16)

Hopes now you have succesfully installed the required packages. If you have any queries in installing please post your comments.

ANDROID

You may be familiar with the term 'android', as it's one of the most popular mobile phone OS. The android platform is using the Java platform. But this doesn't means you have to learn java before starting to learn android.

I don't have a plan to go to the theories, architecture etc. You can learn those from the developer site. Here I will be introducing some of the key features of android and will explain how to implements those.

Let's set go
Now everything to start developing is ready. I had selected eclipse as the IDE for developing android. You can select any other IDE's. When you start eclipse you have to set a workplace location. All the projects you start from eclipse will be saved in that location. After setting the workplace when you open the eclipse it will be like the following pic.




To start a new android project click the 'File' on the top  left and select 'New'. If 'Android Project' is in that drop down list then select that otherwise select 'Other' on the bottom of that drop down and select 'Android Project' under the folder 'Android'. Now you will be seeing a new window like the following pic.




  • Enter a project name as you like and make sure that 'Create new project in workspace' is selected. Here I'm going to name the project as "Sample1". Now click 'Next'.
  • You will be asked to select an SDK target. You can select any target, here I'm selecting Android 2.2.  Then click 'Next'.
  • Now, you will be asked to enter the 'Application Info'. Some fields will be prefilled with values. You can edit it and change the preferences as you wish later. For now change only the package name. You can set any package name for your project. The only condition is an emulator/device will run only one app with a package name. Here I'm giving the package name 'com.android.sample1'.  Now click 'Finish'.
Run the app in an android emulator

You can simply run this project in an android emulator/device. Here we are going to run this on an android emulator. An emulator is a virtual android device that runs on your computer. You can test the apps you develop on this emulator even if you don't have a device.

Creating the virtual device.
Before running the app you have to create an AVD (Android Virtual Device).  For this follow the steps
:
  1.  Select Window>AVD Manager. The 'Android Virtual Device Manager' window will be presented now.
  2. Click 'New'. I'm going to create an AVD with OS 2.2 and resolution HVGA. Because we selected the application target ad Android 2.2 earlier.
  3. Enter a name for you AVD. You can enter any name as you like. The only condition is it should atleast reflect the OS version. Will explain that after creating the AVD. I entered the name Android2.2HVGA.
  4. Select the target. I selected Android 2.2 to run our Sample app.
  5. Enter the size of the SD card. You can enter any value there. Here I'm going to enter 1024.
  6. Click 'Create AVD'.
You AVD will be created now. You can edit these info at any time by selecting the avd in the 'Android Virtual Device Manage' and tapping 'Edit'. For all your apps with target 2.2 you can use this avd.

Now select the AVD in the Android Virtual Device Manage and click 'Start'. In the new window that appears click 'Launch'. Your AVD will be as follows:

It will take some time to start the AVD completely. Let it be there and come back to eclipse!


Your project will be having the a  folder structure as in the following figure.


In android an activity represents a single screen with a user interface. So for an activity there will be a class file and a layout file. The class file will be in the src>your package name folder (src>com.android.sample1 for me) and the layout file in the res>layout folder.

 To run the app select the project name on the top of this hierarchial tree and select Run>Run As>Android Application. The following figure will help you.


 
Take a look at the emulator now. It will be like


"Hello Word, Sample1Activity"!. When ever a new project is created in android it will automaticall displays the hello world text with the base activity and app name. Now get ready to play with it!

Open the main.xml file inside the res>layout. It will be having two type of views 'Graphical Layout' and 'main.xml'


You can add the code in the main.xml and view the changes in the graphical layout. Go to the main.xml. There will be a TextView inside a LinearLayout.

In android a TextView is used to display a text/data in the screen. Inside the TextView tag there will a text set using android:text="@string/hello"(I will mention about @string later). Replace the @string/hello with whatever text you like and run the app.  Please give a little time for the emulator to build and run the app.You can always check the status of your app in the console.


When it says Activity Manager: Starting ... check the emulator. What did you see in the emulator now? You can see the text you entered in the screen!

Now come back to the main.xml. Inside the TextView tag there are other options like android:layout_width and android:layout_height. They specify the width and height of your textview. The width will be prefilled with 'fill_parent' and height with 'wrap_content'.

If width of an element in the layout is set to fill parent then it means that element will stretch to the full width of its parent. Here the LinearLayout is the parent of the text view. So the width of text view set to fill parent means it covers the entire width of the linear layout.

The height of the text view is set to wrap content. If the width of an element is set to wrap content then it means the height of that element will adjust according to the height of it's contents.

Now let's play with the height and width. Try setting both as wrap content, fill parent, in any way you like. Also try to change the text. Don't forget to replace these terms with values you like. If you wants to set height to 50, then set it as android:layout_height="50dp".

dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion.
Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent.


How was that research with height and width?  Thinks it will be interesting. Now take a look at that linear layout. The height and width of is set to fill parent. What does that mean? I think you may now know that this means the linear layout will fill the entire width and height of the screen as its height and width are set to fill parent.

Now what about placing another text view below the textview? We are going to place a text view with text size 50 saying "ANDROID IS INTERESTING :)". Just give a try first and then check my code.

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
  
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ANDROID IS INTERESTING :)"
        android:textSize="50sp"/>

</LinearLayout>


I had mentioned the text size in sp.
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

You have to use sp for font sizes and dp for everything else.


Will post how to use Edit Text in my next post. Please post your comments and suggestions.

No comments:

Post a Comment