Reputation: 1773
I'm having a bit of an issue with the first Android layout I made. On my interface, I have an EditText at the top of my interface, with a ListView under, showcasing a list of elements. I want the EditText to lose focus and the virtual keyboard to disappear when the user touches the ListView. I based myself off this page http://developer.android.com/guide/topics/ui/ui-events.html .
The issue is, while my code does that, it also blocks any other form of interaction with the ListView itself (Can't navigate in the ListView, or select elements). Is there a way to re-establish the default behaviour along with the behaviour I added?
Here is my MainActivity.java:
public class MainActivity extends Activity {
// Variable declarations
EditText ed;
ListView lv;
// Function called when the application starts
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Elements declaration
ed = (EditText) findViewById(R.id.search_text);
lv = (ListView) findViewById(R.id.media_list);
// Events declaration
lv.setOnTouchListener(listViewListener);
// We create a task to load the media list in a different thread.
DownloadMediaList task = (DownloadMediaList) new DownloadMediaList (MainActivity.this,new TheInterface() {
@Override
public void theMethod(ArrayList<Media> result) {
lv.setAdapter(new MediathequeListAdapter(MainActivity.this, result));
}
}).execute();
}
private OnTouchListener listViewListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent motion){
ed.clearFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);
return true;
}
};
// Interface to communicate with the async load.
public interface TheInterface {
public void theMethod(ArrayList<Media> result);
}
}
EDIT: Here is my main layout if that helps:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
<LinearLayout android:id="@+id/container_search"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<EditText android:id="@+id/search_text"
android:inputType="text"
android:hint="@string/search_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80" />
<Button android:id="@+id/search_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20" />
</LinearLayout>
<ListView
android:id="@+id/media_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />
</LinearLayout>
Upvotes: 0
Views: 625
Reputation: 30168
Return false
in your onTouch()
. Otherwise you're telling the system that you've handled all touch events.
Upvotes: 1