I am working on a project that requires that the ShareAction menu be opened on an Onclick method and I can not seem to get it to work. First of I have the ShareAction menu working but only in the settings menu and this is; is what I have when I try to apply that to ImageButton Onclick method
// This is inside the Oncreate Method
share= (ImageButton)findViewById(R.id.share);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShareActionProvider nn= new ShareActionProvider(Login.this);
Intent ShareIntent=new Intent(Intent.ACTION_SEND);
ShareIntent.setAction(Intent.ACTION_SEND);
ShareIntent.setType("text/plain");
ShareIntent.putExtra(Intent.EXTRA_TEXT, "Text To share");
nn.setShareIntent(ShareIntent);
}
});
The code above does not work it does not give any errors or nothing. I have that same code working using the settings menu and doing the following
Menu for activity
<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=".Login">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="200" android:icon="#drawable/overflow" app:showAsAction="never" />
<item android:id="#+id/action_share" android:title="#string/action_share" android:orderInCategory="100" app:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider"
/>
</menu>
then simply add it to the 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.menu_login, menu);
MenuItem shareItem= menu.findItem(R.id.action_share);
ShareActionProvider mShare= (ShareActionProvider)shareItem.getActionProvider();
Intent ShareIntent=new Intent(Intent.ACTION_SEND);
ShareIntent.setAction(Intent.ACTION_SEND);
ShareIntent.setType("text/plain");
ShareIntent.putExtra(Intent.EXTRA_TEXT, "Text To share");
mShare.setShareIntent(ShareIntent);
return true;
}
What am I missing or doing wrong that is not letting the menu open in Oncreate?
I am working on a project that requires that the ShareAction menu be opened on an Onclick method and I can not seem to get it to work
This does not make much sense. ShareActionProvider is an action bar item. It has its own "button" to allow the user to share content, based on a configured Intent indicating what sort of sharing is to be done (e.g., MIME type).
So, there are three possibilities here, that I can see:
Just use the ShareActionProvider and get rid of this ImageButton.
Get rid of the ShareActionProvider, and do something else to show a list of sharing actions in onClick() of the ImageButton. For example, you could wrap the Intent in Intent.createChooser() and call startActivity() on that Intent from onClick() -- if there are two or more activities that match the Intent, the user will get the chooser "menu".
Fork ShareActionProvider to have it expose some sort of open() method that allows you to activate it from onClick() of your ImageButton. I cannot envision a situation in which this makes any sense (why would clicking a button over here pop open a drop-down menu over there?).
Related
Well i read how change icons colors or add a button on navigation system bar like camera button.
But i want to know if i can add a button that open a especific apliccation like whatsApp or anyone installed like a shortcut.
Very simple, in an activity, override onOptionsItemSelected method. Then test the menu item which is clicked and open the application you want to using an Intent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.MENU_ID:
Intent i = new Intent();
i.setFlags(...);
startActivity(i);
break;
}
return super.onOptionsItemSelected(item);
}
All what you need you can find here
You can open another application from your onClick or onOptionsItemSelected event.
After couple of hours of investigations and trying to find a solution I’ve decided to write here my problem in idea that maybe someone else face it also and found a fix for it.
I have a application with a menu, one of the options being a Share option, declare like this in menu xml file:
<item android:id="#+id/menu_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="Share"/>
In onOptionsItemSelected method I create and set the share intent:
private ShareActionProvider shareActionProvider;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_share:
MenuItem actionItem = optionsMenu.findItem(R.id.menu_share);
shareActionProvider = (ShareActionProvider) actionItem.getActionProvider();
shareActionProvider.setShareIntent(createShareIntent());
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When pressing the Share menu option the list with apps to share is displayed.
What I want is when I rotate the device that list to be visible, but instead is disappearing. To mention that I can’t use android:configChanges in order to prevent the activity from being recreated.
I wanted first to see how can I open that share apps list programmatically.
a). Calling the below method nothing visual happens; it just updates the ShareActionProvider
public void showShareItemList() {
MenuItem actionItem = optionsMenu.findItem(R.id.menu_share);
shareActionProvider = (ShareActionProvider) actionItem.getActionProvider();
shareActionProvider.setShareIntent(createShareIntent());
}
b). Calling the below method opens a new activity – but not a menu options list like in the picture above.
public void showShareItemList2(){
Intent sharingIntent = new Intent();
sharingIntent.setAction(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
startActivity(Intent.createChooser(sharingIntent, "Share"));
}
Result:
c).The below method just opens the menu option:
public void showShareItemList3() {
openOptionsMenu(); // programmatically open the options menu
}
So, is there a way to open the share item list of applications programmatically ? Or at least is there a way to press menu key programmatically ( = programatically select a menu item)?
Another issue is how can I know if the share list is visible on orientation changed?
For this I’m using a boolean variable private boolean isShareOptionMenuListVisible = false; which I save it on method onSaveInstanceState() and restored it in onCreate() method.
I make isShareOptionMenuListVisible = true when the Share item is pressed, but I could not find a way to make it to false when is not visible anymore.
Using the override method onOptionsMenuClosed is not helping me because this “is called whenever the options menu is being closed” (this is in documentation, even if I can say this is not real because in my app is never called), and I want to know when the Share apps options menu is being closed.
Here is a sample documented project which demonstrates all the above issues: project link.
Any help is welcome! Thanks.
If you're using SDK 11 or above, you can register to receive a callback when the (or any) action item's menu is showing, including your ShareActionProvider:
getActionBar().addOnMenuVisibilityListener(new ActionBar.OnMenuVisibilityListener() {
#Override
public void onMenuVisibilityChanged(boolean isVisible) {
...
}
});
This method is also supported in ActionBarSherlock, but for some reason the listener is never actually called.
Apologies for what I am sure is a common question, but after Googling for a while it seems I don't know the proper Android term for what I want and so am stuck.
I would like to know how to work with the main pop-up menu associated with a view...that is, if you are sitting at some activity doing nothing and press the menu key on the phone, how to work with that menu that commonly opens on many apps containing "Settings", "Exit", etc.
I am not sure what this is called, but if someone can point me to the appropriate portion of the SDK, that would be appreciated. Also, if someone knows how to work this menu within the context of the Eclipse ADT plugin, that'd be great, too.
Cheers.
As CaseyB says, it's just called a menu.
For a quick start on using it, you could create a subfolder in the res folder of an eclipse project and call it "menu" add in some xml for the view, and the call a MenuInflator from the onCreateOptionsMenu function.
Ok, that might be a bit confusing, so i've included some sample code that should get you started. This code should make it so when you press the menu button you can select one of two new activities to get loaded.
In Main Activity add:
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
// Define whatever other activities you can to load in here or whatever.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.info:
startActivity(new Intent(this, Info.class));
break;
case R.id.logs:
startActivity(new Intent(this, Logs.class));
break;
}
return true;
}
menu.xml file: Link to whatever icon images you want.
<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<!-- Copyright © 2012 Tutela Technologies Ltd. -->
<!-- All Rights Reserved. -->
<!-- -->
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/info"
android:icon="#drawable/ic_menu_info_details"
android:title="Info"></item>
<item
android:id="#+id/logs"
android:icon="#drawable/ic_menu_report_image"
android:title="Logs"></item>
</menu>
Then in your AndroidManifest.xml make sure you remember to add in the new activities.
<activity
android:name="com.whatever.Gui.Info"
android:label="#string/appTitle">
</activity>
<activity
android:name="com.whatever.Gui.Logs"
android:label="#string/appTitle">
</activity>
Note: in this example the labels are defined in strings.xml
<string name="appTitle">Your App name</string>
Hope this helps you!
Cheers
It's just called the menu. Here is a tutorial to get you started. Things get a little weird in 3.0+ but once you get the basics down it shouldn't be too hard to pick up.
On versions prior to 3.0, these are menus. Menus are being replaced by the ActionBar in the more recent versions of Android.
About menus: http://developer.android.com/guide/topics/ui/menus.html
Tutorial about menus: http://www.androidhive.info/2011/09/how-to-create-android-menus/
Look at the example:
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.SettingsMenuItem:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In xml: (in /res/menu/menu.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/SettingsMenuItem"
android:icon="#drawable/settings"
android:title="#string/settings"/>
</menu>
My activity has a menu that inflates on my emulator when I use its hardware "Menu" button, however when I run the app on my actual phone (Galaxy Nexus with 4.0.1) I cant work out a way to make the "3 dot" style menu button appear as it does in some apps. The way that I've implemented my menu is:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.wcoptions, menu);
return (super.onCreateOptionsMenu(menu));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.ooptions:
// open preferences activity
startActivity(new Intent(this, WorkCalcPreferenceActivity.class));
break;
case R.id.oquit:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
Also in my manifest file I have the minSDK set as 8 and the targetSDK set as 15.
This is my first app and Im still learning how to implement all these things so any help would be greatly appreciated :)
Edit: Here is menu.wcoptions
<item
android:id="#+id/ooptions"
android:icon="#drawable/ic_menu_settings"
android:title="Options"/>
<item
android:id="#+id/oquit"
android:icon="#drawable/ic_menu_exit"
android:title="Quit"/>
If you target Honeycomb or above you won't get the menu button. The menu button is only there for legacy purposes, and you should instead make use of the action bar or some other means of bringing up the menu.
In my app i am working with actionbar library,(Bcz i am using api 2.2)
https://github.com/johannilsson/android-actionbar
I have 6 classes in my app. Each class contains different action items in actionbar. I want for different different action it will work differently.
But in actionbar library there is single click listener for all action.
But I want to call clik listener from my activity.
How can i do that?
For each activity you can use code like below. You may wish to use a superclass to avoid duplication of code. For each button on the action bar you need to create an intent, and then specify that intent as the action for the button.
// Set the Action Bar title
actionBar = (ActionBar)findViewById(R.id.actionbar);
actionBar.setTitle(R.string.app_name);
// Set up the Action Bar home/icon button
actionBar.setHomeLogo(R.drawable.icon);
Intent homeIntent = new Intent(Intent.ACTION_VIEW);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
homeIntent.setClassName(context, TaxiMap.class.getName());
actionBar.setHomeAction(new ActionBar.IntentAction(this, homeIntent, R.drawable.icon));
// Add an Action Bar button
Intent actionIntent = new Intent(Intent.ACTION_VIEW);
actionIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
actionIntent.setClassName(context, TaxiMap.class.getName());
actionBar.addAction(new ActionBar.IntentAction(this, actionIntent, R.drawable.ic_action_icon));