Pages

Thursday, May 5, 2016

Android Analogclock And Digitalclock Example




MainActivity.java
package kantapp.androidanaloganddigitalclock;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.TextClock;

public class MainActivity extends AppCompatActivity
{
    Button button;
    AnalogClock analogClock;
    DigitalClock digitalClock;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         digitalClock=(DigitalClock)findViewById(R.id.digitalClock);

        analogClock=(AnalogClock)findViewById(R.id.analogClock);

        button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if ((analogClock.getVisibility()==analogClock.GONE))
                {
                    analogClock.setVisibility(analogClock.VISIBLE);
                    digitalClock.setVisibility(digitalClock.GONE);
                }
                else
                {
                    analogClock.setVisibility(analogClock.GONE);
                    digitalClock.setVisibility(digitalClock.VISIBLE);
                }
            }
        });
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context="kantapp.androidanaloganddigitalclock.MainActivity">

    <AnalogClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/analogClock"
        android:layout_gravity="center_horizontal" />

    <DigitalClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/digitalClock"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch Clock"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" >

        </Button>

</LinearLayout>

Download project: https://userscloud.com/del2evvp63e6

Only Source Code: https://userscloud.com/018wau81f8yk

No comments:

Post a Comment