I want to place a dropdown list on the right. Can I do this in a standard spinner?
I think Image represented is PopUp menu, To achieve the same see the code below,
To define popup menu, we need to create a new folder menu inside of our project resource directory (res/menu/) and add a new XML (popup_menu.xml) file to build the menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/first_item"
android:title="first" />
<item android:id="#+id/second_item"
android:title="second" />
<item android:id="#+id/third_item"
android:title="third" />
</menu>
In your Activity,
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v, Gravity.RIGHT);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
}
Now Override onMenuItemClick to provide functions to each item.
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.first_item:
// do your code
return true;
case R.id.second_item:
// do your code
return true;
case R.id.third_item:
// do your code
return true;
default:
return false;
}
Related
I implemented sorting functionality for users. I used Image button in my menu file, but it is not showing actual image.
Current Screenshot (See next to search image on toolbar):
Expected Screenshot:
Code:
menu_sort:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/filter"
android:icon="#drawable/ic_sort_black_24dp"
android:title="Filter"
app:actionViewClass="android.widget.ImageButton"
app:showAsAction="always">
</item>
</menu>
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_fragment_search, menu);
getMenuInflater().inflate(R.menu.menu_sort, menu);
ImageButton locButton = (ImageButton) menu.findItem(R.id.filter).getActionView();
locButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showPopup(v);
}
});
return true;
}
public void showPopup(View view) {
PopupMenu popup = new PopupMenu(this, view, R.style.PopupMenu);
popup.getMenuInflater().inflate(R.menu.popup_menu_sort, popup.getMenu());
popup.setOnMenuItemClickListener(this);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.ascending:
sendOrderBy(ASECNDING);
getViewPagerPosition(ASECNDING);
break;
case R.id.descending:
sendOrderBy(DESCNDING);
getViewPagerPosition(DESCNDING);
break;
}
return false;
}
popup_menu_sort:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ascending"
android:title="A-Z"/>
<item
android:id="#+id/descending"
android:title="Z-A"/>
</menu>
Please Note: Please don't tell me to do this any other ways like item inside group or you can do in single file, I know that. Why I change because of Popup style for theme. Yes, it is necessary to build my own Popup menu. It's my requirement and I can not change it. So don't suggest me to do that without own popup menu and use default menu.
Hope following steps will be useful to you.
Remove this line: app:actionViewClass="android.widget.ImageButton"
Modify onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_fragment_search, menu);
getMenuInflater().inflate(R.menu.menu_sort, menu);
return true;
}
Your onOptionItemSelected() will be like:
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.filter) {
View menuItemView = findViewById(R.id.filter); // SAME ID AS MENU ID
showPopup(menuItemView);
}
return true;
}
Hope it will work....
Why you are treating menu item as the image button you can do like this.
menu_sort
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/filter"
android:icon="#drawable/ic_sort_black_24dp"
android:title="Filter"
app:showAsAction="always">
</item>
MainActivity :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_sort, menu);
super.onCreateOptionsMenu(menu, inflater);
menuItem = menu.findItem(R.id.filter);
// count=0;
// notify1();
}
#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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.filter) {
View menuView = findViewById(R.id.filter); // SAME ID AS MENU ID
showPopup(menuView);
}
return super.onOptionsItemSelected(item);
}
use your show pop up like this
this is output
I want to a pop up like this is facebook
Hello Guys,
Above is the image where you can see a popup comes over a button. I tried achieving this using AleartDialog but it opens in center. I want it below that button only.
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_show_options, null);
new AlertDialog.Builder(this)
.setView(view)
.create().show();
Any help would be appreciated. Thanks
Use Popup menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/unfriend"
android:icon="#drawable/ic_mail"
android:title="Unfriend" />
<item android:id="#+id/edit_friend_list"
android:icon="#drawable/ic_upload"
android:title="Edit FriendList"
android:showAsAction="ifRoom" />
</menu>
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_example, popup.getMenu());
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.unfriend:
//
return true;
case R.id.edit_friend_list:
return true;
default:
return false;
}
}
Hope it will help.
for more detail please visit below link.
https://www.tutlane.com/tutorial/android/android-popup-menu-with-examples
https://www.javatpoint.com/android-popup-menu-example
http://www.coderzheaven.com/2013/04/07/create-simple-popup-menu-android/
Use Pop Up menu
open menu on your button click
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mail"
android:icon="#drawable/ic_mail"
android:title="#string/mail" />
<item android:id="#+id/upload"
android:icon="#drawable/ic_upload"
android:title="#string/upload"
android:showAsAction="ifRoom" />
<item android:id="#+id/share"
android:icon="#drawable/ic_share"
android:title="#string/share" />
</menu>
Java Code:
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.archive:
archive(item);
return true;
case R.id.delete:
delete(item);
return true;
default:
return false;
}
}
I am adding the toolbar to linear layout I have created a toolbar without using xml.Everything is working fine but i am not able to add overflow menu .Overflow icon is not showing
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_lnrLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.smartify.customizetoolbardemo.MainActivity"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lnrlayout_toolbar"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Add a menu to your toolbar
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Settings"
android:id="#+id/action_settings"
/>
</menu>
inflate you menu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}
You will get icon of overflow menu then.
Add one button or any view in yourlnrlayout_toolbar
Button btn=(Button)findViewById(R.id.btn_menu);
showPopupMenu(btn);
and call below method for showing overlay menu
public void showPopupMenu(View v){
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.sub_menu, popup.getMenu());
popup.show();
}
is sub_menu is your menu file that you want to show..
Try this
XML code for menu items
create menu resource in res>>menu>>menu_optipn.xml
<item android:id="#+id/new_game"
android:icon="#mipmap/ic_launcher"
android:title="item1"
/>
<item android:id="#+id/help"
android:icon="#mipmap/ic_launcher"
android:title="item2"
android:orderInCategory="0"
/>
MainActivity.Java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_option, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
Toast.makeText(this, "Item 1 is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.help:
Toast.makeText(this, "Item 2 is selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have a menu in the title bar of my Android app, that is not a pop-up Menu. In it I have some items. I want to add a line, or a separator, between just one pair of items in the list. I don't want dividers between all the items, just one pair. I tryed with groups who have different IDs, not worked, also tryed with android:actionlayout, no succes.
My current menu looks like this in design mode. I want to do something like this.
My XML containing my menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="#string/editare_nume_jucatori">
<!-- submeniul meu -->
<menu>
<item
android:id="#+id/M_Jucator1"
android:enabled="true"
android:title="#string/Jucatorul1" />
<item
android:id="#+id/M_Jucator2"
android:enabled="true"
android:title="#string/Jucatorul2" />
</menu>
</item>
<item
android:id="#+id/M_Detalii"
android:icon="#drawable/dice10"
android:title="#string/detalii_text_meniu" />
<item
android:id="#+id/M_Despre_Aplicatie"
android:icon="#drawable/dice10"
android:title="#string/despre_aplicatie" />
<item
android:id="#+id/M_Iesire_Aplicatie"
android:icon="#drawable/m3"
android:title="#string/IesireAplicatie" />
</menu>
My Java code for the menu:
Menu meniu1; //a variable used in my menu
//to show my menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.meniul_meu, menu);
meniu1 = menu; //this is my variable from up declaration
return true;
}
//here execute different actions for items clicked in menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
//click on my item ID from menu and execute
case R.id.M_Jucator1:
...(code code)...
return true;
//click on my item ID from menu and execute
case R.id.M_Jucator2:
..(code code)...
return true;
//click on my item ID from menu and execute
case R.id.M_Detalii:
..(code code)...
return true;
//cand dai click pe iesire din meniu
case R.id.M_Iesire_Aplicatie:
..(code code)..
return true;
default:
return super.onOptionsItemSelected(item);
}
} //finish meniu codes
Group your items in the XML menu, such as:
......
<group>
<items...
</group>
<group>
<items...
</group>
.....
And the in your code use:
final Menu menu = ((Toolbar)this.findViewById(R.id.your_toolbar)).getMenu();
MenuCompat.setGroupDividerEnabled(menu, true);
I have options menu item in my application. Requirement was to add a toggle button to a menu item. Is this possible?
As of this writing there are 3 options.
1) Use app:actionViewClass. Example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Switch!"
app:actionViewClass="android.widget.Switch"
app:showAsAction="always" />
</menu>
2) You can use a custom layout in a menu item to add toggle button. Example:
Create a layout with Switch (alternatively, you may also use ToggleButton), res/layout/menu_switch.xml:
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="64dp" />
And use that layout in menu item:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="#string/switch_button_title"
app:actionLayout="#layout/menu_switch"
app:showAsAction="always" />
</menu>
3) You need to set android:checkable property of the menu to true and control its checked state in runtime. Example:
Menu:
<item
android:id="#+id/checkable_menu"
android:checkable="true"
android:title="#string/checkable" />
Activity:
private boolean isChecked = false;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkable = menu.findItem(R.id.checkable_menu);
checkable.setChecked(isChecked);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.checkable_menu:
isChecked = !item.isChecked();
item.setChecked(isChecked);
return true;
default:
return false;
}
}
Hope this helps.
I had a couple of issues when using a actionViewClass="android.widget.Switch in a menu item. It does actually show a switch on the ToolBar, although for me:
The onOptionsItemSelected() doesn't actually get called when I toggle
the switch.
Using setChecked() on the MenuItem doesn't toggle its state.
Upon debugging and removing the actionViewClass="android.widget.Switch, the onOptionsItemSelected() gets called again.
Not sure what was going on; Maybe that I am using a custom ActionBar that I set using setSupportActionBar(), and using OptionsMenu callbacks within a fragment by enabling it with setHasOptionsMenu(true).
I get this solved by inflating the switch itself, and set OnCheckedChangeListener on it
<item
android:id="#+id/my_switch"
android:title=""
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
app:showAsAction="always" />
And in Fragment
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
MenuItem menuItem = menu.findItem(R.id.my_switch);
SwitchCompat mySwitch = (SwitchCompat) menuItem.getActionView();
mySwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
// Do something when `isChecked` is true or false
});
}
And to toggle the switch programmatically, call setChecked() on the Switch, not on the MenuItem.
use app:actionViewClass
<item android:id="#+id/id"
android:title="#string/string"
app:actionViewClass="android.widget.ToggleButton"
android:orderInCategory="80"
app:showAsAction="always" />
public boolean onPrepareOptionsMenu(final Menu menu) {
if(super.mMapView.isTraffic())
menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_off_48);
else
menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_on_48);
return super.onPrepareOptionsMenu(menu);
}
Do you mean you want to add a toggle button as one of the elements/items appearing in the options menu or add a button to a list item from the menu?
Then you can do it with a custom layout(use a ListView within if you want) and inflating it in the
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
and you can save the values each time the button is toggles.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.btnToggleValue:
// save it here
return true;
case R.id.btnSecond:
...
return true;
default:
return super.onOptionsItemSelected(item);
}
}