Android: AutoCompleteTextView as Collapsable Action View focus issues - android

Hi so for my needs i cannot use SearchView because i am filtering an array list that contains some custom object plus this AutoCompleteTextView is embedded into the toolbar as a menu item
<item
android:id="#+id/searchFoodMenuItem"
android:title="#string/generic.search"
app:actionLayout="#layout/view_search_food_auto_complete"
android:orderInCategory="1"
android:icon="#drawable/ic_search_white_48dp"
app:showAsAction="ifRoom|collapseActionView"
/>
I wish to mimic the behaviour of searchview in these cases:
Clicking on the icon puts the auto complete text view in focus and brings up the key board (I have kind of achieved this but i have a small issue that i need help with)
While the auto complete text view is in focus, clicking anywhere else on the screen that is not the keyboard will remove the focus of the auto complete text view rather than passing the touch to the view that was pressed.
Some code
So i wrote my own extension of the AutoCompleteTextView.
Here is some key snippets
The part below simply closes / opens the keyboard when the focus of the view has changed. This also includes clicking on it and pressing back on the soft keyboard.
#Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
InputMethodManager inputMethodManager = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (focused) {
inputMethodManager.showSoftInput(this, 0);
} else {
setFocusableInTouchMode(false);
inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
#Override
public boolean performClick() {
setFocusableInTouchMode(true);
requestFocus();
return super.performClick();
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
clearFocus();
}
return false;
}
Below is the code which puts the view into focus when the menu item is expanded
MenuItemCompat.setOnActionExpandListener(mSearchFoodMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
mSearchFoodInputField.post(new Runnable() {
#Override
public void run() {
DebugUtils.Log("onMenuItemActionExpand");
mSearchFoodInputField.requestFocus();
}
});
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
The above works the first time, i.e. if i click on the menu item, the auto complete text view becomes focused and the key board appears. All is well, but if i collapse this menu item and do it again, it no longer becomes focused and i am not sure why.
More reasons I am not using SearchView
The main reason I do not want to use it is because I want to display search suggestions based on my array list of objects. My understanding of the API tells me that search view can only do this on a database Cursor (which I am not using)
Search view code
The menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="#string/generic.search"
android:icon="#drawable/ic_search_white_48dp"
app:showAsAction="ifRoom|collapseActionView"
android:iconifiedByDefault="true"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
In onCreateOptionsMenu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.piece_discover_menu, menu);
mSearchMenuItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchMenuItem);
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnSuggestionListener(this);
mSearchView.setSuggestionsAdapter(mFoodSearchAdapter);
}
Below is what i tried
I made a searchable config
Note i tried with and without completetionThreshold
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint=“#string/search_hint”
android:searchSuggestThreshold=“1”
android:completionThreshold=“1”
</searchable>
Me applying the search config
Note this was declared between the application tags
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
Me adding the search manager
SearchManager searchManager =
(SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(
searchManager.getSearchableInfo(getActivity().getComponentName()));

Thanks to pskink , I used a search view instead. To get suggestions like the AutoCompleteTextView i used a cursorAdapter. However if like me you had an array list of some Java object, you need to convert it to a Cursor as follows.
public static final String[] COLUMNS = new String[]{"_id","foodName","foodReference"};
public static final int ID_INDEX = 0, FOOD_NAME_INDEX = 1, FOOD_REFERENCE_INDEX = 2;
private MatrixCursor convertToCursor (ArrayList<Food> foods){
MatrixCursor cursor = new MatrixCursor(COLUMNS);
for(Food food : foods){
String[]row = new String[COLUMNS.length];
row[ID_INDEX] = Integer.toString(mFoods.indexOf(food));
row[FOOD_NAME_INDEX] = food.getName();
row[FOOD_REFERENCE_INDEX] = food.getReferenceNumber();
cursor.addRow(row);
}
return cursor;
}

Related

Search View disappearing (icon)

I have an interesting problem - though maybe it is the expected behavior? I have a SearchView in my tool bar, which functions just fine till I enter a search then close it, at which time the search icon disappears and the ability to do another search is lost.
This behavior only happens when the search query is initiated - so if I enter search text, clear the search, then close it, the icon is there. But if I close it with data in it, then the Icon disappears. It is not the text color - I have clicked away and nothing seems to be there.
Here are some pictures of what I am talking about:
Search is there:
Enter search, query:
Close SearchView, icon gone
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/library_forms_refresh"
android:orderInCategory="100"
android:showAsAction="always|withText"
android:icon="#drawable/ic_action_refresh"
android:title="refresh"/>
</menu>
The code looks like this:
import android.widget.SearchView;
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
menu.clear();
mMenu = menu; // remember so we can get to later.
inflater.inflate(R.menu.library_forms, menu);
(removed some code not related to this)
// Implementing ActionBar Search inside a fragment
MenuItem item = menu.add("Search");
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
sv = new SearchView(getActivity());
// If we are on a Phone, make the search an icon, otherwise show it all
if (GlobalState.getInstance().isTwoPane == true) {
sv.setIconifiedByDefault(false);
}
else {
sv.setIconifiedByDefault(true);
}
sv.setFocusable(false);
// modifying the text inside edit text component
int id = sv.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) sv.findViewById(id);
textView.setHint("Form or Reference Number");
textView.setHintTextColor(Color.WHITE);
textView.setTextColor(Color.WHITE);
// Change the Icon color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int searchButtonId = sv.getContext().getResources().getIdentifier("android:id/search_button", null, null);
ImageView searchButton = (ImageView) sv.findViewById(searchButtonId);
searchButton.setImageTintList(ColorStateList.valueOf(Color.WHITE));
}
// implementing the listener
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
Handler handler = new Handler();
#Override
public boolean onQueryTextSubmit(String s) {
handler.removeCallbacksAndMessages(null);
if (s != null && s.trim().length() > 0)
{
searchFilter = s;
// Delay the lookup by 800 ms
handler.postDelayed(new Runnable() {
#Override
public void run() {
// do some stuff here...
}
}, 800);
}
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
// If the string is empty, reload the form
if (newText == null || newText.trim().length() == 0)
{
searchFilter = null;
// do some stuff here...
// Hides the soft keyboard
sv.setVisibility(View.INVISIBLE);
sv.setVisibility(View.GONE);
}
else {
onQueryTextSubmit(newText);
}
return true;
}
});
item.setActionView(sv);
}
The problem was this code:
// do some stuff here...
// Hides the soft keyboard
sv.setVisibility(View.INVISIBLE);
sv.setVisibility(View.GONE);
Which hid the keyboard and the icon.

