Android how to open a menu / voice recognition - java

Android Studio here. I am trying to open the menu (the three dot one in the right upper corner) but without clicking it. I am using a voice recognition commands.
I already tried to call it in many ways, e.g. openOptionsMenu();, MapsActivityCurrentPlace.this.openOptionsMenu(); etc. but it didn't work, the menu doesn't open.
// This is my menu - current_place_menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.current_place_menu, menu);
//optionsMenu = menu;
return true;
}
// Later in the code, this is the place I want to open my menu by recognizing the "menu" command
private int voiceR() {
switch (OPERATOR) {
case 'M':
//getMenuInflater();
//openOptionsMenu();
// MapsActivityCurrentPlace.this.openOptionsMenu(); // activity's onCreateOptionsMenu gets called
// optionsMenu.performIdentifierAction(R.id.groupp, 0);
//optionsMenu.performIdentifierAction(R.menu.current_place_menu, 0);
//getMenuInflater();
// MenuInflater inflater = getMenuInflater();
// inflater.inflate(R.menu.current_place_menu, optionsMenu);
//MenuInflater inflater = getMenuInflater();
//inflater.inflate(R.menu.current_place_menu, menu);
//Inflater.performIdentifierAction(R.id.groupp, 0);
//mShowMenu = true;
//invalidateOptionsMenu();
break;
case 'T':
break;
}
return -999;
}
Expected result would be the opened menu with an implemented list. For now, it recognizes the command but doesn't open the menu.
Thanks!

I tried with other stuff and still can't open the Menu.
Maybe my question was a bit twisted but does anyone know how to perform a click on menu without actually clicking it?

Ok, that's what helped finally:
switch (OPERATOR) {
case 'M':
optionsMenu.performIdentifierAction(R.id.groupp, 0);
break;
where groupp is the id of the menu.

Related

mend.findItem explanation and confusion

I'm new to android development and kinda to Java as well.
I'm learning how to add buttons to actionbar - everything is working, but I don't understand few things.
//Showing small icons at actionbar
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
menuItem = menu.findItem(R.id.recBin); // Finds the button in Actionbar and gets the ID
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.recBin: // Actions for delete button
dbHandler.remove(getID());
displayTaskList();
menuItem.setVisible(false); // Hide the button
break;
case R.id.editBtn: // Actions for editbutton
Toast.makeText(MainActivity.this,
"EDIT", Toast.LENGTH_LONG).show();
break;
case R.id.closeBtn:
Toast.makeText(MainActivity.this,
"CLOSE", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
This line: menuItem = menu.findItem(R.id.recBin); Is for what exactly? I took it off and my app crashed, so I understand that it's mandatory. What I don't understand is what icons ID should I put at bold space? It has to be from "menu" right, but does it matter which id I take? For instance, if I took R.id.closeBtn instead of recBin? As long as it's an id from menu, it works?
P.S. I hope this isn't a terrible question [probably is] and I'm sorry if so.
Solution:
Turns out you can simply get ID's. Stupid and simple.
private MenuItem item1, item2, item3;
//Showing small icons at actionbar
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
item1 = menu.findItem(R.id.recBin); // Rec button
item2 = menu.findItem(R.id.editBtn); // Edit button
item3 = menu.findItem(R.id.closeBtn); // Close button
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.recBin: // Actions for delete button
dbHandler.remove(getID());
displayTaskList();
item1.setVisible(false); item2.setVisible(false); item3.setVisible(false); // Hiding all buttons
break;
case R.id.editBtn: // Actions for editbutton
item1.setVisible(false); item2.setVisible(false); item3.setVisible(false); // Hiding all buttons
break;
case R.id.closeBtn:
item1.setVisible(false); item2.setVisible(false); item3.setVisible(false); // Hiding all buttons
break;
default:
break;
}
return true;
}
It's a reference to MenuItem, but it would be easier if you do:
case R.id.recBin: // Actions for delete button
dbHandler.remove(getID());
displayTaskList();
item.setVisible(false); // Hide the button
break;
You can safely remove the reference afterwards

How to call public boolean onPrepareOptionsMenu(Menu menu)

