Friday, September 30, 2011

AnalogClock and DigitalClock Views android exam

Besides the standard views that you have seen up to this point, the Android SDK also provides some interesting views that make your applications much more interesting. In the following sections, you will learn more about the following views:
  • AnalogClock
  • DigitalClock
  • WebView

AnalogClock and DigitalClock Views

The AnalogClock view displays an analog clock. Populate the main.xml file with the following element:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >  
 
    <AnalogClock android:id="@+id/clock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
 
</LinearLayout>
In the ViewsActivity.java file, ensure that the main.xml file is loaded as the UI for the activity:
package net.learn2develop.AndroidViews;
 
import android.app.Activity;
import android.os.Bundle;
 
public class ViewsActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
 
    }
}
When you press F11 now, you will see the analog clock as shown in Figure 6.

Figure 6 - The AnalogClock in action
Instead of an analog clock, you can display a digital clock instead using the DigitalClock view.
Add the DigitalClock element to main.xml as shown below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >  
 
    <AnalogClock android:id="@+id/clock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <DigitalClock android:id="@+id/clock2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />            
 
</LinearLayout>
Press F11 in Eclipse. Figure 7 shows the analog clock and the digital clock.

Figure 7 - The DigitalClock in action