javaandroidtablelayout

Is it possible to set Focus or set OnClick for an Android TableLayout?


Problem: I've been trying to set either the setOnClickListener or setOnFocusChangeListener on a TableLayout but I'm not getting any response and there are no debug errors or issues I can identify.

What I'm trying to do: When someone clicks on the tablelayout to add authors, the EditText for a single author populates with a standard message (maybe disable it) if the tablelayout has more than one child using getChildCount().

What I have tried: As I mentioned, I've tried setting the setOnClickListener and setOnFocusChangeListener events but nothing seems to happen. I tried setting Toast messages to see if it is being activated but I'm not getting anything. I'm not sure what I may be doing wrong. I've set OnClick and Focus for many objects, but this is the first tablelayout and I'm having trouble.

tableLayoutAuthors.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            Toast.makeText(AddNote.this,tableLayoutAuthors.getChildCount(), Toast.LENGTH_LONG);
            if(tableLayoutAuthors.getChildCount()>1)
                author.setText(R.string.add_author_phrase);
            else if(tableLayoutAuthors.getChildCount()==1)
                populateSourceDetails(DBQueryTools.getSourcesByTitle(sourceTitle.getText().toString()));
        }
    }
});

Solution

  • I found a my solution...

    Once I selected addOnLayoutChangeListener the table would responded as I was hoping. This may not be my final design response but it worked with the following:

    View tblLayAut = tableLayoutAuthors;
    tblLayAut.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            if(tableLayoutAuthors.getChildCount()>1)
                author.setText(R.string.add_author_phrase);
            else if(tableLayoutAuthors.getChildCount()==1)
                populateSourceDetails(DBQueryTools.getSourcesByTitle(sourceTitle.getText().toString()));
        }
    });
    

    }