Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- layout for edittext
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <EditText
- android:id="@+id/edittext_search"
- android:inputType="text"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
- </RelativeLayout>
- menu
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- tools:context=".MainActivity">
- <item android:id="@+id/search_item"
- android:icon="@drawable/abs__ic_search"
- android:orderInCategory="0"
- android:title="@string/search_menuitem_titile"/>
- <!-- android:actionLayout="@layout/search_layout" -->
- </menu>
- and code in Activity:
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getSupportMenuInflater().inflate(R.menu.menu_main, menu);
- menu.getItem(0).setActionView(R.layout.search_layout);
- menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
- etSearch = (EditText)menu.getItem(0).getActionView().findViewById(R.id.edittext_search);
- etSearch.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- Log.d(TAG, "s");
- }
- @Override
- public void afterTextChanged(Editable s) {
- }
- });
- MenuItem menuSearch = menu.findItem(R.id.search_item);
- menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
- @Override
- public boolean onMenuItemActionExpand(MenuItem menuItem) {
- Log.d(TAG, "onMenuItemActionExpanded");
- etSearch.requestFocus();
- InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
- return false;
- }
- @Override
- public boolean onMenuItemActionCollapse(MenuItem menuItem) {
- Log.d(TAG, "onMenuItemActionCollapse");
- etSearch.clearFocus();
- return false;
- }
- });
- return super.onCreateOptionsMenu(menu);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement