Android Bundle: Activity Menu View Button Textview Mainactivity Activity Clockcounter Clockcounter
Android Bundle: Activity Menu View Button Textview Mainactivity Activity Clockcounter Clockcounter
public class MainActivity extends Activity { private ClockCounter mClockCounter = new ClockCounter(); boolean ifPressed = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Set Views final TextView currentTime = (TextView) findViewById(R.id.textView1); Button startStopButton = (Button) findViewById(R.id.button1); startStopButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //turns stopwatch on and off. ifPressed ^= true; //runs the loop try{ while(ifPressed){//this will only run if ifPressed is true Thread.sleep(1000);//makes the loop wait 1 second String time = mClockCounter.getTime(); //Set time to TextView currentTime.setText(time); } }catch(InterruptedException ex){ //idk lmao } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu);
return true; } } Activity_Main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="match_parent" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:weightSum="1" > <View android:id="@+id/view1" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.2" /> <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.6" android:text="@string/StartStop" /> <View android:id="@+id/View2" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.2" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/linearLayout1" android:orientation="vertical" android:weightSum="1" > <View android:id="@+id/view3" android:layout_width="match_parent" android:layout_height="0dp"
android:layout_weight="0.1" /> <AnalogClock android:id="@+id/analogClock1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.8" /> <DigitalClock android:id="@+id/digitalClock1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.1" android:text="DigitalClock" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/linearLayout1" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:orientation="vertical" android:weightSum="1" > <View android:id="@+id/view4" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.3" /> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="1" > <View android:id="@+id/View01" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.2" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_weight="0.60" android:text="00:00:00" android:textSize="50dp"
android:width="@dimen/TimerSize" /> <View android:id="@+id/View02" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.2" /> </LinearLayout> <View android:id="@+id/view5" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.3" /> </LinearLayout> </RelativeLayout>
Activity_Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Chronometer android:id="@+id/Chronometer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0" android:format="@string/chronometer_initial_format" android:paddingBottom="30dip" android:paddingTop="30dip" android:text="@string/txttampil" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtstart" > <requestFocus /> </Button> <Button android:id="@+id/btn2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/txtstop" > </Button> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtreset" > </Button> </LinearLayout>
Mainactivity
package com.example.stopwatch; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer;
setContentView(R.layout.activity_main);
Button button;
} }; }