Hi, I picked up the book a few weeks ago and slowly going through the material to build a good foundation.
I have previous experience with JavaScript, MEL, Cg, etc. but no "real" compiled programming language.
I'm wondering if this book is for me? I've had trouble with every example in the book. I've copied every line word for word and none of them worked on the first try. Possibly I'm missing the basics, things not explicitly in the tutorials (like to "import" classes when I use them, what to name files, creating extra Java files with new class extensions, etc). Debugging has been mostly trial and error, looking for spelling mistakes, and letting Eclipse suggest a fix, or looking at downloaded code(which is often different from the book's examples). I wish I knew a way to isolate errors...to print so that I can tell which part of the code is working. It's very hard to know what's failing/where. I end up butting my head against a wall looking for anything obvious -since I'm not experienced enough yet to know the fix.
I've managed to get through the examples to page 87(ClearableEditText example)...eventually getting them to work. But I'm stuck on that example.
My button seems to do nothing. I'm using the code from the wrox download package...so I think everything is right. Eclipse doesn't complain and my app runs on the emulator. But the button does nothing.
I even tried just putting in my command to change the text directly in the code:
editText.setText("Test Text");
Nothing. The "editText" field remains unchanged.
How can I tell if it's the views (the Button, the EditText?) or the function calling for the text to be cleared?
I can provide code, but it's basically the same as the downloaded code. If I can't get a function to attach to a button, I have no hope of ever making anything on android. I mean, that's as basic as it gets, right?
Code:
//ClearableEditText.java - the second java file in my src folder.
/**
* Creating a new compound view by extending LinearLayout.
* In this example the layout itself is defined as an external
* resource {@link clearable_edit_text_view} which is inflated
* within the constructor.
*/
package com.paad.todolist;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class ClearableEditText extends LinearLayout
{
EditText editText;
Button clearButton;
public ClearableEditText(Context context) {
super(context);
// Get a reference to the LayoutInflater Service
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
// Inflate the view from the layout resource
li.inflate(R.layout.text_button_group_layout, this, true);
// Get references to the child controls
editText = (EditText)findViewById(R.id.editText);
clearButton = (Button)findViewById(R.id.clearButton);
// Hook up the functionality
hookupButton();
}
private void hookupButton() {
clearButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
editText.setText("");
}
});
}
}
Code:
//text_button_group_layout.xml - the layout file.
<?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/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter custom text here..."
/>
<Button
android:id="@+id/clearButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clear"
/>
</LinearLayout>
Code:
//TextButtonGroup.java - the first Java file containing my activity.
package com.paad.todolist;
import android.app.Activity;
import android.os.Bundle;
public class TextButtonGroup extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_button_group_layout);
}
}
-Vlad