Google Android Tutorial
Demo 3 - Ticker
The third demo is a 'ticker' having a textbox for entering the ticker's text, a ticker(timer) , two labels(editview) one for status and the other for diplaying the sliding message.
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <EditText android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Ticker android:id="@+id/ticker1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dip" /> <TextView android:id="@+id/label1" android:layout_width="wrap_content" android:layout_height="fill_parent" /> </Ticker> <TextView android:id="@+id/label2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dip" /> </LinearLayout>
LinearLayout with vertical orientation is like FlowLayout but in a vertical direction.

package mypack.mydemos;
import android.app.Activity;
import android.os.Handler;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import java.util.Map;
public class demo extends Activity
implements Ticker.TickerListener {
Ticker ticker1;
TextView label1, label2;
EditText text1;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
ticker1 = (Ticker) findViewById(R.id.ticker1);
label1 = (TextView) findViewById(R.id.label1);
label2 = (TextView) findViewById(R.id.label2);
text1 = (EditText) findViewById(R.id.text1);
ticker1.setTickerListener(this);
text1.setOnClickListener(new clicker());
}
class clicker implements EditText.OnClickListener {
public void onClick(View v) {
label1.setText(text1.getText());
ticker1.startTicker();
label2.setText("Ticking...");
}
}
public void onTickerFinished(Ticker view) {
label2.setText("Done!");
}
}
Demo 4 - Checkbox
Copy this to tools\demo\res\layout\
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/ apk/res/android"> <CheckBox android:id="@+id/check1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="java" android:layout_x="50px" android:layout_y="22px"> </CheckBox> <CheckBox android:id="@+id/check2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C#" android:layout_x="50px" android:layout_y="72px"> </CheckBox> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Confirm" android:layout_x="60px" android:layout_y="122px"> </Button> <EditText android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="EditText" android:textSize="18sp" android:layout_x="60px" android:layout_y="202px"> </EditText> </AbsoluteLayout>
To be copied to tools\demo\mypack\mydemos
package mypack.mydemos;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class demo extends Activity
{
Button button1;
CheckBox check1, check2;
EditText text1;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
text1 = (EditText) this.findViewById(R.id.text1);
check1 = (CheckBox) findViewById(R.id.check1);
check2 = (CheckBox) findViewById(R.id.check2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new clicker());
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
String r = "";
if (check1.isChecked())
{
r = r + "java" + "\n";
}
if (check2.isChecked())
{
r = r + "c#";
}
text1.setText(r);
}
}
}
This is just the usual Java code and needs very little explanation. The only difference is the way , the controls are defined ( through res\layout\xml file).
- Login or register to post comments
- 60769 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
NodakPaul replied on Thu, 2008/06/05 - 8:38am
Great article and tutorial. My company has been digging into Android lately, and you have a lot of helpful links and info for developers who are just starting with it. Thanks!
Musa Musa L replied on Mon, 2008/06/09 - 6:10am
Hello,
Great tutorial.. I am trying to run the example on openSuSE 10.3 with jdk6 update 6 and i keep on getting the following error message
Application error: mypack.mydemos
An error has occured in mypack.mydemos.
Unable to start activity ComponentInfo{mypack.mydemos/mypack.mydemos.demo}:java.lang.ClassCastException:Landroid/widget/TextView;.
What do you think is going on? I followed the steps exactly as you outlined..
I'd appreciate any help
Musa Musa L replied on Tue, 2008/06/10 - 2:15pm
in response to: musamusa
javaslinger replied on Mon, 2008/07/21 - 1:18pm
shanmugavel replied on Tue, 2008/09/09 - 12:51pm
pnero replied on Thu, 2008/10/23 - 4:30pm
line 5: xmlns:android="http://schemas.android.com/ apk/res
the space between .com/ apk causes errors. Just thought I'd throw it out there. Got me puzzled for a while.
Good tutorial though! Very helpful. Need one on databases though; that would be nice :). keep up the good work.
pnero replied on Thu, 2008/10/23 - 4:32pm
joby.nk replied on Mon, 2008/12/29 - 7:02am
Hello,
great tutorial, iam trying to this code in Eclipd IDE , the first two demos working fine,but the Demo-3
throws an error "Ticker.TickerListener cannot be resolved to a type" when we implements the demo class by Ticker.TickerListener . why this error comes ,is there any other library need to link or anythng want to import.Hopfully expecting reply
android_dev replied on Fri, 2009/01/16 - 3:41am
Hi Geetha,
This is Naina. I have recently started working on android. I have written 1 or 2 small applications. I went through the link, [url=http://geeth.ganesan.googlepages.com/android-tutorial]geeth.ganesan - ANDROID-TUTORIAL[/url]. I am trying to use the Spinner application written by you. I tried to run the same Spinner application on the android emulator using Eclipse IDE. But its not working. Iam getting the following exception error.
The application spin_and(process com.example.android.spin_and) has stopped unexpectedly. Please try again. Force Close.
I am not understanding where is the problem. I did not get any errors also. Please help me out.
Thanks,
Naina
rakronney replied on Tue, 2009/03/03 - 3:34pm
vhtmobile replied on Wed, 2009/04/08 - 8:45am
in response to: joby.nk
vhtmobile replied on Wed, 2009/04/08 - 8:49am
rajeshvijayanad... replied on Thu, 2009/07/02 - 6:23am
Hi,
I had creates a gallery with some images and its working fine for me.
Is there any way to put a seeker in the window for gallery.Is there any way to start a slide show(with gallery) when startActivity?
Please give me some suggestions...
Thanks And Regards,
Rajesh.V