creating overflow option (not in action bar) - android

How to create overflow menu in application. but I don't want the overflow menu in action bar. But i want it to be attached any where in the application. I have seen one music app in which something similar is done. I want ot include it in my application. I am attaching image fro the same.
Clicking on the image.. on drop down menu will come similar to overflow menu in action bar. Can anybody tell me how to do it. Any help will be appreciated.

User117 comment was helpful and worked for me.
You can use this class and create overflow menu fron any widget.
Refer Pop up menu link.
private void setOverflowMenu(View view)
{
popup = new PopupMenu(getActivity(), view.findViewById(R.id.image_overflow));
popup.getMenu().add(Menu.NONE, OPTION1, Menu.NONE, "My option 1");
popup.getMenu().add(Menu.NONE, OPTION2, Menu.NONE, "My option 2");
popup.getMenu().add(Menu.NONE, OPTION3, Menu.NONE, "My option 3");
popup.setOnMenuItemClickListener(this);
}
Called from fragment thus passed view as parameter. If called from activity you can directly call this method. view.findViewById(R.id.image_overflow) is the image from which I need to attach the menu. then Implemented PopupMenu.OnMenuItemClickListener in my class which force me to implement below method
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch (item.getItemId())
{
case OPTION1:
Toast.makeText(getActivity(), "OPTION1 clicked", Toast.LENGTH_LONG).show();
break;
case OPTION2:
Toast.makeText(getActivity(), "OPTION2 clicked", Toast.LENGTH_LONG).show();
break;
case OPTION3:
Toast.makeText(getActivity(), "OPTION3 clicked", Toast.LENGTH_LONG).show();
break;
}
return false;
}
That it. every thing works. AND don't forget to add click listener for widget(imageview in my case).

Related

Detecting click in overflow menu in Android?

Is there any option to detect a click in overflow menu?
I don't want to detect clicking in particular items.
As it was posted in this other question, you can do the following:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(featureId == AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR){
Toast.makeText(this, "OPEN", Toast.LENGTH_SHORT).show();
}
return super.onMenuOpened(featureId, menu);
}
#Override
public void onPanelClosed(int featureId, Menu menu) {
if(featureId == AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR){
Toast.makeText(this, "CLOSE", Toast.LENGTH_SHORT).show();
}
super.onPanelClosed(featureId, menu);
}
If you are inheriting from AppCompat. If not, the right constant to be used is Window.FEATURE_ACTION_BAR
Are you simply trying to detect when the options menu itself is made visible? If so, I believe the "onPrepareOptionsMenu" method on Activity is your best bet.
Prepare the Screen's standard options menu to be displayed. This is
called right before the menu is shown, every time it is shown. You can
use this method to efficiently enable/disable items or otherwise
dynamically modify the contents.
The default implementation updates the system menu items based on the
activity's state. Deriving classes should always call through to the
base class implementation.
http://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu)

ActionProvider. Change text in submenu and know when its pressed

I use this example
http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider
But, i want to now, if its possible change the text "see all" for other text, for example, my gallery app, have the text in spanish "ver todo", if possible to change?
And.. if possible know when the share button in the action bar (in my case actionbarsherlock) are press?
i see "However, if the action provider provides a submenu of actions, then your activity does not receive a call to onOptionsItemSelected() when the user opens the list or selects one of the submenu items" but..no other way to know it? I want that when the user press that "button" make one accion also show the list of app to share.
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater= getSupportMenuInflater();
inflater.inflate(R.menu.menu_resultados, menu);
MenuItem item = menu.findItem(R.id.menu_compartir);
mShareActionProvider =(com.actionbarsherlock.widget.ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent ()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_EMAIL,"TestText");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "TestSubject");
return shareIntent;
}
Try setting setOnMenuItemClickListener for ex:
MenuItem item = menu.findItem(R.id.menu_compartir);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
});
You can change the string at actionbarsherlock 's project, by changing lines at res/values/abs__strings, or you could create a folder res/values-es, copy there abs__strings, and edit the lines there. That way, you would have both english and spanish strings on your code, depending on device's language. The line you're looking for in abs__strings is called abs__activity_chooser_view_see_all.
Just a tip, try using the search function in Eclipse for these string changing next time. I found this out by searching "See all...".
About knowing when the button has been pressed, I couldn't figure it out. I just tested that onOptionsItemSelected() does not work, just as you said...

