An Android Analog and Digital Clock widgets are used to display Clock in Analog and Digital format. Since the DigitalClock package is deprecated we are going to use TextClock to display Digital Clock.
Creating Project:
Make sure you have properly setup the Android SDK, AVD for Testing the Application. Create a New project in Eclipse IDE with the package as “com.learn2crack.clock“. Create the Main Activity as “MainActivity” and the main Layout as “activity_main“.
Creating Layout:
The Main layout for our project is “activity_main” which has a Button widget to switch between AnalogClock and TextClock widgets.
activity_main.xml
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/welcome" android:layout_gravity="center" android:textSize="20sp" android:text="Android Clock" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btn" android:layout_gravity="center" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextClock android:id="@+id/clock" android:layout_gravity="center" android:textSize="40sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <AnalogClock android:id="@+id/aclock" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></LinearLayout>
Creating Activity:
First we are importing the TextClock and AnalogClock. When the button is pressed it hides one widget and enables other widget.
MainActivity.java
import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextClock;import android.widget.AnalogClock;import android.app.Activity;public class MainActivity extends Activity { TextClock clock; AnalogClock aclock; Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); clock = (TextClock)findViewById(R.id.clock); aclock = (AnalogClock)findViewById(R.id.aclock); btn = (Button)findViewById(R.id.btn); btn.setText("Show Analog Clock"); aclock.setVisibility(View.INVISIBLE); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub btn.setText("Show Digital Clock"); aclock.setVisibility(View.VISIBLE); clock.setVisibility(View.INVISIBLE); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub onCreate(null); } }); } }); }}
Creating Manifest:
Creating Manifest:No other special Permissions are required for our project.
AndroidManifest.xml
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.learn2crack.clock" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.learn2crack.clock.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>
Finally run the project in the Emulator.
Enjoy
Any questions comment here.
Enjoy
Any questions comment here.


No comments:
Post a Comment