Android - ListView EditText Focus Issues

I have an EditText that I'm using as the footer View for my ListView. I'm attempting to access the focus / lose focus events, but for some reason, it's firing multiple times per focus. What is going on here?

// in onCreate

// ...

// the footer
View v = getLayoutInflater().inflate( R.layout.comment_edittext, null );

EditText commentEditText = (EditText)v.findViewById( R.id.comment_edittext );
commentEditText.setOnFocusChangeListener( new OnFocusChangeListener() {

    @Override
    public void onFocusChange( View v, boolean hasFocus )
    {
        U.log("View: " + v.getClass().getName().toString() );

        if( hasFocus )
        {
            U.log( "Clicked" );
        }
        else
        {
            U.log( "Un Clicked" );
        }
    }
} );

// add the footer
commentListView.addFooterView( v );

// ...

The output, of a single touch, which brings up the keyboard is:

04-11 10:22:17.449: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.459: E/004 - X(4576): Clicked
04-11 10:22:17.569: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.569: E/004 - X(4576): Un Clicked
04-11 10:22:17.569: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.569: E/004 - X(4576): Clicked
04-11 10:22:17.689: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.689: E/004 - X(4576): Un Clicked
04-11 10:22:17.709: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.709: E/004 - X(4576): Clicked

I can kind of understand it calling multiple events with hasFocus set to true, but why then are there also falses, triggering the Un Clicked log?

EDIT: Furthermore, whenever I scroll the ListView up and down, moving the EditText in and out of view, it's calling these events as well. Am I using the wrong listener type perhaps?

Answers


From personal experience, EditTexts inside ListViews are a bane. Because of the way views are recycled and rendered, the focus behaves strangely. I recommend approaching the problem some other way. Actually, if it's a comment text box, wouldn't it be better if it was always visible?

Sample layout:

  <LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"> 
    <ListView 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1"/> 
    <EditText 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"/>  
  </LinearLayout>

Use a GridView instead and give a numColumns of 1. If you don't like how that reads in your java classes, store them as an AbsListView (and just keep them as a GridView in your xml).

ListView is buggy with EditTexts, GridViews lack those problems.


Need Your Help

How to build an iPhone static library based on a Makefile?

c ios build make

I have managed to build a static C library on OSX using make on the command line.

Windows 7 icon sizing problems

c# windows-7 icons

I'm trying to create an application icon. I took a bunch of created *.ico files (one for each size) and combined them into a single *.ico file. I can see them all fine. I have 16x16, 32x32, 48x48,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.