Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. layout for edittext
  2. <RelativeLayout
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. xmlns:android="http://schemas.android.com/apk/res/android">
  6.  
  7. <EditText
  8. android:id="@+id/edittext_search"
  9. android:inputType="text"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"/>
  12.  
  13. </RelativeLayout>
  14.  
  15.  
  16. menu
  17. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  18. xmlns:tools="http://schemas.android.com/tools"
  19. tools:context=".MainActivity">
  20. <item android:id="@+id/search_item"
  21. android:icon="@drawable/abs__ic_search"
  22. android:orderInCategory="0"
  23. android:title="@string/search_menuitem_titile"/>
  24. <!-- android:actionLayout="@layout/search_layout" -->
  25.  
  26. </menu>
  27.  
  28.  
  29. and code in Activity:
  30. @Override
  31. public boolean onCreateOptionsMenu(Menu menu) {
  32. getSupportMenuInflater().inflate(R.menu.menu_main, menu);
  33.  
  34. menu.getItem(0).setActionView(R.layout.search_layout);
  35. menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
  36.  
  37. etSearch = (EditText)menu.getItem(0).getActionView().findViewById(R.id.edittext_search);
  38.  
  39. etSearch.addTextChangedListener(new TextWatcher() {
  40. @Override
  41. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  42.  
  43. }
  44.  
  45. @Override
  46. public void onTextChanged(CharSequence s, int start, int before, int count) {
  47. Log.d(TAG, "s");
  48. }
  49.  
  50. @Override
  51. public void afterTextChanged(Editable s) {
  52.  
  53. }
  54. });
  55. MenuItem menuSearch = menu.findItem(R.id.search_item);
  56. menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
  57. @Override
  58. public boolean onMenuItemActionExpand(MenuItem menuItem) {
  59. Log.d(TAG, "onMenuItemActionExpanded");
  60. etSearch.requestFocus();
  61. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  62. imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
  63. return false;
  64. }
  65.  
  66. @Override
  67. public boolean onMenuItemActionCollapse(MenuItem menuItem) {
  68. Log.d(TAG, "onMenuItemActionCollapse");
  69. etSearch.clearFocus();
  70. return false;
  71. }
  72. });
  73. return super.onCreateOptionsMenu(menu);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement