jasonostrander / androiduifundamentals Goto Github PK
View Code? Open in Web Editor NEWCode repo for Android UI Fundamentals.
Home Page: http://www.peachpit.com/androiduifundamentals
Code repo for Android UI Fundamentals.
Home Page: http://www.peachpit.com/androiduifundamentals
<TextView
android:id="@+id/counter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="@string/default_value"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp" >
</TextView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/start_stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/start" >
</Button>
<Button
android:id="@+id/reset"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/finish" >
</Button>
</LinearLayout>
<ListView
android:id="@+id/time_list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<!-- Preview: listitem=@layout/time_row -->
</ListView>
package com.jaceace.android.timetracker;
import java.lang.ref.WeakReference;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class TimeTrackerActivity extends Activity implements OnClickListener {
private TimeListAdapter mTimeListAdapter = null;
private static long mStart = 0;
private static long mTime = 0;
private final MyHandler mHandler = new MyHandler(this);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the Timer
TextView counter = (TextView) findViewById(R.id.counter);
counter.setText(DateUtils.formatElapsedTime(0));
Button startButton = (Button) findViewById(R.id.start_stop);
startButton.setOnClickListener(this);
Button stopButton = (Button) findViewById(R.id.reset);
stopButton.setOnClickListener(this);
if (mTimeListAdapter == null)
mTimeListAdapter = new TimeListAdapter(this, 0);
ListView list = (ListView) findViewById(R.id.time_list);
list.setAdapter(mTimeListAdapter);
if (Util.useStrictMode(this)) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDialog()
.build());
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDialog()
.build());
}
}
@Override
protected void onDestroy() {
mHandler.removeMessages(0);
super.onDestroy();
}
@Override
public void onClick(View v) {
TextView ssButton = (TextView) findViewById(R.id.start_stop);
if (v.getId() == R.id.start_stop) {
if (!isTimerRunning()) {
startTimer();
ssButton.setText(R.string.stop);
} else {
stopTimer();
ssButton.setText(R.string.start);
}
} else if (v.getId() == R.id.reset) {
resetTimer();
TextView counter = (TextView) findViewById(R.id.counter);
counter.setText(DateUtils.formatElapsedTime(0));
ssButton.setText(R.string.start);
}
}
private void startTimer() {
mStart = System.currentTimeMillis();
mHandler.removeMessages(0);
mHandler.sendEmptyMessage(0);
}
private void stopTimer() {
mHandler.removeMessages(0);
}
private boolean isTimerRunning() {
return mHandler.hasMessages(0);
}
private void resetTimer() {
stopTimer();
if (mTimeListAdapter != null)
mTimeListAdapter.add(mTime/1000);
mTime = 0;
}
private static class MyHandler extends Handler {
private final WeakReference<TimeTrackerActivity> mActivity;
public MyHandler(TimeTrackerActivity activity) {
mActivity = new WeakReference<TimeTrackerActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
TimeTrackerActivity activity = mActivity.get();
if (activity != null) {
long current = System.currentTimeMillis();
mTime += current - mStart;
mStart = current;
TextView counter = (TextView) activity.findViewById(R.id.counter);
counter.setText(DateUtils.formatElapsedTime(mTime/1000));
this.sendEmptyMessageDelayed(0, 250);
}
}
}
}
<TextView android:id="@+id/lap_name" android:layout_height="wrap_content"
android:text="@string/default_value_lap" android:layout_weight="1" android:layout_width="0dp" />
<TextView android:id="@+id/lap_time" android:layout_height="wrap_content"
android:text="@string/default_value_time" android:layout_weight="1"
android:layout_width="0dp" android:gravity="right" />
Hi. I bought your book and read up to chapter two. i ran into a couple problems with the code based on my version of android etc... anyway i got it all working and those are the files i changed to get it working. I am going to continue through your book and see how it goes. if you want to contact me - my email is [email protected]
Check this code out and see what you think... i think i fixed a memory leak with the handler too... maybe....
First of all, great book on Android UI, very clearly written, and followed by straithforward examples.
I'm having trouble executing some of the examples. Using Eclipse, have managed to correctly
build and execute examples prior tu say, Chapter 3 or 4.
In ch4. there are several files needed to be changed in order for examples to even build. Some
deletions of @OverRide where a must in my case.
So, example from chapter 5, after making some changes in order to be built, gives me following logcat
on Motorola Xoom runing ICS 4.0.3, and the same output is on Qemu emulating Froyo system:
05-27 22:21:55.098: E/AndroidRuntime(425): FATAL EXCEPTION: main
05-27 22:21:55.098: E/AndroidRuntime(425): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example/com.example.TimeTrackerActivity}: java.lang.ClassNotFoundException: com.example.TimeTrackerActivity in loader dalvik.system.PathClassLoader[/data/app/com.example-2.apk]
05-27 22:21:55.098: E/AndroidRuntime(425): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-27 22:21:55.098: E/AndroidRuntime(425): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-27 22:21:55.098: E/AndroidRuntime(425): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-27 22:21:55.098: E/AndroidRuntime(425): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-27 22:21:55.098: E/AndroidRuntime(425): at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 22:21:55.098: E/AndroidRuntime(425): at android.os.Looper.loop(Looper.java:123)
05-27 22:21:55.098: E/AndroidRuntime(425): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-27 22:21:55.098: E/AndroidRuntime(425): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 22:21:55.098: E/AndroidRuntime(425): at java.lang.reflect.Method.invoke(Method.java:521)
05-27 22:21:55.098: E/AndroidRuntime(425): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-27 22:21:55.098: E/AndroidRuntime(425): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-27 22:21:55.098: E/AndroidRuntime(425): at dalvik.system.NativeStart.main(Native Method)
05-27 22:21:55.098: E/AndroidRuntime(425): Caused by: java.lang.ClassNotFoundException: com.example.TimeTrackerActivity in loader dalvik.system.PathClassLoader[/data/app/com.example-2.apk]
05-27 22:21:55.098: E/AndroidRuntime(425): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-27 22:21:55.098: E/AndroidRuntime(425): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-27 22:21:55.098: E/AndroidRuntime(425): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-27 22:21:55.098: E/AndroidRuntime(425): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-27 22:21:55.098: E/AndroidRuntime(425): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-27 22:21:55.098: E/AndroidRuntime(425): ... 11 more
Of course, after making new project and adding support package, and making SDK minimal 8, bla.bla.bla... as I said
projects prior to Ch3 are functioning. Maybe something when the fragments showed up in code...
Am I missing something here?
<string name="app_name">TimeTracker</string>
<string name="action_settings">Settings</string>
<string name="elapsed_time_format">%1$d:%2$02d:%3$02d:%4$03d</string>
<string name="finish">Finish</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="task_name">Task %1$d</string>
<string name="default_value">0</string>
<string name="default_value_lap">Lap 1</string>
<string name="default_value_time">00:00:00</string>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.