0

I am a Android Newbie and am having trouble getting the buttons in this table to fire the OnClick event. The goal is to have a table full of buttons and when they are clicked, do something. Right now, I just want to output to the console, but it will not call the OnClick method.

I am sure it is a stupid issue and appreciate the help.

public class HiThereActivity extends Activity  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  

        ArrayList<String> names = new ArrayList<String>();

        names.add("Michael");
        names.add("John");
        names.add("Mike");
        names.add("Tom");
        names.add("Steve");

        ArrayList<String> codes = new ArrayList<String>();

        codes.add("abcde");
        codes.add("fghij");
        codes.add("klmno");
        codes.add("pqrst");
        codes.add("uvwxy");
        codes.add("12345");
        codes.add("67890");



        TableLayout table = new TableLayout(this);  

        table.setStretchAllColumns(true);  
        table.setShrinkAllColumns(true);  

        TableRow rowTitle = new TableRow(this);  
        rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);  

        TableRow rowConditions = new TableRow(this);  
        rowConditions.setGravity(Gravity.CENTER);  


        // title column/row  
        TextView title = new TextView(this);  
        title.setText("IH10 Katy Extension - 11/7/2011");  

        title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);  
        title.setGravity(Gravity.CENTER);  
        title.setTypeface(Typeface.SERIF, Typeface.BOLD);  

        TableRow.LayoutParams params = new TableRow.LayoutParams();  
        params.span = (codes.size() + 1);  

        rowTitle.addView(title, params);  

        Iterator<String> nameItr = names.iterator();
        while(nameItr.hasNext()){
            TableRow row = new TableRow(this);
            TextView nameLable = new TextView(this);
            nameLable.setText(nameItr.next());
            nameLable.setTypeface(Typeface.DEFAULT_BOLD);
            row.addView(nameLable);

            Iterator<String> codeItr = codes.iterator();
            while(codeItr.hasNext()){
                /*TextView codeLabel = new TextView(this);  
                codeLabel.setText(codeItr.next());  
                codeLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);

                row.addView(codeLabel);
                */
                codeItr.next();
                Button rowButton = new Button(this);
                rowButton.setText("8");
                //rowButton.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
                rowButton.setOnClickListener(btnListener);


                row.addView(rowButton);


            }
            table.addView(row);
        }


        setContentView(table);  
    }

    private OnClickListener btnListener = new OnClickListener() {
        public void onClick(View v) {
            System.out.println("TEST");
        }
    };


}

EDIT #1: I did some experimenting and did this very simple example the the onClick is not called. I know it is something really stupid. Any ideas?

package com.bordeloniphone.timeentry;

import android.app.Activity;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TimeEntryActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

        Button okButton = new Button(this);
        okButton.setText(":)");
        okButton.setOnClickListener(this);
        setContentView(okButton);

    }

public void onClick(View v) {
    Log.d("TEST", "TEST");


}

}
1
  • check logcat, changed my answer....
    – Mateusz
    Dec 2, 2011 at 22:36

3 Answers 3

0

If you are using Eclipse go to Window->Show View->Other... and in Android group select Logcat. As John mentioned it is very useful for Debug.

1
  • Thanks Sanandrea. I am able to see Logcat and have tested to make sure it is getting my log messages, but the onclick method is not called. Dec 3, 2011 at 2:32
0

Android's printing to console with System.out.println() won't work because the console is internal to the phone.

Instead of printing to console, try showing a toast instead (its easier to see):

private OnClickListener btnListener = new OnClickListener() {
    public void onClick(View v) {
        Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
    }
};

Or if you really want to print to a console, print to LogCat instead:

Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT");

Update:

Maybe try calling the setDescendantFocusability method on your TableLayout and TableRow variables:

table.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
rowTitle.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
rowConditions.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

and in your iterative loop:

row.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
4
  • Darnit. I am feeling pretty stupid. I tried both the Toast and Log and put in breakpoints and the OnCLick is never called. I am wondering if the table is somehow intercepting the event. Dec 2, 2011 at 23:59
  • Thanks for your help John. I checked LogCat and nothing is getting logged. Also, when I go into debug, it does not get to the OnClick method. I tried the Focusability setting as well. That was a good idea. I do a lot of iOS dev and never run into anything like this. It seems like it would not be that weird to have buttons in table rows. Dec 3, 2011 at 1:45
  • @MichaelBordelon in regards to the simple TimeEntryActivity on your update, if even that is not working, then you may not be viewing LogCat correctly. Make sure you have your device plugged in an selected in the DDMS perspective, and make sure there is other LogCat output. Also make sure you can see debug logcat messages. I recommend using a toast (like in my example) instead of using logcat if you can't see any output. Dec 5, 2011 at 20:40
  • 1
    Did anything ever come from this? I am facing a similar issue.
    – Fred
    Mar 17, 2012 at 3:18
0

You just need an @Override statement before the onClick method:

public class TimeEntryActivity extends Activity implements OnClickListener{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    Button okButton = new Button(this);
    okButton.setText(":)");
    okButton.setOnClickListener(this);
    setContentView(okButton);

}

**@Override**

public void onClick(View v) {
    Log.d("TEST", "TEST");


}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.