android menu code not working - java

I have been trying to figure out why my boolean is not changing when I press the button, when I changed it manually it worked, but it doesn't do any thing. I have tried to follow tutorials to the word but they don't work. Can anybody point out where I am going wrong?
public boolean onOptionsItemSelected(MenuItem menu)
{
MenuItem freeze = (MenuItem)findViewById(R.id.freeze);
// Handle item selection
switch (menu.getItemId()) {
case R.id.freeze:
if (freze == false){
freze = true;
} else {
freze = false;
}
return true;
case R.id.toggleVolCount:
if (toggleVol == true){
toggleVol = false;
} else {
toggleVol = true;
}
return true;
default: return super.onOptionsItemSelected(menu);
}
Thanks for all your help, when I tried the code that was suggested and it didn't work I went back and changed the menu. Previously I had made a button with an onClick to create the menu, when created the icon with code the code that I had previously written worked fine. Hope this helps someone other than me so I don't feel like so much of an idiot.}

In res folder create one folder menu like drawable
Create new xml file optionmenu.xml in that folder.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuitem"
android:title="Prefs">
</item>
<item android:id="#+id/menuitem1"
android:title="Prefs1">
</item>
</menu>
In onCreate method write this code....
setOptionMenu(R.menu.optionmenu);
and in overide method of Menu write this code.....
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) {
case R.id.menuitem:
startActivity(new Intent(this, Prefs.class));
break;
case R.id.menuitem1:
startActivity(new Intent(this, Prefs1.class));
break;
default:
break;
}
return true;
}

Related

Different Link in context menu depending on ImageView Clicked

I have an activity that has crests of different teams, when one is clicked a context menu is displayed with the possibility of opening the teams wikipedia page on the browser. I managed to create a different context menu depending on the ImageView but have no idea how I'm going to pass the link. Any help ?
Context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/link"
android:title="Open Wikipedia Page"
/>
</menu>
Teams Class
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu,v,menuInfo);
MenuInflater inflater = getMenuInflater();
switch (v.getId()){
case R.id.atalanta:
menu.setHeaderTitle("Atalanta");
inflater.inflate(R.menu.context_menu,menu);
break;
case R.id.bologna:
menu.setHeaderTitle("Bologna");
inflater.inflate(R.menu.context_menu,menu);
break;
case R.id.cagliari:
menu.setHeaderTitle("Cagliari");
inflater.inflate(R.menu.context_menu,menu);
break;
case R.id.chievo:
menu.setHeaderTitle("Chievo");
inflater.inflate(R.menu.context_menu,menu);
break;
}
If you're able to set the context menu's header title based on where do your code get in the switch block, you are able also to set a String variable to the link. After the switch, you can create a new Intent, pass the link to it, then start it. All of this should look like below:
String link = "";
//...
switch (v.getId()) {
case R.id.atlanta:
menu.setHeaderTitle("Atalanta");
link = "https://whateveryouwant.com"
inflater.inflate(R.menu.context_menu,menu);
break;
//do this with every case
}
//...
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(link));
startActivity(i);

How to create a simple drop down list/ListView programmatically in android

I want to create a simple drop down list/listview like in below image. It should generate programmatically without using any xml layout.
NOTE : I am not using a spinner in here . Also I want to open it when I click on the ImageView next to the Switch.
I have no idea about this .
Have any ideas ?
not perfect, but it works ;)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
popupMenu.getMenu().add("Edit");
popupMenu.getMenu().add("Delete");
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getTitle().toString()) {
case "Edit" :
//execute "edit" action
break;
case "Delete" :
//execute "delete" action
break;
}
return false;
}
});
popupMenu.show();
}
});
Just try to check and implement it
PopupMenu overflowPopupMenu = new PopupMenu(getContext(), finalOverflow);
overflowPopupMenu.getMenuInflater().inflate(R.menu.popup_overflow_options, overflowPopupMenu.getMenu());
overflowPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
break;
case R.id.delete:
break;
}
return true;
}
});
overflowPopupMenu.show();
popup_overflow_options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/edit"
android:title="#string/edit"/>
<item
android:id="#+id/delete"
android:title="#string/delete"/>
</menu>
I use PopupMenu's for this. See also this guide. The guide explains how to use the PopupMenu with an xml menu resource.
In your case you would attach a click listener to the ImageView. That listener would then create a PopupMenu using the ImageView as an anchor. Like this: PopupMenu popup = new PopupMenu(imageView.getContext(), imageView);
At this point since you need dynamic menu items you have the following options:
You can call PopopMenu.getMenu() and manually populate it with MenuItems
You can create an xml menu resource and then adjust/hide ones that need to be changed

AndroidStudio adding two images to a button

