Android Digital and Analogue clock

Create and copy the code to DigitalClockActivity class

package intent.filter;

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

public class DigitalClockActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//see pg 265,
setContentView(R.layout.digital_clock);


Button activit2btn=(Button) findViewById(R.id.button1);

activit2btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent replyIntent=new Intent();
setResult(RESULT_OK, replyIntent);
finish();
}
});
}
}

Create and copy the code to AnalogueClockActivity class



package intent.filter;

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

public class IntentExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
       //see page 266
        Button activity1btn=(Button) findViewById(R.id.button1);
     
        activity1btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//declaring intent and passing the current view context with class to act
Intent myIntent=new Intent(v.getContext(),DigitalClockActivity.class);
startActivityForResult(myIntent, 0);
}
});
     
    }
}

Copy the code below in main.xml file


<?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"
        android:textColor="#FFEEAA"
        android:textSize="22dp"
        android:paddingBottom="20dp"
        />
    <Button
        android:id="@+id/button1"
        android:layout_width="280px"
        android:layout_height="60px"
        android:textSize="18px"
        android:layout_gravity="center"
        android:text="Go to Digital Clock Activity"
        />
 
    <AnalogClock
        android:id="@+id/AnalogClock1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
         />
   </LinearLayout>

Create and copy the code below in digital_clock.xml file



<?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="You are currently in Activity 2"
        android:textColor="#AADDFF"
        android:textSize="22dp"
        android:paddingBottom="20dp"
        />
    <Button
        android:id="@+id/button1"
        android:layout_width="280px"
        android:layout_height="60px"
        android:textSize="18px"
        android:layout_gravity="center"
        android:text="Go to Analog Clock Activity"
        />
 
    <DigitalClock
        android:id="@+id/DigitalClock1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        android:textSize="32dp"
        android:textColor="#adf"
        android:typeface="monospace"
        />
    </LinearLayout>

Screen shot of the output





source file can be downloaded from below link
click here


No comments:

Post a Comment