<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayoutBinary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="35dp">
<LinearLayout
android:id="@+id/linearLayoutHours2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom">
<View
android:id="@+id/lblHour_2_2"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblHour_2_1"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutHours1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:gravity="bottom">
<View
android:id="@+id/lblHour_1_4"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblHour_1_3"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblHour_1_2"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblHour_1_1"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutMinutes2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="25dp"
android:gravity="bottom">
<View
android:id="@+id/lblMinutes_2_3"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblMinutes_2_2"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblMinutes_2_1"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutMinutes1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:gravity="bottom">
<View
android:id="@+id/lblMinutes_1_4"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblMinutes_1_3"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblMinutes_1_2"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblMinutes_1_1"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutSeconds2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="25dp"
android:gravity="bottom">
<View
android:id="@+id/lblSeconds_2_3"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblSeconds_2_2"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblSeconds_2_1"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutSeconds1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:gravity="bottom">
<View
android:id="@+id/lblSeconds_1_4"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblSeconds_1_3"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblSeconds_1_2"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF"
android:layout_marginBottom="10dp" />
<View
android:id="@+id/lblSeconds_1_1"
android:layout_height="55dp"
android:layout_width="55dp"
android:background="#66FFFFFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
package sekagra.android.binaryclock;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class BinaryClockActivity extends Activity {
private Timer _timer;
private Runnable Timer_Tick = new Runnable() {
public void run() {
Date __date = new Date();
//Set the labels to display the current time
/* ((TextView) findViewById(R.id.lblHours)).setText(String.format("%02d", __date.getHours()));
((TextView) findViewById(R.id.lblMinutes)).setText(String.format("%02d", __date.getMinutes()));
((TextView) findViewById(R.id.lblSeconds)).setText(String.format("%02d", __date.getSeconds()));
*/
//Set the views to display the current in its binary representation
SetDigit((LinearLayout) findViewById(R.id.linearLayoutHours2), __date.getHours() / 10);
SetDigit((LinearLayout) findViewById(R.id.linearLayoutHours1), __date.getHours() % 10);
SetDigit((LinearLayout) findViewById(R.id.linearLayoutMinutes2), __date.getMinutes() / 10);
SetDigit((LinearLayout) findViewById(R.id.linearLayoutMinutes1), __date.getMinutes() % 10);
SetDigit((LinearLayout) findViewById(R.id.linearLayoutSeconds2), __date.getSeconds() / 10);
SetDigit((LinearLayout) findViewById(R.id.linearLayoutSeconds1), __date.getSeconds() % 10);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set screenOrientation landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
_timer = new Timer(true);
_timer.schedule(new TimerTask() {
public void run() {
TimerMethod(); //every tick executes this method in the background thread
}
}, 100, 200);
}
//pass the job to a method on the UI
private void TimerMethod() {
this.runOnUiThread(Timer_Tick);
}
private void SetDigit(LinearLayout group, int digit) {
//Convert the digit to an boolean array
boolean[] __values = IntToBoolArray(digit, group.getChildCount());
//Iterate through all blocks an apply the correct color
for (int i = 0; i < group.getChildCount(); i++) {
//reverse, because the binary clock shows the digits from the bottom to the top
if (__values[i])
group.getChildAt(group.getChildCount() - 1 - i).setBackgroundColor(0xFFFFFFFF);
else
group.getChildAt(group.getChildCount() - 1 - i).setBackgroundColor(0x66FFFFFF);
}
}
private boolean[] IntToBoolArray(int value, int digits) {
boolean[] __binary = new boolean[digits];
for (int i = 0; value > 0 || i < digits; i++) {
__binary[i] = (value % 2) != 0;
value /= 2;
}
return __binary;
}
}