Friday, January 30, 2009

Fiddling with widgets

How to create a simple android application using widgets?
In this post, I'll give you the code for creating a simple Welcome Application using widgets. After you create a project ( File - New -Project-Android Project), lets say Welcome, you'll get a file called Welcome.java. Replace the following code with the existing code:

package com.android.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
import android.content.Intent;


public class Hello extends Activity {

/** Called when the activity is first created. */
TextView a,b,c;
Button d;
EditText e;
String sname;
@Override
public void onCreate(Bundle savedInstanceState) {
a=(TextView)findViewById(R.id.Name);
b=(TextView)findViewById(R.id.wel);
c=(TextView)findViewById(R.id.msg);
d=(Button)findViewById(R.id.sub);
e=(EditText)findViewById(R.id.nm);
e.setText(" ");
d.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sname=e.getText().toString();
c.setText(sname);

}
});

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}


Main.xml


android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
android:id="@+id/sub"
android:layout_width="127px"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_x="91px"
android:layout_y="116px"
>

android:id="@+id/Name"
android:layout_width="121px"
android:layout_height="21px"
android:text="Enter your name"
android:layout_x="23px"
android:layout_y="46px"
>

android:id="@+id/wel"
android:layout_width="86px"
android:layout_height="28px"
android:text="Welcome"
android:layout_x="37px"
android:layout_y="223px"
>

android:id="@+id/nm"
android:layout_width="148px"
android:layout_height="31px"
android:textSize="18sp"
android:layout_x="144px"
android:layout_y="42px"
>

android:id="@+id/msg"
android:layout_width="133px"
android:layout_height="22px"
android:layout_x="114px"
android:layout_y="224px"
>



Welcome Application:
Well, this is a simple application. All it does is that it inputs your name and when you hit the submit button, it displays Welcome on the same screen. This is just a small example to demonstrate the working of widgets.


Code Explanation:

In the xml code for a widget, for eg android:id="@+id/something" , the text after slash("/") gives the name of your widget. We will use this reference in Welcome.java.

Welcome.java

The first step is to create an object for each widget.

We now need to initialize each of these objects with the widget name given in main.xml. This is done by findViewById(R.id.something). You must be wondering where this R came from. Well, check out the file R.java, you will see the class name as R. This file is auto generated with some kind of values like 0x7f020000 (hexa values), do not make changes in this file.

We now need to set a listener whcih will respond when the submit button is clicked. This is done by onClickListener(), I have taken the text from the text field and set it on the textview. Thats it, quite simple, isn't it!!!

Now jus run it as Android application in Run Menu.. Wait for a couple of minutes since the emulator takes a lot of time when it runs for the first time.....

Sunday, January 25, 2009

Android-DroidDraw


A word about widgets:
Well, all your text boxes, radio buttons, check boxes etc are called widgets. Believe me, you don't need to know anything else :) ...

Designing a Layout: DroidDraw
Some of you may think that you need to know xml to design a layout of your application. Theres a good news for you, tools like DroidDraw let you design layouts without any knowledge about xml.
Heres how:
1. Go to http://www.droiddraw.org/ and download DroidDraw for free.
2. Now open it, and choose the layout(I prefer absolte layout).
3. Click on the Widgets tab on the right side and drag the widget to be inserted.
4. Click on the properties tab and type a meaningful name for the widget.
5. In the Id field, type another name.This is the name that you will choose for reference in your program.
6. Click on Generate, this generates the xml code for the layout.

Where do I use the xml code generated?
You need to replace this generated code with your main.xml (in Projectname/res/layout).


Friday, January 23, 2009

Configuring Eclipse for Android Project

Configuring Eclipse for Andorid Project
After updating the eclipse with the steps in the previous post, you need to configure eclipse with android sdk path. Here are the steps:
  • Go to the android Windows > Preferences menu. Eclipse Preference window will be opened.
  • Select Android from the left hand size menu.
  • Click on the Browse button and select the location of the SDK.
  • Click Apply and then Ok.

Saturday, January 10, 2009

Android Installation

What is Android ?
Android is a software stack for mobile devices that includes an operating system, middleware and key applications.Using Android, you can create applications,like you can do using J2ME.

Installing Android on Windows XP
Its very easy to install android after you read this tutorial.Just follow the steps:
1. Go to http://code.google.com/android/ and download the Android SDK.This contains Android emulator, documentation and sample codes.
2. Go to http://www.eclipse.org/downloads/ and download Eclipse IDE using which you will write your applications.
3. You now need to download ADT, which is a plugin.It needs to be configured with Eclipse so that you can create applications.
4. Open your Eclipse IDE, Click on Help-Software Updates-Available Sofwares (tab)-Add Site.
5. On the location box write, https://dl-ssl.google.com/android/eclipse/ and click OK.
6. You will now get a check box, expand it and check all the items.Click on the Install button.
7. After this, click on a couple of next and finish and wait until it gets downloaded form internet.
8. Right click on My Computer-Properties-Advanced(tab)-Environment Variables-Edit the variable Path and give the location of the tools folder in the unzipped Android SDK folder.
9. Restart your computer.
10. Open Eclipse, Click on File-New-Android Project..

Congratulations, you have now successfully installed Android..