I need help figuring out how to check if a device is equipped with the search hardware button or not. Is this possible?
EDIT: I'm talking about finding out if the device has the search hardware button or not. Simple question. Each android device has a set of hardware buttons; menu, home button, back button, and search button. But some devices is only equipped with some of them, not all.
EDIT 2: The reason why I ask is because I want to have a software button showing in my UI if the device is not equipped with a hardware button. I am using the searchable interface in my activity. I am not following the EditText / TextField approach.
I don't think you need to detect if it actually has a search hardware button. The framework will help you out here (though, I'm sure this process will be simplified once Ice Cream Sandwich is released)
Currently, the only devices that won't have hardware search are Honeycomb tablets. So, by using android:targetSdkVersion="11" (or higher), adding implements OnQueryTextListener to your Fragment or Activity, and then adding something like:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.notebooks_menu, menu);
final SearchView sv = new SearchView(getActivity());
sv.setOnQueryTextListener(this);
menu.findItem(R.id.search_notebooks).setActionView(sv);
}
You will essentially solve the problem. Now, to get it working on pre-Honeycomb devices, you may need to use the compatibility library, or use reflection or some other guards in your code.
EDIT
The Samsung Galaxy S II does not have a dedicated hardware search button, but if you hold the menu button down for a couple of seconds, it will begin acting as a hardware search button.
Better way you can do usign two ways
put the search button and call to onSearchRequested();
Second way on click the editText put android:imeOptions="actionSearch" so you need to
check for key
searchBox.setOnEditorActionListener(new OnEditorActionListener()
{
public boolean onEditorAction(TextView textView, int id,KeyEvent event)
{
if (id == EditorInfo.IME_ACTION_SEARCH)
{
//do what ever you want
}
return true;
}
});
Hope this will help you.
Related
I have a main activity that, among other elements, has a search view on screen. When a client clicks on the search box (and makes an entry) I need to invoke an activity that will provide results to populate other places on that activity screen.
I've looked at a ton of StackOverflow (and other) threads about implementing search view (am using the widget) - most either refer to using the action bar or a separate search activity window. Neither are options in this case.
Without lectures on why I should use the action bar, does anyone have the very basics on using search? In effect, when they enter info in it and click the search icon (magnifying glass) I want to listen for a click/search on that item, call a function to get the contents of that (as in intent.getStringExtra(SearchManager.QUERY);), do some work and go from there. However, I'm not successful in hooking in to when the search icon on the keyboard is pressed and getting the user's search line. Any ideas?
Thanks
Okay, finally figured it out. Oh so often I'll look at something for hours, finally post a question, then figure it out 20 minutes later. Oh, well. Hopefully this helps someone else:
What I've got is an embedded search view in another activity and I just want to hook into it like I would a button. I found lots of stuff regarding action bars and xml intents and all that crap, but what I really wanted turned out to be this:
Make sure to import the listener:
import android.widget.SearchView.OnQueryTextListener;
Say that you implement it:
public class MainActivity extends Activity implements OnQueryTextListener {
Then just stick in the functions:
#Override
public boolean onQueryTextSubmit(String myQuery) {
// "myQuery" represents query as submitted. Go ahead
// and do your searchy stuff here
return (false);
} /* on query text submit */
#Override
public boolean onQueryTextChange(String change)
{
// "change" represents current text string as being typed
return(false);
} /* on query text change */
That's all I wanted :)
I have a DialogDragment which I can show one of two ways:
1) By tapping on a ListView item from its OnItemClickListener
2) By activating a the ListView's context menu and selecting a menu item
Doing #1 works fine under all lifecycle events, but if I invoke it via #2 and I pause the activity (by going Home) and the resuming it via the task switcher, the dialog is no longer displayed. The fragment is there, and I can rotate the device and show the dialog.
I experimented, and if I put the showing of the DialogFragment into a Handler with a delay of at least 1/2 seconds, it works.
The following snippet fails -- it shows the dialog, but then pause/resume hides it:
public boolean onContextItemSelected(android.view.MenuItem item) {
boolean consumed = false;
switch (item.getItemId()) {
case R.id.menu_item:
showMyDialogFragment();
consumed = true;
break;
}
return consumed;
}
So the following snippet works. Pause/resume display the dialog again correctly:
public boolean onContextItemSelected(android.view.MenuItem item) {
boolean consumed = false;
switch (item.getItemId()) {
case R.id.menu_item:
new Handler().postDelayed(new Runnable() {
public void run() {
showMyDialogFragment();
}
}, 300);
consumed = true;
break;
}
return consumed;
}
Replacing the 300ms second delay with a 0ms or 250ms delay causes it to be broken again. This repeatable 100% of the time.
This is a terrible hack obviously, made worse by the constant that's probably depends on the speed of the device.
Anybody know why this is going on and/or offer a better solution? I spent hours on this issue and this is the best I could come up with.
I can reproduce this on Android 4.2 (ARM emulator and Galaxy Nexus). I am unable to reproduce your findings on an x86 4.1 emulator, a Nexus S (4.1), and a Motorola RAZR i (4.0). I can also reproduce the problem by modifying one of my own book samples. I filed an issue on it, using your sample: http://code.google.com/p/android/issues/detail?id=41901 Please add any other information you think would help them diagnose the problem.
With respect to a workaround, if 300ms works, then we have one of those lovely "timing issues", and I haven't the foggiest idea how you'd work around it, short of not using a menu to display it. For example, with your sample app, simply switching to SHOW_AS_ACTION_ALWAYS (and therefore having it be an item on the action bar rather than in an overflow menu) is sufficient to have the DialogFragment behave properly. Hopefully, you'll have a way of adjusting your UI to compensate for this bug, or perhaps somebody will cook up another workaround and post it here or on the issue.
I would recommend destroying the dialog on all pauses and recreate in onResume depending on state regardless of how the dialog is invoked. To do otherwise risks a memory leak if the app is killed by the OS in while paused.
To explicitly answer your question, don't rely on the OS to maintain your app state.
I am new to work with android tablet api level 12. I have created 7 inches avd with 1024*600 screen resolution. I have implemented sample application for get the option menus on my screen and back button. I am unable to see option button and back button on my emulator.
I have implemented for option menu code as follows:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icon: Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
break;
case R.id.text: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
break;
case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
break;
}
return true;
}
You can use keyboard shortcuts to simulate the hardware buttons. There's a table of the shortcuts here. The Back button is simulated by pressing escape, and the menu button is simulated by the Page-Up or F2 keys.
Ordinary options menus will appear regardless of Android version. If you have:
android:targetSdkVersion="11"
then the options menu will appear in the action bar on Android 3.0+ devices. All items in the options menu will be available when clicking the "overflow" button in the upper-right corner. Your options menu will appear normally on Android 1.x and 2.x devices, even if you have android:targetSdkVersion="11" in your manifest element.
If you overrode the MENU button to have other behavior, you will need to create some other trigger for that behavior for API Level 11 and higher. Overriding the MENU button was never a good idea to begin with, and as you can see, it is even less of a good idea now. I know of no way to get a MENU button on an Android 3.0 device if you have
android:targetSdkVersion="11".
As you said "I am unable to see option button and back button on my emulator"
SO its does not make a sense that you have posted your code because you can't changes the hardware as programmatically.
and
for menu u can press F2
for back u can press Esc
Short update: a current list of commands is found at https://developer.android.com/studio/run/emulator.html#tasks.
For the menu, you are supposed to use Ctrl-M (on a Mac: Command-M).
For windows 10 you have to open your emulator, then press Ctrl + M
and the menu will show.
I am using tabs, with Activity groups in each. I want to add an options menu to certain activities but it won't show on any. I have options menus working in other projects with the exact same code, so I can't figure out why they won't show up. This is the code I am using:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Option 1");
menu.add("Option 2");
menu.add("Option 3");
return super.onCreateOptionsMenu(menu);
}
Is there anything wrong with this code or anywhere else I should be looking at that might be blocking this menu from showing when I hit the menu button?
The only thing that would be blocking the menu AFAIK would be if you are overriding onKeyDown. Make sure you aren't overriding that method and thus preventing the menu button from doing what it is supposed to.
I have an Options menu up and running in my Android application and I've overridden the onCreateOptionsMenu, onOptionsItemSelected and onPrepareOptionsMenu methods to customize the menu a little.
My question is related to keeping the Options menu open after the user clicks on a menu item. Basically, I'd like to be able to hide the menu until the user clicks on the device menu key. Once the user clicks on this key, I'd like to be able to hold the menu in place regardless of how many times the user clicks on menu items. If the user wants to hide the Options menu, they'd just need to click on the device menu key again.
Is this type of interaction supported (or even advisable). If this interaction is not supported, any alternative suggestions are more than welcome.
Cheers!
Sean
This will not be possible with onCreateOptionsMenu and the other methods. They always act that way.
But you can do it another way. But there you have to program the whole menu yourself. Basically add the Menu in your layout.xml and let it be hidden (visibility = gone). Then you overwrite the methods onKeyDown. There you check if it is the Menu key. if the menu is not yet open yes, then you show the menu. If it is open already, you hide it.
should not be too difficult. Good thing as well is, that you can make the menu look exactly the way you want and as well let it react the way you want.
For anybody like me, who found this question in google:
To keep menu open after selecting item, you need this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setChecked(!item.isChecked());
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(this));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
return false;
}
Important to return false in onOptionsItemSelected and methods of OnActionExpandListener
This solution from #MayurGajra. More details here: How to hold the overflow menu after I click it?