Menu Button Trouble on Android - java

I am trying to use the menu button on the phone but for some reason it is not working? There is no error but the when I press menu it does nothing. I am running a 2.3.3 Android emulator.
Here is my Activity:
public boolean onCreatOptionsMenu(Menu menu){
MenuInflater Inflater = getMenuInflater();
Inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.MenuClass:
startActivity (new Intent ("com.clayton.calendar.TOCLASS"));
return true;
case R.id.MenuFriends:
startActivity (new Intent ("com.clayton.calendar.TOFRIENDS"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here is my XML:
<item
android:id="#+id/MenuClass"
android:title="Open Classes"/>
<item
android:id="#+id/MenuFriends"
android:title="Open Friends"/>

Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater Inflater = getMenuInflater();
Inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.MenuClass:
startActivity (new Intent ("com.clayton.calendar.TOCLASS"));
return true;
case R.id.MenuFriends:
startActivity (new Intent ("com.clayton.calendar.TOFRIENDS"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
(Don't put this inside the onCreate() method)

Related

error: method does not override or implement a method from a supertype OnCreateOptionsMenu

#Override
public boolean OnCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean OnOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.logout:
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, StartActivity.class));
finish();
return true;
}
I have a error message when i build on AndroidStudio : error: method does not override or implement a method from a supertype
I need help
Those methods have a lower case 'o'
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu); return true; }
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.logout:
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, StartActivity.class)); finish(); return true; }

How to add App Store link to menu item?

I can give link via Button.But I want to add a App store link to "rateus" in menu item.(please see attached image)here is the button code in MainActivity.java.This is not working for menu item.please help me.
//rateus button
android.widget.Button ratebutton = (android.widget.Button) findViewById(R.id.id_rateus);
ratebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(Intent.ACTION_VIEW,
android.net.Uri.parse("market://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
}catch (android.content.ActivityNotFoundException e){
startActivity(new Intent(Intent.ACTION_VIEW,
android.net.Uri.parse("https://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
}
}
});
//end rateus button code
here is my menu item image...
rate us ite menu
here is the code for rate us item
<item
android:id="#+id/id_rateus"
android:orderInCategory="100"
android:title="#string/action_rateus"
android:textAllCaps="false"
app:showAsAction="ifRoom" />
You need to override the menu functions, something like the code below.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds appModels to the action bar if it is present.
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.share:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBodyText = "Check it out. Your message goes here";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBodyText);
startActivity(Intent.createChooser(sharingIntent, "Shearing Option"));
return true;
case R.id.id_rateus:
try {
startActivity(new Intent(Intent.ACTION_VIEW,
android.net.Uri.parse("market://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
}catch (android.content.ActivityNotFoundException e){
startActivity(new Intent(Intent.ACTION_VIEW,
android.net.Uri.parse("https://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
}
return true;
}
return super.onOptionsItemSelected(item);
}
This is how you can handle a menu item click
Inflate the menu file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_file, menu);
return true;
}
Then handle onCLick here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.id_rateus:
//handle onclick event for intent to playstore
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Set Share Intent is never used

I'm trying to add a share button to my actionbar. The icon displays correctly, however when pressed, nothing happens. It says that my setShareIntent is never used. How do I go about using this?
} #Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
First Override method onOptionsItemSelected in your activity.
Here is the code you can try.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_item_share) {
doShare();
}
return super.onOptionsItemSelected(item);
}
private void doShare() {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "I'm Sharing Data");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is my data");
startActivity(Intent.createChooser(shareIntent, "Share myData to.."));
}
Refer the Docs :- Here
Well, I don't see any use of method setShareIntent(). And, In your, it's giving you warning "setShareIntent is never used" because you are not calling that method anywhere in your code.
You should set listener:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// do something
return true;
}
});
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(item);
// Return true to display menu
return true;
}

Android onOptionsItemSelected method of Fragment doesn't get called

there are many questions about the topic but i can't figure out my problem. I have a menu declared in my MainActivity(ActionBarActivity). Now i want to work with MenuItem in onOptionsItemSelected of a Fragment class. Here is my MainActivity methods
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.add_note:
createNewNote();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void createNewNote() {
Intent addIntent = new Intent(MainActivity.this, AddNote.class);
startActivity(addIntent);
}
And Fragment methods
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_note:
Toast.makeText(getActivity(), "Entered into fragment", Toast.LENGTH_LONG).show();
createNewNote();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void createNewNote() {
Intent addIntent = new Intent(getActivity(), AddNote.class);
startActivity(addIntent);
}
Here in MainActivity onOptionsItemSelected get called even in Fragment but doesn't in Fragment as i don't see Toast in Fragment. I think something is missed in my code. Thanks in advance.
inside onOptionsItemSelected() in activity inside your switch after calling createNewNote() instead of returning true return super.onOptionsItemSelected(item)
you must call setHasOptionsMenu() inside onCreate of fragment for menu related method to work.
Your onCreateOptionsMenu() method does not inflate the menu file:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}

Action Bar Menu Items not Working

I have created an action bar with two Menu Items. When I try using an intent for the buttons they don't work. When click on the menu item, web page will be loaded.How to do it.
main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
yourapp:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/camera"
android:icon="#drawable/ic_menu_camera"
android:title="Camera"
yourapp:showAsAction="always"/>
<item
android:id= "#+id/emoticons"
android:icon="#drawable/ic_menu_emoticons"
android:title="Emoticon"
yourapp:showAsAction="always"/>
</menu>
MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean OnOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.camera:
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")));
return true;
case R.id.emoticons:
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Thanks
Your method is overridden with wrong name (thus is not overridden but is a different method) and thus is not called by menu.
public boolean OnOptionsItemSelected(MenuItem item) {
Should be starting with small "o"
#Override
public boolean onOptionsItemSelected(MenuItem item) {
You should use Override annotation to avoid such mistakes.

Categories

Resources