EditText.requestFocus() returns true but doesn't really give focus to the view

I have an EditText in expandable action view in Toolbar. When I expand the action, the EditText is shown and I want it to get focus. To achieve that I do following:
action_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/connections_find_people_container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<EditText
android:id="#+id/connections_find_people_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
</RelativeLayout>
In my fragment
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.add_connection, menu);
mAddMenuItem = menu.findItem(R.id.menu_add);
MenuItemCompat.setActionView(mAddMenuItem, R.layout.action_layout);
mSearchView = MenuItemCompat.getActionView(mAddMenuItem);
mSearchEditText = (EditText) mSearchView.findViewById(R.id.connections_find_people_edit);
MenuItemCompat.setOnActionExpandListener(mAddMenuItem, mOnSearchExpandListener);
super.onCreateOptionsMenu(menu, inflater);
}
private final OnSearchExpandListener mOnSearchExpandListener = new MenuItemCompat.OnActionExpandListener {
mSearchEditText.requestFocus();
return true;
}
#Override
public boolean onMenuItemActionCollapse(final MenuItem item) {
return true;
}
}
Surprisingly, this does not work. After tapping the action in toolbar, the view expands, mSearchEditText is shown, but doesn't get focus.
More specifically (and weirdly), mSearchEditText.requestFocus(); returns true, but software keyboard is still not shown and there is no cursor in mSearchEditText.
Showing software keyboard is not a big deal, there are ways to force it, but even when I show the keyboard, typing doesn't insert any text into mSearchEditText. It's only after I tap on mSearchEditText when cursor appears and I can type some text in it.
What I've tried:
Setting focusable and focusableInTouchMode both in XML and programatically and both in onCreateOptionsMenu() and when the action view is expanded
Clearing focus before requesting it like mSearchEditText.clearFocus()
Delaying requestFocus() after the view expands
It's really weird, because following
#Override
public boolean onMenuItemActionExpand(final MenuItem item) {
mSearchEditText.setFocusableInTouchMode(true);
mSearchEditText.setFocusable(true);
mSearchView.postDelayed(new Runnable() {
#Override
public void run() {
if (mSearchEditText.requestFocus()) {
KeyboardUtils.showSoftKeyboard(ConnectionsFragment.this);
}
}
}, 300);
return true;
}
shows the keyboard, which should mean mSearchEditText has focus, but typing doesn't enter any text. (Again, when I tap on the EditText, cursor appears and everything works fine).
Aaand as it happens sometimes, soon after posting the question I found the answer myself.
Instead of getting reference to the EditText in onCreateOptionsMenu() I had to use the MenuItem passed to onMenuItemActionExpand() and get the reference there.
So this now works for me:
#Override
public boolean onMenuItemActionExpand(final MenuItem item) {
final View actionView = MenuItemCompat.getActionView(item);
final View edit = actionView.findViewById(R.id.connections_find_people_edit);
if(edit.requestFocus()) {
KeyboardUtils.showSoftKeyboard(ConnectionsFragment.this);
}
return true;
}

