I am trying to display action overflow menu with icons in my one application. I am not getting icons in menu. My Target SDK = 23 and Minimum SDK = 16.
My Menu.xml is like below
<?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">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_donate"
android:icon="#drawable/new_facebook_page"
android:title="#string/facebook"
app:showAsAction="never" />
<item android:id="#+id/menu_settings"
android:icon="#drawable/new_facebook_page"
android:title="#string/facebook"
app:showAsAction="never" />
<item android:id="#+id/menu_logout"
android:icon="#drawable/new_facebook_page"
android:title="#string/facebook"
app:showAsAction="never" />
</menu>
and Java code is like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
But it does not showing icons in menu. Let me know if anyone can help me to achieve this without changing menu structure.
Thanks
Add below line to your java class inside onCreateOptionsMenu(Menu menu):
menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile)));
Try with below code :
#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_patient_home_screen, menu);
menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile)));
menu.add(0, 2, 2, menuIconWithText(getResources().getDrawable(R.mipmap.add_user), getResources().getString(R.string.action_add_user)));
menu.add(0, 3, 3, menuIconWithText(getResources().getDrawable(R.mipmap.switch_profile), getResources().getString(R.string.action_switch_profile)));
menu.add(0, 4, 4, menuIconWithText(getResources().getDrawable(R.mipmap.logout), getResources().getString(R.string.action_sign_out)));
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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (item.getItemId()) {
case 1:
Toast.makeText(PatientHomeScreen.this, "Profile is Clicked", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(PatientHomeScreen.this, "Add New User is Clicked", Toast.LENGTH_SHORT).show();
return true;
case 3:
Toast.makeText(PatientHomeScreen.this, "Switch Profile is Clicked", Toast.LENGTH_SHORT).show();
return true;
case 4:
Toast.makeText(PatientHomeScreen.this, "Sign Out is Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
private CharSequence menuIconWithText(Drawable r, String title) {
r.setBounds(0, 0, r.getIntrinsicWidth(), r.getIntrinsicHeight());
SpannableString sb = new SpannableString(" " + title);
ImageSpan imageSpan = new ImageSpan(r, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
Or, you can show icon with menu item by :
xml code :
<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/action_alarm"
android:icon="#drawable/ic_accept"
android:orderInCategory="100"
android:title="#string/menu_create_alarm"
android:showAsAction="always|withText"
app:showAsAction="always|withText" />
Java Code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.menu_item, menu);
super.onCreateOptionsMenu(menu, inflater);
}
You can use the custom layout for menu item child
<?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">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_donate"
android:title="#string/facebook"
android:actionLayout="#layout/menu_donate_layout"
app:showAsAction="never" />
<item android:id="#+id/menu_settings"
android:title="#string/facebook"
android:actionLayout="#layout/menu_setting_layout"
app:showAsAction="never" />
<item android:id="#+id/menu_logout"
android:title="#string/facebook"
android:actionLayout="#layout/menu_logut_layout"
app:showAsAction="never" />
</menu>
Create menu_donate_layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/profile_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile" />
</LinearLayout>
Same create the separate layout of each menu item or change text and icon dynamically.
Related
Hi I wouldike to know when I click on my bottomnavigationView how to display my search toolbar ?
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
return true;
case R.id.navigation_loope:
toolbar_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Search"
android:id="#+id/action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
Bottom_nav_view.xml
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_loope"
android:icon="#drawable/ic_loope"
android:title="#string/title_loope"
/>
<item
android:id="#+id/navigation_take_photo"
android:icon="#drawable/ic_take_photo"
android:title="#string/title_take_photo" />
<item
android:id="#+id/navigation_public"
android:icon="#drawable/ic_public"
android:title="#string/title_public" />
</menu>
I can't actually use getMenuInflater().inflate(R.menu.toolbar_menu, menu) I don't know how to implement it for doing the same result when i click on my loope (R.id.navigation_loope)
EDIT : Bottom_nav_view it's for my bottom bar and toolbar_menu for my topbar. If I click on the item navigation_loop, I wouldike to show the search bar on the top.
I wanted to create an option menu which shows up similar to Whatsapp even if the device has its own "hardware" option button. The solution I found online worked just fine until I wanted to add another action to the Bar.
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title=""
android:id="#+id/addViewedMovie"
android:icon="#mipmap/ic_add_to_queue_black_24px"
android:showAsAction="always"
></item>
<item
android:title=""
android:id="#+id/menu_overflow"
android:icon="#mipmap/ic_more_vert_white_48dp"
app:showAsAction="always">
<menu>
<item
android:id="#+id/show_settings"
app:showAsAction="never"
android:title="#string/setttings"/>
<item
android:id="#+id/show_help"
app:showAsAction="never"
android:title="#string/help"/>
<item
android:id="#+id/show_about"
app:showAsAction="never"
android:title="#string/about"/>
</menu>
</item>
</menu>
And this in my MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
With this the app forces the addViewedMovie Action into an Overflow-Menu. Does anyone know how to prevent this?
Try this code and tell me if its work.. you should use app:showAsAction not android:showAsAction also you do not need to use submenu to show 3 dots.. just below code works fine also show 3 dots
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Movie"
android:id="#+id/addViewedMovie"
android:icon="#mipmap/ic_add_to_queue_black_24px"
app:showAsAction="always"/>
<item
android:id="#+id/show_settings"
app:showAsAction="never"
android:title="#string/setttings"/>
<item
android:id="#+id/show_help"
app:showAsAction="never"
android:title="#string/help"/>
<item
android:id="#+id/show_about"
app:showAsAction="never"
android:title="#string/about"/>
</menu>
Create a folder name Menu in your res folder.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:icon="#drawable/ic_launcher"
myapp:showAsAction="ifRoom" />
<item android:id="#+id/abc"
android:title="ABC" />
<item android:id="#+id/abc1"
android:title="ABC1" />
<item android:id="#+id/abc2"
android:title="ABC2" />
</menu>
In java file put this:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
return true;
case R.id.abc:
return true;
case R.id.abc1:
return true;
case R.id.abc2;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Simple.
use app instead of android as app is for design support library.
And so, in Gradle build in dependencies, you must have: compile com.android.support:design..... like that. But, I have seen that you already used it below. So,
Just use: app:showAsAction="never" instead of android:showAsAction="always" in your addViewedMovie item.
Remember: never means it will be on overflow menu item. always means is will show in action bar.
If you are using in a fragment, add below line in the onCreate
setHasOptionsMenu(true);
And two more functions
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
Inflate your views in the above method. Listen to the action in the below one.
#Override public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
I want to added to menu item but a user can choose what he want to see or hide , but I don't know how I can do it . I did this :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="NARZĘDZIA">
<menu>
<item
android:id="#+id/show_point"
android:icon="#drawable/point"
android:title="Pokaż wybraną pozycję obiektu" />
<item
android:id="#+id/sync"
android:icon="#drawable/sync"
android:title="Pojedyńcze odpytanie" />
<item
android:id="#+id/show_chooce"
android:icon="#drawable/show_chooce"
android:title="Pokaż wybrane obiekty" />
<item
android:id="#+id/my_location"
android:icon="#drawable/position2"
android:title="Moja lokalizacja" />
</menu>
</item>
<item android:title="APLIKACJA">
<menu>
<item
android:id="#+id/settigs"
android:icon="#drawable/set"
android:title="Ustawienia aplikacji" />
<item
android:id="#+id/about"
android:icon="#drawable/about"
android:title="O programie" />
</menu>
</item>
</menu>
Now I want to create a Activity in which user can choose what it will be show , but I don't have idea how I can hide this item on menu
you can change java file
//menu intialation
#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);
menu.clear();
MenuItem item =
menu.add(Menu.FIRST, R.id.dashboard_nodifycation, 3, "NODIFICATION");
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.FIRST, R.id.System_log, 4, "LOGOUT");
dbHelper = new Database_for_GCM_data(Activity_Drawer_Dashboard.this);
int unread_count = Integer.parseInt(dbHelper.getUnreadCount());
// calling function for nodification inflater
item.setIcon(buildCounterDrawable(unread_count, R.drawable.white_nodification, getApplicationContext()));
return true;
}
public class MyActivity extends AppCompatActivity {
private Menu mToolbarMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//init menu here
mToolbarMenu = menu;
getMenuInflater().inflate(R.menu.toolbar, menu);
setToolbarMenuItemVisibilties();
return true;
}
private void setToolbarMenuItemVisibilties() {
final MenuItem menuItem1 = mToolbarMenu.findItem(R.id.item1);
final MenuItem menuItem2 = mToolbarMenu.findItem(R.id.item2);
final MenuItem menuItem3 = mToolbarMenu.findItem(R.id.item3);
final MenuItem menuItem4 = mToolbarMenu.findItem(R.id.item4);
//Set visibilities
menuItem1.setVisible(true);
menuItem2.setVisible(true);
menuItem3.setVisible(false);
menuItem4.setVisible(false);
}
}
you can change visibilities any time in activity, but you have to inflate it first
I click an Icon on the action bar to display a popup menu, then I click a popup menu item and a toast message in my Java code shows that it changes state from false to true. The problem is that when the popup menu is opened again no items are clicked. And clicking a popup menu item always shows that it changes state from false to true.
Does anyone have a code solution for this problem?
Thank you for your help.
Java code:
#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_action, 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.
int id = item.getItemId();
if (id == R.id.action_popup) {
View menuItemView = findViewById(R.id.action_popup);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.opt1_name) {
if (item.isChecked()){
item.setChecked(false);
Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
}
else{
item.setChecked(true);
Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
}
}
else if (id == R.id.opt2_date) {
if (item.isChecked()){item.setChecked(false);
Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
}
else{item.setChecked(true);
Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
}
}
return true;
}
});
popupMenu.show();
}
return super.onOptionsItemSelected(item);
}
menu_action.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=".PlayerActivity">
<item android:id="#+id/action_popup" android:title="Sort Popup" android:icon="#drawable/ic_sort"
android:showAsAction="always" />
<item android:id="#+id/action_settings" android:title="Settings"
app:showAsAction="never" />
<item android:id="#+id/action_help" android:title="Help"
app:showAsAction="never" />
</menu>
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/opt1_name"
android:title="Name" />
<item
android:id="#+id/opt2_date"
android:title="Date" />
</group>
</menu>
Check the error if you examine then you must have one listview in your mainlist.xml file with id as #android:id/list
<ListView
android:id="#android:id/list"
android:layout_height="wrap_content"
android:layout_height="fill_parent"/>
I am trying to have a search action on my action bar. I have set an icon and have showAsAction set to always but it is still always displayed in the overflow menu. How can I make it display on the action bar itself? Thanks!
Activity
public class MainActivity extends ActionBarActivity {
#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.
setTitle("Add");
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Settings",
Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.action_search) {
Toast.makeText(getApplicationContext(), "Search",
Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu XML file
<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.testtabs.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_search"
android:showAsAction="always"
android:title="#string/title_search"/>
</menu>
Either remove
android:actionViewClass="android.support.v7.widget.SearchView"
or delete your case statement for action_search in onOptionsItemSelected. Doing either of those will display the icon.
If you want to use SearchView the callback is handled differently than onOptionsItemSelected.
Documentation here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
Just change app:showAsAction instead of android:showAsAction
<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.testtabs.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"`enter code here`
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_search"
app:showAsAction="always"
android:title="#string/title_search"/>
</menu>