Analog Clock Using Android Studio code and preview

 It is pretty easy to make analog clock using android studio. 




figure: preview after  developing the app



At first we Have to do xml design

activity_main.xml  :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="100dp"
android:orientation="vertical">

<AnalogClock
android:id="@+id/analogClockId"
android:layout_width="350dp"
android:layout_height="286dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="50dp"
android:background="#63e1ac" />

<TextClock
android:textSize="20sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textClockId"
/>
<TextView
android:layout_gravity="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ibnsina*s clock"/>


</LinearLayout>


for implement the functionality we have to implement java code 

MainActivity.java:

package com.example.androidclock;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.TextClock;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private TextClock textclock;
private AnalogClock analogClock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
analogClock=findViewById(R.id.analogClockId);
textclock=findViewById(R.id.textClockId);

analogClock.setOnClickListener(this);
textclock.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if(v.getId()==R.id.analogClockId) {
Toast.makeText(MainActivity.this, "Analog clock", Toast.LENGTH_SHORT).show();
}
if(v.getId()==R.id.textClockId){
Toast.makeText(MainActivity.this,"Text clock",Toast.LENGTH_SHORT).show();
}

}
}


But be care full guys you have to change my  package name  in my java code .

here my package name package com.example.androidclock;

Here you should not copy my project name otherwise your application may not run .Try not to change your project name.Sometimes many functions dont work for lack of import files







Post a Comment

0 Comments