This is an todolist application that makes each item appear as if on a paper
pad(margin on the side & black line between items)
When i run the app, it does not show the entered text, but empty items are added to the list every time an entry is made. Also, the app works fine if todolistitemview isnt used in the array adapter construction.
This is my todolistactivity file:
package com.paad.todolist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
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;
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.myListView);
final EditText myEditText = (EditText)findViewById(R.id.myEditText);
// Create the array list of to do items
final ArrayList<String> todoItems = new ArrayList<String>();
// Create the array adapter to bind the array to the listview
int resID = R.layout.todolist_itemview;
final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, resID,
todoItems);
// Bind the array adapter to the listview.
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
todoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
return true;
}
return false;
}
});
}
}
This is my todolistitemview code:
package com.paad.todolist;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
public class todolistitemview extends TextView{
public todolistitemview(Context context){
super(context);
init();
}
public todolistitemview(Context context,AttributeSet ats){
super(context,ats);
init();
}
public todolistitemview(Context context,AttributeSet ats,int ds){
super(context,ats,ds);
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.notepad_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, getMeasuredWidth(), 0, linepaint);
canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linepaint);
canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginpaint);
canvas.save();
canvas.translate(margin,0);
}
}
These are the layout files-
main.xml-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New To Do Item"
/>
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
todolist_itemview.xml-
<?xml version="1.0" encoding="utf-8"?>
<com.paad.todolist.todolistitemview
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="@color/notepad_text"
android:fadingEdge="vertical"
/>
Please help me resolve this issue. Any help is much appreciated.
You didn’t copy the full code from the book. In your custom
TextViewonDraw()method, after the line:you must add:
Without the call to the superclass
onDraw()method your customTextViewwill not draw the most important part , the text, that is why you actually get the paper drawing but no text.