I am using Java and Android Studio. I need to change an image on the actionbar. What I need to do is to change the image I am displaying on the actionbar based on a reading received from a remote sensor. Therefore, I cannot use a clickable method but instead I have some code that activates when the data is received from the sensor. The code then analyzes the data and decides which of several images to display. The program then needs to update the actionbar image. This is the part I need your help on. I think I should use onPrepareOptionsMenu to update the image. However, I cannot seem to get the code correct to call onPrepareOptionsMenu. In particular, onPrepareOptions uses parameters (Menu menu). I cannot seem to specify the second menu properly as it always gives an error. The code below gives:
"required android.view.Menu found int". If I initialize menu with null I get a null reference error. Any suggestions how to correct my code or maybe do something totally different to be able to update the image? Thanks. Below is my java code.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); // Clear the menu first
Log.d(TAG, " Before inflation");
getMenuInflater().inflate(R.menu.gatt_services, menu);
Log.d(TAG, "Before the findItem");
menu.findItem(R.id.menu_batlevel).setIcon(R.drawable.low25);
return super.onPrepareOptionsMenu(menu);
}
Code to call the onPrepareOptionsMenu with:
...
else if (dataType == 2.0) {
Menu menu = (R.menu.gatt_services);
getMenuInflater().inflate(R.menu.gatt_services, menu);
onPrepareOptionsMenu(Menu menu);
...
XML code for the action bar. The image I am trying to change is item id/menu_batlevel:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_refresh"
android:checkable="false"
android:orderInCategory="1"
android:showAsAction="ifRoom"/>
<item android:id="#+id/menu_batlevel"
android:checkable="false"
android:icon="#drawable/discharged"
android:title="BatLev"
android:orderInCategory="2"
android:showAsAction="ifRoom"/>
<item android:id="#+id/menu_connect"
android:title="#string/menu_connect"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_disconnect"
android:title="#string/menu_disconnect"
android:orderInCategory="101"
android:showAsAction="ifRoom|withText"/>
</menu>
My solution is shown below. I put the if statements that update the image into the onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "onCreateOptions Before inflation");
getMenuInflater().inflate(R.menu.gatt_services, menu);
...
if (dataType == 2.0){
if (bat_level < 10) {
MenuItem batItem = menu.findItem(R.id.menu_batlevel);
batItem.setIcon(R.drawable.discharged);
}
...
I added a class:
class VersionHelper
{
static void refreshActionBarMenu(Activity activity)
{
activity.invalidateOptionsMenu();
}
}
I call the class from my program:
...
else if (dataType == 2.0) {
VersionHelper.refreshActionBarMenu(DeviceControlActivity.this);
...
The above code is working. As you can tell, I am new to java so if you see a problem with this code or have a better method, please let me know.
If you want to change the image on an existing menu item, you can save a reference to the menu item when it's created, something like this:
private MenuItem photoMenuItem;
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.menu_end_details, menu);
// find the photo menu and save it
photoMenuItem = menu.findItem(R.id.photoMenuItem);
}
Then when it's time to update the menu item, just call setIcon:
if (photoMenuItem != null)
{
photoMenuItem.setIcon(R.drawable.camera_active);
}
If you want to decide whether or not to show a particular menu item, you can call setVisible instead of setIcon on the items whose presence may change.

Option menu twice option click in one item?

I need a help, how to make one item option menu with different function?
example : when i click first R.id.search then show edittext then when i click R.id.search again then edittext hide.
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_2, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
if (item.getItemId() == R.id.search){
editsearch.setVisibility(View.VISIBLE);
}else{
editsearch.setVisibility(View.GONE);
}
break;
Test the current visibility of the editSearch view
case R.id.search:
if (editsearch.getVisibility() == View.GONE)
editsearch.setVisibility(View.VISIBLE);
else
editsearch.setVisibility(View.GONE);
add global varible:
boolean isFirst=true;
and first time the click fires set it to false something like that:
case R.id.search:
if (isFirst){
isFirst=false;
editsearch.setVisibility(View.VISIBLE);
}else{
editsearch.setVisibility(View.GONE);
}
break;

Android Call Popup Menu

Im working on a simple music player for my intro to Android apps class. I want to be able to add songs to a playlist listed in my context menu. When I click on my addtoplaylist contextmenuitem I want my popup menu to appear. How do I call my popup menu? Also if you have some suggestions on how to populate my popup menu, rather than my for loop, that would be cool too.
I have a contextmenu listener that kind of looks like this.
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
shareIt();
return true;
case R.id.Store:
musicStore();
return true;
case R.id.addtoplaylist:
// open popup menu
return true;
case R.id.snippet:
snippet(tem1);
return true;
default:
return super.onContextItemSelected(item);
}
}
And i have a popup menu that kind of looks like this.
public void showPopup(View v) {
int i = view.getPlaylists().size();
ArrayList<String> playlist = view.getPlaylists();
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
for(int k = 0; k > i;k++){
popup.getMenu().add(playlist.get(k));
}
popup.show();
}
As you can see here PopupMenu
V is just an anchor, so you can pass for instance the ListView which contains your item
Or if you really want the popupmenu to be anchored to the item have a look here
You get your target view like this.
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
View v = info.targetView;
So after call
showPopUp(v);
For your loop, I don't see a far better solution.
the accepted question is fine, but for the for loop part you could use an iterator :)
for (String song : playlist) {
popup.getMenu().add(song);
}

Camera Preview Example

I know this is really basic but it is a error that I cannot find a solution to.
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html
I have 2 errors and I dont know what Im doning wrong.
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate our menu which can gather user input for switching camera
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.camera_menu, menu);
return true;
}
Im getting an error at "menu" in "R.menue..."
error 2:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.switch_cam:
// check for availability of multiple cameras
if (numberOfCameras == 1) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
/**builder.setMessage(this.getString(R.string.camera_alert))
.setNeutralButton("Close", null);*/
AlertDialog alert = builder.create();
alert.show();
return true;
}
Im getting an error on "id" in "case R.id..."
Thanks
First, do you have : <uses-permission android:name="android.permission.CAMERA" />in your AndroidManifest.xml ?
You can try to check a full Camera preview here for Android : Camera Preview Android
Another good website with source files : Using Camera API with SourceFile
You most likely just copied the source file you linked above into your project. You also have to add res/menu/camera_menu.xml. This defines the options menu that comes up when you press the menu button. See the menu doc for more information how that works if you're interested.
If you don't do that the tools notice that you miss a file that you reference in code but that's not actually there, which results the first error. The second error is also caused by this in an indirect way. The missing ID is also created within the menu file.

Categories

Resources