I am using Android Studio and I was wondering is there any way to add two images to a button and only one of them to be visible(active) at a time?
Now I am using the following technique:
In my activity I have 4 buttons and to all of them I have register an View.OnTouchListener and I manage the events in it like this:
private class ButtonOnTouchListener implements View.OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
clickButton(v);
return true;
case MotionEvent.ACTION_UP:
unClickButton(v);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
v.callOnClick();
}
break;
}
// tell the system that we handled the event but a further processing is required
return false;
}
private void clickButton(View button) {
switch (button.getId()) {
case R.id.buttonLogin:
button.setBackgroundResource(R.drawable.activity_main_button_enter_press);
break;
case R.id.buttonRegister:
button.setBackgroundResource(R.drawable.activity_main_button_registration_press);
break;
case R.id.buttonCheckUpdate:
button.setBackgroundResource(R.drawable.activity_main_button_update_check_press);
break;
case R.id.buttonExit:
button.setBackgroundResource(R.drawable.activity_main_button_exit_press);
break;
}
}
private void unClickButton(View button) {
switch (button.getId()) {
case R.id.buttonLogin:
button.setBackgroundResource(R.drawable.activity_main_button_enter_idle);
break;
case R.id.buttonRegister:
button.setBackgroundResource(R.drawable.activity_main_button_registration_idle);
break;
case R.id.buttonCheckUpdate:
button.setBackgroundResource(R.drawable.activity_main_button_update_check_idle);
break;
case R.id.buttonExit:
button.setBackgroundResource(R.drawable.activity_main_button_exit_idle);
break;
}
}
}
So in this way I am making a switch in the event to determine which button is clicked and change the appropriate resource. Which means that for each activity I must create a custom class and do the switches in the events.
Could I make something like the following:
private void ClickButton(View button){
button.setBackgroundResource(button.getResourceImage1);
}
private void unClickButton(View button){
button.setBackgroundResource(button.getResourceImage2);
}
This way I can make a global OnTouchListener and just set the images in the xml file for each button and not to worry about any java code.
I tried to switch between background/foreground images but I couldn't hide the foreground image for some reason.
You Can make a drawable file selector for this purpose
Like this:
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:drawable="#drawable/activity_main_button_enter_press"
android:state_pressed="true" />
<item
android:drawable="#drawable/activity_main_button_enter_press"
android:state_selected="true" />
<item
android:drawable="#drawable/activity_main_button_enter_idle" />
And in your java code on click of button:
btn.setSelected(!btn.isSelected());
And set the selector drawable file to your button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector.xml"
android:text="Click_me"/>
You could create a State List Drawable for each button and implement android:state_pressed.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:drawable="#drawable/activity_main_button_enter_press"
android:state_pressed="true" />
<item
android:drawable="#drawable/activity_main_button_enter_idle" />
</selector>

Android - MenuItem onOptionsItemSelected method not working but returning no errors

I am relatively new to android development and have been trying to learn about MenuItem's but haven't had much success. I am having trouble calling a method when a menuitem is clicked on. Here is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
String selectedMenuIdString = (String) item.getTitleCondensed();
if (selectedMenuIdString.equals("about")) {
doThis(item);
return true;
}
return true;
}
public void doThis(MenuItem item) {
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
It worked once a few days back but it wont now and I have no idea why. Could anyone help?
<item
android:id="#+id/search"
android:orderInCategory="100"
android:title="#string/search"
android:titleCondensed="search"
/>
<item
android:id="#+id/products"
android:orderInCategory="100"
android:title="#string/products"/>
<item
android:id="#+id/contact_us"
android:orderInCategory="100"
android:title="#string/contactus"/>
<item
android:id="#+id/about"
android:orderInCategory="100"
android:title="#string/about"/>
And my strings:
<string name="search">Search</string>
<string name="products">Products</string>
<string name="contactus">Contact Us</string>
<string name="about">About</string>
Do it this way, it's much easier:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getId(); // Retrieve the id of the selected menu item
switch(id) {
case R.id.about: // "#+id/about" is the id of your menu item (menu layout)
doThis(item);
return true;
break;
case ...
}
return super.onOptionsItemSelected(item);
}
No listeners, no extra complexity.
Always compare the items id not the title or something else, like in the other answer.
An other nice way to do it: Try handling the click events through an OnMenuItemClickListener, in the onPrepareOptionsMenu function. Sample code below:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem prefs = menu.findItem(R.id.prefs); // your item identifier
prefs.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
doThis();
return false;
}
});
}
You say
String selectedMenuIdString = (String) item.getTitleCondensed();
if (selectedMenuIdString.equals("about")) {
and the about menu item as it seems here does not have a condensedTitle
<item
android:id="#+id/about"
android:orderInCategory="100"
android:title="#string/about"/>
So modify your code like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.about:{
//do something here
return true;
break;
}
case R.id.search:{
//do something here
return true;
break;
}
case R.id.contact_us:{
//do something here
return true;
break;
}
case R.id.products:{
//do something here
return true;
break;
}
}
return super.onOptionsItemSelected(item);
}

Changing option menu button image programmatically - ResourcesNotFoundException

I have a microphone button in my options menu. When I click it, I want it to display an alternate image( red microphone ).
But it's not working. Fails with ResourcesNotFoundException. The images(png's) are definitely in the appropriate res folders.
Here's my code:
In activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_action_microphone :
MenuItem rec = m_menu.findItem(R.id.ic_action_microphone);
if (m_start) {
rec.setIcon(R.id.ic_action_microphone_active);
startRecording();
} else {
rec.setIcon(R.id.ic_action_microphone);
stopRecording();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/ic_action_microphone"
android:icon="#drawable/ic_action_microphone"
android:title="#string/action_microphone"
android:showAsAction="always"/>
<item android:id="#+id/ic_action_microphone_active"
android:icon="#drawable/ic_action_microphone_active"
android:title="#string/action_microphone"/>
</menu>
What am I doing wrong?
Thanks in advance
Try this instead: Set icon by providing images from the drawable resource folders.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.ic_action_microphone :
MenuItem rec = m_menu.findItem(R.id.ic_action_microphone);
if (m_start) {
rec.setIcon(R.drawable.ic_action_microphone_active);
startRecording();
} else {
rec.setIcon(R.drawable.ic_action_microphone);
stopRecording();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You probably want to set resource from drawable and not ids. I hope this helps you.

Categories

Resources