Can't select item in list

I wanted to create application where items in list ll be editting when you select item and press in menu "Edit", but items in list dont selecting...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
final long id = this.getSelectedItemId();
switch (item.getItemId()) {
case IDM_EDIT:
if (id > 0) {
CallEditContactDialog(id);
}
else {
Toast.makeText(this, R.string.toast_notify, Toast.LENGTH_SHORT)
.show();
}
break;
}
return(super.onOptionsItemSelected(item));
}
Maybe it is working for android 2nd, but i make it for third too and can test it just on tab 8.9 android 3.2...
Sorry for my english and knowledge in android.
Am i right that you have some items in the list in activity, you want to pres on one of it then press menu button and press Edit ?
I think you have to save id when press on the item in list and then perform change action with this id onOptionsItemSelected...

how to make android menu , and how to open new screen when choose an item in this menu

i'am new in android programming
so wanna to ask for something
i made a menu and respond to action of menu with this code
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.main:
setContentView(R.layout.test2);
return true;
case R.id.news:
setContentView(R.layout.test2);
return true;
case R.id.feature:
setContentView(R.layout.test1);
return true;
default:
return super.onOptionsItemSelected(item);
}
all of this items turn me to a new layout , what i need is , when select an item i can make a new codes and programs
I would suggest to read the Android fundamentals article (especially the ActivatingComponents section).
A menu should start a new activity. If you want to change the content of one activity I would recommend to use tabs (TabActivity).
So here's what to do (or at least what I suggest):
Create a new activity (e.g. MyTaskActivity). Add it to your AndroidManifest.xml in the application tag:
<activity android:name="your.pacakge.MyTaskActivity" />
And call this in onOptionsItemSelected:
startActivity(new Intent(this, MyTaskActivity.class));

Mixing menuItem.setIntent with onOptionsItemSelected doesn't work

While extending a sample Android activity that fires some other activities from its menu, I came to have some menu items handled within onOptionsItemSelected, and some menu items (that just fired intents) handled by calling setIntent within onCreateOptionsMenu.
Basically something like:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_ID_1, Menu.NONE, R.string.menu_text_1);
menu.add(0, MENU_ID_2, Menu.NONE, R.string.menu_text_2);
menu.add(0, MENU_ID_3, Menu.NONE, R.string.menu_text_3).
setIntent(new Intent(this, MyActivity_3.class));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case (MENU_ID_1):
// Process menu command 1 ...
return true;
case (MENU_ID_2):
// Process menu command 2 ...
// E.g. also fire Intent for MyActivity_2
return true;
default:
return false;
}
}
Apparently, in this situation the Intent set on MENU_ID_3 is never fired, or anyway the related activity is never started.
Android javadoc at some point goes like <<[if you set an intent on a menu item] and nothing else handles the item, then the default behavior will be to [start the activity with the intent]>>.
What does it actually mean "and nothing else handles the item"?
Is it enough to return false from onOptionsItemSelected?
I also tried not to call super.onOptionsItemSelected(item) at the beginning and only invoke it in the default switch case, but I had same results.
Does anyone have any suggestion?
Does Android allow to mix the two type of handling?
Thanks for your time everyone.
Ok. The solution was dumb enough. The destination activity name (say MyActivity_3 in the example) was mispelled in the manifest.
I changed the 3rd menu item handling to the classic switch logic in onOptionsItemSelected and I got an ActivityNotFoundException exception in Eclipse debugger.
By handling the menu item in the "setIntent way", no exception was thrown. Although if I looked at the LogCat, I could have spotted a MenuItemImpl: Can't find activity to handle intent; ignoring.

Categories

Resources