Android SearchView requires two clicks to expand the view

Short: When I click on my SearchViewIcon, the SearchView doesn't collapse/expand.
Long:
I"m using a SearchView to filter a RecyclerView in a Fragment that is in my MainActivity.
When I click on the SearchViewIcon (SearchView is iconified by default). I open the tab with the correct Fragment with this code:
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(2, false);
}
});
The Tab with the correct Fragment is opened like expected. Also the text input is shown, but the SearchView stays iconified. (See picture below.)
Application
My SearchView in XML:
<item
android:id="#+id/action_search"
android:orderInCategory="1"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/menu_item_search_title"
app:showAsAction="ifRoom"
app:queryHint="Search name or function"
app:actionViewClass="android.support.v7.widget.SearchView" />
Things I already tried:
Setting my showAsAction to always or ifRoom|collapseActionView
app:showAsAction="always"
app:showAsAction="ifRoom|collapseActionView"
Request focus on my SearchView:
searchView.requestFocus();
Expanding my SearchViewItem:
MenuItem searchViewItem = menu.findItem(R.id.action_search);
searchViewItem.expandActionView();
But non of these things worked...
EDIT
Like the title says, if I click the SearchViewIcon again, the SearchView does expand.
I've also encountered a similar problem, and was able to solve it.
The main problems I've needed to solve were:
searchView was not expanding by default
the search EditText didn't get a focus until a second click
even after solving (2), the keyboard didn't show up until a second click
solutions:
in menu.xml need to define app:showAsAction as "collapseActionView" + in java code need also to call searchView.setIconifiedByDefault(false) for the expansion to take priority over the iconification (that is - when the icon is pressed, expand and don't stay in icon mode)
add a MenuItem.OnActionExpandListener for your search menu-item object and use a handler to post a runnable that will request focus to your search-view object.
(why runnable? because requesting focus when the menu is still not fully inflated is not guaranteed to work. When requesting focus in a handler-runnable, I'm making sure the focus request happens AFTER all the work in the onCreateOptionsMenu() is ready and finished.
in The same defined runnable from (2), also ask android OS to show the keyboard.
complete code solving all those problems:
menu.xml:
<item
android:id="#+id/searchContacts"
android:icon="#drawable/ic_search_white_24dp"
android:title="search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.widget.SearchView"
/>
<!-- and of course your other menu items -->
searchable configuration:
Need to create such xml file.
right-click your res folder and choose new --> android resource file. put whatever you want as file name ("searchable" will work ok, for example), and choose XML as resource type.
Then copy & paste this code in the created file (replace the hint string with your own):
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="put here your hint string to be shown"
/>
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
implementSearch(menu);
return true;
}
private void implementSearch(final Menu menu) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final MenuItem searchMenuItem = menu.findItem(R.id.searchContacts);
final SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener(){
#Override
public boolean onMenuItemActionExpand(MenuItem item){
// the search view is now open. add your logic if you want
new Handler().post(new Runnable() {
#Override
public void run() {
searchView.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) { // it's never null. I've added this line just to make the compiler happy
imm.showSoftInput(searchView.findFocus(), 0);
}
}
});
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item){
// the search view is closing. add your logic if you want
return true;
}
});
}
Also, if you want to use your own callbacks for the search's text change and submitted (instead of Android intents), in the implementSearch() method add a call to searchMenuItem.setOnActionExpandListener(new SearchView.OnQueryTextListener({...})).
view this SO question for more details
The simplest solution is to use always option for app:showAsAction:
app:showAsAction="always"
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:title="search"
app:showAsAction="always"
app:actionViewClass="android.widget.SearchView"
/>

