AnalogClock And DigitalClock in Android Example

person folder_openAndroidaccess_time April 28, 2015

In this tutorial we are going to learn how to use AnalogClock and DigitalClock in android. DigitalClock is depricated at 17. So use TextClock instead of DigitalClock . There are two types of clockViews.
See flowing Steps :

  1. You can use the AnalogClock and DigitalClock classes and create the views programatically.
  2. The instance of the ArrayAdapter class has been created and the two string items have been added (AnalogView and DigitalView)

1. Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stackandroid.analoganddigital"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
 

2. Activity layout file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.stackandroid.analoganddigital.MainActivity" >
    <ListView
        android:id="@+id/number_list"
        android:layout_width="200dp"
        android:layout_height="match_parent" />
    <fragment
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/number_list"
        class="com.stackandroid.analoganddigital.ClockFragment" />
</RelativeLayout>

3. Main Activity code

package com.stackandroid.analoganddigital;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity implements OnItemClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ListView list = (ListView) findViewById(R.id.number_list);
		ArrayAdapter<String> numbers = new ArrayAdapter<String>(
				getApplicationContext(), android.R.layout.simple_list_item_1,
				new String[] { "Analog View", "Digital View" });
		list.setAdapter(numbers);
		list.setOnItemClickListener(this);
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		StackFragment(position + 1);
	}
	private void StackFragment(int index) {
		// TODO Auto-generated method stub
		Fragment fragment = new ClockFragment(index);
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.replace(R.id.fragment, fragment);
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		ft.addToBackStack(null);
		ft.commit();
	}
}

Here we can see the Clockfragment.java. This is used to give the view depends on our desire.

package com.stackandroid.analoganddigital;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AnalogClock;
import android.widget.RelativeLayout;
import android.widget.TextClock;
public class ClockFragment extends Fragment {
	private int index;
	public ClockFragment() {
	}
	public ClockFragment(int index) {
		this.index = index;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Context context = getActivity().getApplicationContext();
		RelativeLayout relativelayout = new RelativeLayout(context);
		if (index == 1) {
			AnalogClock analog = new AnalogClock(context);
			relativelayout.addView(analog);
		} else if (index == 2) {
			// Digital clock is deprecated at 17. So use TEXTCLOCK
			TextClock digital = new TextClock(context);
			digital.setTextSize((float) 20d);
			relativelayout.addView(digital);
		}
		return relativelayout;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (null != savedInstanceState) {
			index = savedInstanceState.getInt("index");
		}
	}
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putInt("index", index);
	}
}

4. Demonstration

Analog_and_DigitalView
Analog_and_DigitalView

This is initial screen when the activity created. click on the things . It will show the respective Views of clock

AnalogView
AnalogView
DigitalView
DigitalView

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">