I have create an Android application using NavigationDrawer. When the application is created, there is one icon menu set by default, placed in the left of the toolBar. And my aim is to add another icon placed, for example, in the right of the toolbar. I have tried many tutorials by following them step by step, by I can't achieve it. Could anyone help me?
in activity
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.game_menu, menu)
return true
}
to handle clicks on menu items
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle item selection
return when (item.itemId) {
R.id.new_game -> {
newGame()
true
}
R.id.help -> {
showHelp()
true
}
else -> super.onOptionsItemSelected(item)
}
}
in menu folder game_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="always"/> // this icon will be always shown
<item android:id="#+id/help"
android:icon="#drawable/ic_help"// add you icon image
android:title="#string/help"
android:showAsAction="ifRoom"/> // this icon will be shown if there is space available
</menu>
If you are Using Toolbar set as Actionbar (setSupportActionBar(Toolbar)) or Actionbar alone:
In your Activity:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
getMenuInflater().inflate(R.layout.menu_home, menu)
return true
}
In res -> menu folder add a new file named menu_home (if menu folder does not exists make one):
Add as many items you need to menu_home file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/item_01"
android:title="#string/my_title"
android:icon="#drawable/my_icon"
app:showAsAction="ifRoom"/>
...
</menu>
showAsAction="ifRoom" makes icon available in Toolbar if there is room for it. you also have an option for never as it goes inside the popup menu.
If you are using Toolbar only (without setting as Actionbar):
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:menu="#menu/menu_home"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
You need to create a menu to achieve this task. For example;
res/menu/menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/new_item"
android:icon="#drawable/new_icon"
app:showAsAction="ifRoom">
</item>
</menu>
If you already have a menu then you can continue with it.
After you have created the menu, you need to inflate it.
public class MainActivity 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_main, menu);
return true;
}
}
Refer this for more details about using the menu resource: https://developer.android.com/guide/topics/resources/menu-resource
In your menu.xml add :
<item
android:title="icon"
app:showAsAction="always"
android:id="#+id/menuitem_icon"
android:icon="#drawable/micon"
>
</item>
Then in OnCreateView: setHasOptionsMenu(true); Closing the OncreateView below write this method:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.icon, menu);
...
}
Related
I have a tabbed fragment I need two different actionbars / same action bar that could like the one shown in the image!
Page 1!
Page 2
After watching both images , you don't need two different actionbar instead have one action bar and two different menu.xml file , when fragment 1 is clicked go to the code of firstFragment class :
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_one, menu);//here menu_one.xml is called
return true;
}
and on second fragment call this in your secondFragmentclass
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_two, menu);//here menu_two.xml is called
return true;
}
both menu_one.xml and menu_two.xml will contain different item as per your wish
menu_one.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/someButton"
android:icon="#drawable/some_button"
android:title="#string/some_button"
android:showAsAction="always"/>
</menu>
menu_two.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/someButton2"
android:icon="#drawable/some_button2"
android:title="#string/some_button2"
android:showAsAction="always"/>
</menu>
I'm coming back to my main activity from a fragment and for some logic I have to change the appearence of an icon on the action bar menu.
This is the menu on the action bar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="it.gn.sfa.Main">
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:title="Search" />
<item
android:id="#+id/action_filter"
android:icon="#drawable/ic_action_filter_empty"
android:showAsAction="ifRoom"
android:title="Filter" />
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:showAsAction="ifRoom"
android:title="New" />
</menu>
I have to change the sencond item (the one with id = action_filter).
I've tried different solutions, found on different post. The most rated is
mOptionsMenu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_action_filter));
but seems not to work.
On the other side getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_action_filter)); changes the logo, and I don't want so.
How can i change only the second item on menu?
try this one
mOptionsMenu.findItem(R.id.action_filter).setIcon(R.drawable.ic_action_filter);
Assuming you have it all set up for mOptionsMenu in
private Menu mOptionsMenu;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
// inflating your menu here
mOptionsMenu = menu;
return super.onCreateOptionsMenu(menu);
}
Hope it helps :)
I hope it will be help for you
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>" + "Messages" + "</font>"));
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.messagebar_color)));
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_arrow_black);
You have to modify your onCreateOptionsMenu(Menu menu)
I changed the color of my search bar programmatically. I am posting the code here. Hope it helps.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
menu.getItem(0).setIcon(getTintedDrawable(R.drawable.search, R.color.blue));
return super.onCreateOptionsMenu(menu);
}
Where getTintedDrawable() is a function that i created which returns a drawable. So all you need to do is replace getTintedDrawable(R.drawable.search, R.color.blue) by your drawable.
NOTE: I have used menu.getItem(0) my code since I had only 1 item defined in menu/items.xml. If you have multiple try different values (from 0 to one less than number of menu items). My guess would be that its the number at which the item is defined but I'm not too sure.
I manage to rotate/change the icon this way:
MenuItem item = getToolbar().getMenu().findItem(Menu.FIRST);
<prepare the image view from drawable here>
item.setActionView(imageView);
Seems to work OK.
You could also simply use the item.setIcon() instead.
I want to create a simple option menĂ¹ on the Action Bar (classic three dots). I wrote this part of code but nothing appear:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Even this one but always nothing appear:
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Settings");
return super.onCreateOptionsMenu(menu);
}
There are no three dots on the Action Bar. Is there anything else to write maybe in the onCreate()?
Edit. The menu.xml
<xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Info" android:id="#+id/settings"></item>
</menu>
Add the below line into the menu xml file
<item
android:title="Info"
android:showAsAction="never"
android:id="#+id/settings" >
</item>
check AndroidManifest.xml, set android:targetSdkVersion="10" or lowwer.
At my new application that I created, I got auto generated code for creating 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);
return true;
}
And I added item at menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/item1"></item>
</menu>
But there is no menu button, am I missing something?
EDIT:
In your menu definition you put:
android:showAsAction="never"
Change it to:
android:showAsAction="always"
Check this example, including an icon for the menu:
<item
android:id="#+id/menu_calendar"
android:title="#string/calendar"
android:icon="#drawable/ic_menu_calendar_holo_light"
android:showAsAction="always|withText" />
always means the button will always be displayed. You can replace it by ifRoom if it is a menu such as Settings that should appear in the menu as an option but not displayed all the time.
withtext means that the title of the menu will be displayed beside the icon if there is enough place for it.
Details about all these options are available here.
For the rest, you need to create and show the ActionBar in your onCreate() function:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//you might need this line if you are not using the Holo theme
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main_activity);
ActionBar actionBar = getActionBar();
actionBar.show();
(...)
Check for the import you are using. Check example below
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
You should use Menu and MenuInflater of actionbarsherlock..
Check out this link on implemention of ActionBarSherlock
Try
<item
android:id="#+id/action_settings"
android:showAsAction="always"
android:title="#string/action_settings"/>
<item android:id="#+id/item1"></item>
I've got a SherlockListActivity, and am trying to have a button in the action bar. Here's my menu.xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=">http://schemas.android.com/apk/res/android">
<item
android:id="#+id/add"
android:title="Add"
android:showAsAction="ifRoom" />
</menu>
Here's my java code to show the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
return false;
}
But in my activity, the action bar shows, but with no button, and the menu button on my device does nothing. My device is running 2.3.3.
Thanks.
Try to return true from onCreateOptionsMenu().