Design Analog and Digital Clock Using Android Studio
Implement Analog and Digital Clock Using Android Studio
1.In this Tutorial,By Hitting the Button We are going to swap the clock type...
Step 1: Open Android Studio...
Step 2:Create New Project...
Step 3:
a.Drag Analog Clock and Digital Clock from date and time as shown below...
b.Drag a button....Provide appropriate id,name and OnClick property for the button by accessing properties...
Step 4:
Now Lets Work with MainActivity.java file.....
Refer the following code..
Step 5:
Now Lets Run the App and Test on Emulator...
1.In this Tutorial,By Hitting the Button We are going to swap the clock type...
Step 1: Open Android Studio...
Step 2:Create New Project...
Step 3:
a.Drag Analog Clock and Digital Clock from date and time as shown below...
Step 4:
Now Lets Work with MainActivity.java file.....
Refer the following code..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.design.widget.Snackbar; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.text.style.TtsSpan; | |
import android.view.View; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.AnalogClock; | |
import android.widget.Button; | |
import android.widget.DigitalClock; | |
public class MainActivity extends AppCompatActivity | |
{ | |
//create 3 variables | |
private static Button btn; //button for submitting | |
private static DigitalClock digital; //digital clock variable | |
private static AnalogClock analog; ////analog clock variable | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
OnButtonClick(); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show(); | |
} | |
}); | |
} | |
public void OnButtonClick() //name of button OnClick property | |
{ | |
btn = (Button)findViewById(R.id.swap); | |
digital = (DigitalClock)findViewById(R.id.digitalClock); | |
analog = (AnalogClock)findViewById(R.id.analogClock); | |
//add button listener | |
btn.setOnClickListener( | |
new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if(digital.getVisibility() == DigitalClock.GONE) //if digital clock hides view analog clock | |
{ | |
digital.setVisibility(DigitalClock.VISIBLE); | |
analog.setVisibility(AnalogClock.GONE); | |
} | |
else // else if analog clock hides view digital clock | |
{ | |
analog.setVisibility(AnalogClock.VISIBLE); | |
digital.setVisibility(DigitalClock.GONE); | |
} | |
} | |
} | |
); | |
} |
Step 5:
Now Lets Run the App and Test on Emulator...
Its Working...!!


If You are New or Don't Know How To Run App On Emulator Go to My Following Link...
Run App On Emulator..




If You are New or Don't Know How To Run App On Emulator Go to My Following Link...
Run App On Emulator..
Thank you...!!!
Hi! I am android learning beginner and unable to understand that how condition is implemented. Can you explain the below code.
ReplyDeleteif(digital.getVisibility() == DigitalClock.GONE)
Thankyou for this wondrous post, I am glad I observed this website on yahoo. watches
ReplyDelete