How do I create a "Search" field in a contextual action bar?

I have an activity with an ActionBar. One of the options is to search a document (not necessarily local). I want to use a contextual action bar (CAB) with the following items:
edit field - place for user to enter text they want to search for
Previous Button - moves to the previous item that matches the search
Next Button - moves to the next item that matches the search
I want to use a CAB for several reasons. Primarily, I want the user to pick an option which will bring up the CAB defined above. When the user is done with the search, they select the done button and the ActionBar returns to previous state.
Here's the problem. I can't get the search item to appear in my CAB in an expanded state. NOTE: The activity I'm attempting to modify is NOT the main activity but is an activity launched by the user based on certain events. Once this activity is loaded, the user may or may not decide to use the 'search' functionality I'm describing. I also do not want Android to do the searching. I have an API that will search for the results I want and allow me to navigate to the prev/next search result.
My code for the CAB (which is being called correctly) is:
protected ActionMode mActionModeSearch;
private ActionMode.Callback mActionModeSearchCallback = new ActionMode.Callback()
{
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
{
actionMode.getMenuInflater().inflate(R.menu.pdf_menu_search, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.pdf_menu_search_item).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextSubmit(String s)
{
return false;
}
#Override
public boolean onQueryTextChange(String s)
{
return false;
}
});
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
{
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
{
switch(menuItem.getItemId())
{
case R.id.pdf_menu_search_prev:
findPrevSearchResult();
return true;
case R.id.pdf_menu_search_next:
findNextSearchResult();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode)
{
//resetHideToolbarsTimer();
}
};
The CAB menu code is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/group_search_mode">
<item
android:id="#+id/pdf_menu_search_item"
android:title="#string/search"
android:icon="#drawable/ic_pdf_action_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="#+id/pdf_menu_search_prev"
android:title="#string/search_prev"
android:icon="#drawable/ic_pdf_action_search_prev"
app:showAsAction="ifRoom" />
<item
android:id="#+id/pdf_menu_search_next"
android:title="#string/search_next"
android:icon="#drawable/ic_pdf_action_search_next"
app:showAsAction="ifRoom" />
</group>
I also changed my activity definition in AndroidManifest.xml although I'm not sure this is required:
<activity
android:name="com.myapp.myActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value="com.myapp.myActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
When I run the app, the activity load correctly. When the user selects the search option, I load the CAB and it seems to be running fine. I've stepped through the code to make sure the onCreateActionMode is functioning as expected. But, all I see is the icon for search, next and previous, with the 'Done' button. When I touch the 'Search' icon, it doesn't do anything. No text field is created in the CAB for me to enter text. What am I doing wrong????
Screenshot showing the CAB coming up. As you can see, the search icon shows but doesn't do anything when I press it. What I really need is for the search edit box to appear by default.
First, we should set always value for app:showAsAction of all menu items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/group_search_mode">
<item
android:id="#+id/pdf_menu_search_item"
android:icon="#drawable/ic_pdf_action_search"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
<item
android:id="#+id/pdf_menu_search_prev"
android:icon="#drawable/ic_pdf_action_search_prev"
android:title="#string/search_prev"
app:showAsAction="always"/>
<item
android:id="#+id/pdf_menu_search_next"
android:icon="#drawable/ic_pdf_action_search_next"
android:title="#string/search_next"
app:showAsAction="always"/>
</group>
</menu>
Secondary, in this case we don't need to set intent filter for our Activity and searchable info for our SearchView.
Definition of this Activity in AndroidManifest.xml:
<activity
android:name="com.myapp.myActivity"
android:label="#string/app_name" />
ActionMode.Callback implementation:
private ActionMode.Callback mActionModeSearchCallback = new ActionMode.Callback() {
private SearchView mSearchView;
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.home, menu);
mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.pdf_menu_search_item));
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
mSearchView.requestFocus();
return true;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.pdf_menu_search_prev:
findPrevSearchResult();
return true;
case R.id.pdf_menu_search_next:
findNextSearchResult();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
}
};
I just tried this code on three 4.0+ devices and it was fully working. But I didn't test on devises with lower OS versions.
Hope it will be helpful for you.
i have problem searchView in CAB
so i set layout(with edittext) in manu item, it give me same view as searchview
try this i hope it works for you
menu/search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/edt_mySearch"
android:actionLayout="#layout/search_bar"
android:enabled="true"
android:showAsAction="always"
android:visible="true"/>
</menu>
layout/search _bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:layout_alignParentLeft="true"
android:id="#+id/edt_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint=" Search..."
/>
</RelativeLayout>
and myAction mode
mActionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// mode.setTitle("Demo");
getSupportMenuInflater().inflate(R.menu.search, menu);
RelativeLayout m = (RelativeLayout) menu.findItem(
R.id.edt_mySearch).getActionView();
EditText mSearchView = (EditText) m
.findViewById(R.id.edt_search);
mSearchView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
// search here
}
});
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
};

