Here is my menu resource file
<item
android:id="#+id/action_share"
android:title="#string/action_share"
android:orderInCategory="2"
app:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider">
</item>
</menu>
And here is my java code
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main,menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider =(ShareActionProvider)menuItem.getActionProvider();
// setIntent("COOL NIKS");
return super.onCreateOptionsMenu(menu);
}
This is not showing share action provider. How do I fix it?
Here is how full working example should look like.This uses AppCompat library.
You need to add http://schemas.android.com/apk/res-auto schema to your menu items XML.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/action_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always"
android:title="Share" />
</menu>
Now you will have to update your MainActivity to use ShareActionProvider
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Get the menu item.
MenuItem menuItem = menu.findItem(R.id.action_share);
// Get the provider and hold onto it to set/change the share intent.
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
// Set share Intent.
// Note: You can set the share Intent afterwords if you don't want to set it right now.
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}
// Create and return the Share Intent
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "http://google.com");
return shareIntent;
}
// Sets new share Intent.
// Use this method to change or set Share Intent in your Activity Lifecycle.
private void changeShareIntent(Intent shareIntent) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
Related
I made Action Bar Menu App. But If I click menuitem, Toast Message is not shown.
I run the emulator and my phone. But It doesn't work.
when I click menuitem, logcat is show error message:
W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
This is MY CODE:
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#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.mymenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.info:
Toast.makeText(this,"짠",Toast.LENGTH_SHORT);
return true;
case R.id.support:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
res/menu/mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/help"
android:title="help"/>
<item
android:id="#+id/support"
android:title="support"/>
</menu>
you Toast.makeText(this,"짠",Toast.LENGTH_SHORT); should Toast.makeText(this,"짠",Toast.LENGTH_SHORT).show(); invoke show() method.Hope help you.
So I recently trying to add another action button beside the overflow icon on the toolbar:
But by following a tutorial on adding action buttons, I can't get it to show on my toolbar.
Here's my menu/menu_notify.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.jovie.canteen.MenuNotify">
<item
android:id="#+id/action_notify"
android:icon="#drawable/ic_notifications_black_24px"
android:title="#string/action_notify"
app:showAsAction="always" />
</menu>
I separated my supposed new action button on a new class which is MenuNotify from my main class, not sure if that's the correct way of adding new action buttons on the toolbar.
MenuNotify.java:
package com.example.jovie.canteen;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
/**
* Created by Jovie on 1/28/2016.
*/
public class MenuNotify extends AppCompatActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_notify, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
//noinspection SimplifiableIfStatement
case R.id.action_notify:
startActivity(new Intent(this, Home.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Btw, my filter icon button is in my main class.
EDIT:
Thanks g2o for the help!
I separated my supposed new action button on a new class which is MenuNotify from my main class, not sure if that's the correct way of adding new action buttons on the toolbar.
There is no need to create new class to add new action button.
Just add
<item
android:id="#+id/action_notify"
android:icon="#drawable/ic_notifications_black_24px"
android:title="#string/action_notify"
app:showAsAction="always" />
To your menu/main.xml and add
case R.id.action_notify:
startActivity(new Intent(this, Home.class));
return true;
to the switch of your Main class onOptionsItemSelected method.
I am trying to implement my menu in a Utils.java file. Well, so far so good, the menu is appearing as it should although when I click on any menu item, the application stops working. I don't seem to find where the issue is and I would be grateful for your advice. The code for my menu, utils and activity files is as below:
Utils.java
package com.package.name;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class Utils extends Activity{
private Context _context;
public Utils(Context context){
this._context = context;
}
public void menuSwitch(int item) {
switch (item) {
case R.id.menu_one:
startActivity(new Intent(getApplicationContext(),
MainActivity.class));
case R.id.menu_two:
startActivity(new Intent(getApplicationContext(),
MessageActivity.class));
case R.id.menu_three:
startActivity(new Intent(getApplicationContext(),
LocateActivity.class));
case R.id.menu_four:
startActivity(new Intent(this,
AboutActivity.class));
}
}
}
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/menu_one" android:title="#string/menu_one" />
<item android:id="#+id/menu_two" android:title="#string/menu_two" />
<item android:id="#+id/menu_three" android:title="#string/menu_three" />
<item android:id="#+id/menu_four" android:title="#string/menu_four" />
</menu>
menuActivity.java
package com.package.name;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class DatabaseActivity extends ListActivity {
Utils util;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database);
util = new Utils(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
util.menuSwitch(item.getItemId());
return true;
}
}
You should not extend your Utils class. Replace your codes by this one.
public class Utils{
private Context _context;
public Utils(Context context){
this._context = context;
}
public void menuSwitch(int item) {
switch (item) {
case R.id.menu_one:
_context.startActivity(new Intent(_context.getApplicationContext(),
MainActivity.class));
case R.id.menu_two:
_context.startActivity(new Intent(_context.getApplicationContext(),
MessageActivity.class));
case R.id.menu_three:
_context.startActivity(new Intent(_context.getApplicationContext(),
LocateActivity.class));
case R.id.menu_four:
_context.startActivity(new Intent(_context.getApplicationContext(),
AboutActivity.class));
}
}
}
you are using startactivity of Util class , but it should be of the class in which menu is there, thus you should use
_context.startactivity(new Intent(_context,wahteverclass.class))
I'm having some issues with this icon, the thing is that I can do an onLongPress then it shows a toast with the text of this item.
My menu "new_offer_menu.xml"
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/newOffer"
android:icon="#drawable/ic_action_new"
android:title="#string/nueva_lista"
android:alphabeticShortcut="r"
android:orderInCategory="200"
android:showAsAction="ifRoom" />
</menu>
This icon isn't in all of my fragments, but when I go to the correct fragment (where it has to be shown), when I click it doesn't do nothing. In my ListaProductosFragment.java I have :
package info.androidhive.slidingmenu;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ListaProductosFragment extends ListFragment {
private List<ListViewItem> mItems; // ListView items list
private Menu optionsMenu;
public ListaProductosFragment(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
// initialize the items list
mItems = new ArrayList<ListViewItem>();
Resources resources = getResources();
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.tomate_oferta), getString(R.string.aim), getString(R.string.aim_precio), getString(R.string.aim_descuento), getString(R.string.aim_data)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.levadura_oferta), getString(R.string.youtube), getString(R.string.youtube_precio), getString(R.string.youtube_descuento), getString(R.string.youtube_data)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.sopa_oferta), getString(R.string.bebo), getString(R.string.bebo_precio), getString(R.string.bebo_descuento), getString(R.string.aim_data)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.zumo_oferta), getString(R.string.pew), getString(R.string.pew_precio), getString(R.string.pew_descuento), getString(R.string.aim_data)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.tomate_oferta), getString(R.string.aim), getString(R.string.aim_precio), getString(R.string.aim_descuento), getString(R.string.aim_data)));
// initialize and set the list adapter
setListAdapter(new ListViewDemoAdapterListaProductos(getActivity(), mItems));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// remove the dividers from the ListView of the ListFragment
getListView().setDivider(null);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// retrieve theListView item
ListViewItem item = mItems.get(position);
// do something
Toast.makeText(getActivity(), item.title, Toast.LENGTH_SHORT).show();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.new_offer_menu,menu );
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.newOffer:
Toast.makeText(getActivity(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), TipusNouProducte.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I've created a toast to see if the problem is the Intent but it doesn't appears anyways, it only do something when I do a LongPress on this item.
What I'm doing wrong?
I believe you should change
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/newOffer"
android:icon="#drawable/ic_action_new"
android:title="#string/nueva_lista"
android:alphabeticShortcut="r"
android:orderInCategory="200"
android:showAsAction="ifRoom" />
</menu>
to
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourAppName="http://schemas.android.com/apk/res-auto"> <--
<item android:id="#+id/newOffer"
android:icon="#drawable/ic_action_new"
android:title="#string/nueva_lista"
android:alphabeticShortcut="r"
android:orderInCategory="200"
--> yourAppName:showAsAction="ifRoom" />
</menu>
The LongPress behavior, if I'm not mistaken, is a regular behaviour where the system explains to the user the meaning of the menuItem, getting the string from the title tag
android:title="#string/nueva_lista"
You are not managing it directly, it's Android itself that can manage it, giving you the wrong feeling.
Hello i learn to use menu in Android. And i set minimum SDK = 8 and Maximum SDK = 17.
I want when the Application run in minimum SDK the menu show at the bottom. And in latest SDK show menu on top of application bar.
But it was error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidmenu/com.example.androidmenu.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class menu
Here is my menu.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_settings"
android:icon="#drawable/icon_settings"
android:title="Settings"/>
<item
android:id="#+id/menu_save"
android:icon="#drawable/icon_save"
android:title="Save"/>
<item
android:id="#+id/menu_search"
android:icon="#drawable/icon_search"
android:title="Search"/>
<item
android:id="#+id/menu_delete"
android:icon="#drawable/icon_delete"
android:text="Delete"/>
</menu>
And here is my MainActivity.java file
package com.example.androidmenu;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
// Event Handling for individual menu
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch(menuItem.getItemId()) {
case R.id.menu_settings:
Toast.makeText(MainActivity.this, "Settings selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(MainActivity.this, "Save selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(MainActivity.this, "Search selected:", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(MainActivity.this, "Delete selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
}
Any solutions? Thank you :)
change this
menuInflater.inflate(R.layout.menu, menu);
To
menuInflater.inflate(R.menu.menu, menu); // assuming menu.xml in /res/menu/menu.xml
Also do you have a layout like menu.xml if so rename it to avoid confusion to activity_main.xml
setContentView(R.layout.activity_main); // res/layout/activity_main.xml
You can check the docs. has code snippets
http://developer.android.com/guide/topics/ui/menus.html
Change android:text="Delete" to android:title="Delete" in your last menu item.