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.
Related
I have created a Toolbar for an activity in Android in Xamarin. I have enabled the back/home button with SupportActionBar.SetDisplayHomeAsUpEnabled(true);. I am trying to capture the event of pressing the back/home button with the following code, as instructed by this and many other stackoverflow posts:
public override bool OnOptionsItemSelected(IMenuItem item)
{
System.Diagnostics.Debug.WriteLine("OnOptionsItemSelected() called: " + item.ItemId);
switch (item.ItemId)
{
case Resource.Id.home:
System.Diagnostics.Debug.WriteLine("Home button pressed");
Finish();
return base.OnOptionsItemSelected(item);
default:
return base.OnOptionsItemSelected(item);
}
}
When I press the back button, OnOptionsItemSelected is called, but item.ItemId is not equal to Resource.Id.home. The former is 16908332 (tested on two different devices) but the latter is 2131492903. How can I capture the home/back button from the toolbar in Xamarin? One possible option is to hardcode the back button ID as 16908332, but I do not know if that number will stay the same permanently.
You are using the wrong resource, you want the one from the Android space:
Android.Resource.Id.Home
The right one to use is
case Android.Resource.Id.Home:
Finish();
break;
I have an application that has an options menu in fragments that works perfectly in 4.0. But when i test it in 2.3 i get a weird issues. When i click on an option menu it doesn't cause any events to trigger. But if i do anything else after like click back, click a button, open another activity. The event that was supposed to happen when i click the action button triggers.
I have setHasOptionsMenu(true); in my fragments the only thing different that maybe the source of the issue is that my fragments inherit from a base fragment that has the logic for the action buttons.
---Base Class ---
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if(menu != null)menu.clear();
inflater.inflate(R.menu.msg_menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
//Logic Here
}
}
To make this issue more strange sometimes the button in the actionbar will behave normally but i can't figure out why.
EDIT: I found that if i press a menu button then open and shut the navigation drawer. The button then work correctly after but still haven't found a work around
I'm having an issue connected with actions oferflow. On mdpi device with Android 2.3 on board, when I put two actions on the action bar and then add a ShareActionProvider it overflows to be under hardware menu button instead the overflow icon.
What is happening is partially acceptable, but the ShareActionProvider does not work at all under those circumstances. When I roll over the menu panel and click nothing happens.
Oh, I'm using AB Sherlock 4.2.0.
Do you know any workaround?
Thanks!
Current workround for me is to handle generic onOptionsItemSelected for provider's ID and do as follows:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
[...]
case R.id.menu_item_share:
startActivity(Intent.createChooser(mShareIntent, getString(R.string.share_title)));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But it'd be nice to see this fixed. :)
I have been used this example, in this example you have to add /libs/android-support-v4.jar library file and and put a break point on public boolean onMenuItemSelected(int featureId, MenuItem item) method in /src/android/support/v4/app/Watson.java
line no. 115
debug source code when you use app for lower version api where "Menu" button exist.
Hope you will be able to find problem.
I have a TabActivity with four tabs. When I set android:targetSdkVersion="15" the onCreateOptionsMenu method is not called on any of the tab activities when testing on a Nexus 7.
It works correctly with android:targetSdkVersion="10".
With android:targetSdkVersion="15" it works correctly when the activities are not in a TabActivity and when tested on a phone (Evo).
Here is the code for onCreateOptionsMenu.
public boolean onCreateOptionsMenu(Menu menu) {
Log.i("Test","Base In create option menu");
if( menuId != null ) {
new MenuInflater(this).inflate(menuId,menu);
}
return super.onCreateOptionsMenu(menu);
}
Sorry but this is an easy one. According to Google:
http://developer.android.com/guide/topics/ui/menus.html
So, if you set the target SDK lower, you can show the deprecated options bar. For the newer SDKs you need to use an action bar or some other form of navigation. Basically Google decided that not all devices would have a "menu" button.
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.