Wrox Programmer Forums
|
BOOK: Professional Android 2 Application Development
This is the forum to discuss the Wrox book Professional Android 2 Application Development, 2nd Edition by Reto Meier; ISBN: 978-0-470-56552-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 2 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 20th, 2012, 02:32 AM
Registered User
 
Join Date: Feb 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation ToDoList - Notepad View

In chapter 4, the code snippet is shown to view our todolist in notepad view. I implemented the code but am not able to view the list in notepad view. Please point out the problem.
This is my current java code:

package todo.list;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.graphics.Canvas;
import android.graphics.Paint;

public class ToDoListActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView)findViewById(R.id.list);
final EditText myText = (EditText)findViewById(R.id.edittext);
final ArrayList<String> toDoItems = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this, R.layout.todolist_item, toDoItems);
myListView.setAdapter(aa);
myText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
if(keyCode == KeyEvent.KEYCODE_ENTER) {
toDoItems.add(myText.getText().toString());
aa.notifyDataSetChanged();
myText.setText("");
return true;
}
return false;

}
});


}

public class ToDoListItemView extends TextView {
public ToDoListItemView(Context context, AttributeSet ats, int ds) {
super(context, ats, ds);
init();
}

public ToDoListItemView(Context context) {
super(context);
init();
}

public ToDoListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private Paint marginPaint;
private Paint linePaint;
private int paperColor;
private float margin;

private void init() {
Resources myResources = getResources();
marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
marginPaint.setColor(myResources.getColor(R.color. notepad_margin));
linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(myResources.getColor(R.color.no tepad_lines));
paperColor = myResources.getColor(R.color.notepad_paper);
margin = myResources.getDimension(R.dimen.notepad_margin);
}

@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(paperColor);
canvas.drawLine(0, 0, getMeasuredHeight(), 0, linePaint);
canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);
canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
canvas.save();
canvas.translate(margin, 0);
super.onDraw(canvas);
canvas.restore();

}
}
}


In the book it is not told where to write the ToDoListItemView class. Please tell me whether I have written it in the right place, or I have to create another java file and write it there. The above code works fine except that I don't get a notepad view, and when I press ENTER, "the application stops working" error occurs.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 4 ToDoList runs but no custom view? scottm BOOK: Professional Android 2 Application Development 8 June 21st, 2010 01:04 PM
Encryption Notepad, need some help! Groogy Visual C++ 2005 0 March 23rd, 2007 12:31 PM
Notepad or VS,Net Oh_Help VS.NET 2002/2003 2 February 11th, 2004 01:59 AM
Reference in notepad???? quetzalcoatl ADO.NET 2 October 31st, 2003 06:54 PM
Newline in notepad, HOW.....???? veskoula XSLT 3 October 29th, 2003 10:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.