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.....

1 comment:

  1. Hey, it worked for me... Wonderful to c my first android appln running....keep it up

    ReplyDelete