android inline/horizontale/list option menu

I'm trying to create this kind of list menu but with no success:
I would like to use the Android menu button or to press an arrow with "open" or "close" text.
That menu will display a list of options :
[Icon] + Open/close
[Icon] + Take a picture
[Icon] + Import pictures from the gallerie
[Icon] + Delete picture alrealy sent
But, I've got this kind of result, a menu as block options:
I'm using this code from Android developer website :
XML File (/menu/gallerie_menu.xml) :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/open_or_close"
android:icon="#drawable/ic_open"
android:title="#string/open"
android:showAsAction="ifRoom"/>
<item android:id="#+id/take_pic"
android:icon="#drawable/ic_camera"
android:title="#string/take_picture" />
<item android:id="#+id/import_pic"
android:icon="#drawable/ic_import"
android:title="#string/import_picture" />
<item android:id="#+id/delete"
android:icon="#drawable/ic_delete"
android:title="#string/delete_picture" />
</menu>
Java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.gallerie_menu, menu);
return true;
}
Could someone advise me or suggest something to me?
The only option I can suggest is not a particularly straightforward one.
If inside your activity you declare a PopupWindow variable:
private PopupWindow myMenu;
Then in the OnCreate of that activity setup your menu window inflate the layout xml file that is the way you want you menu to look and add eventhandlers as necissary:
View v = getLayoutInflater().inflate(R.layout.test_menu, null, false);
Button b = (Button)v.findViewById(R.id.myFirstMenuOption);
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//do something....
}
});//repeat for rest off menu buttons.
myMenu = new PopupWindow(v, 0, 0, false);
myMenu.setWidth(LayoutParams.MATCH_PARENT);
myMenu.setHeight(LayoutParams.WRAP_CONTENT);
Then override the onKeyDown method of your activity to show the menu in response to the button press:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_MENU)
{
if (myMenu.isShowing())
{
myMenu.dismiss();
}
else
{
ScrollView sv = (ScrollView)findViewById(R.id.mainLayout);
myMenu.showAtLocation(sv, android.view.Gravity.BOTTOM | android.view.Gravity.LEFT, 0, 0);
}
return true; //swallow the event
}
return super.onKeyDown(keyCode, event);
}
The ScrollView here being the topmost view in my activity's layout xml.
There are various options from here on in, for example to code a reusable PopupWindow class of your own, but I leave that to you if this option is what you decide to go with.
Hope this helps you.

Categories

Resources