I want to customize my Actionbar. I want to add an ImageButton in Actionbar, which when clicked goes to another activity. My code is as below. I don't know to proceed forward. can anyone suggest me step by step what to do.
final ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.blue));
To add a button to action bar, locate the onCreateOptionsMenu method in the activity with the action bar that you want to add the button to.
Next go to res>menu>main and add the item based on this example:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings" //<-- id of the view
android:orderInCategory="100" //<-- not important for the moment (importance)
android:showAsAction="never" //<-- this will always put it in overflow. set always to show as action
android:icon="#drawable/ic_launcher"/> //<-- Icon to display in action bar
android:title="#string/action_settings"/> //<-- not really important
</menu>
next, we will want to listen for item clicks, so add this method to your activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_settings was selected
case R.id.action_settings:
// this is where you put your own code to do what you want.
break;
default:
break;
}
return true;
}
And there ya go!
You should add a custom view to you action bar inside your activity.
See this : https://stackoverflow.com/a/16029214/713778
Related
I am having an issue where i have 2 items in my action bar (one 'refresh' button and one 'Save' Button, but for some reason they do not show, instead they are nested inside an options menu (3 dots). Would anyone know how to remove the 3 dots menu and display my 2 items? I have tried many things but ultimately I just end up removing all three items. Thanks in advance.
Here is my code
add_event_action.xml (this is my menu xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_refresh"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="#+id/action_save"
android:showAsAction="always"
android:title="#string/save"/>
</menu>
Here is my Java class
public class RandomActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_list);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_event_action, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_refresh:
Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
.show();
break;
// action with ID action_settings was selected
case R.id.action_save:
Toast.makeText(this, "Save selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
}
If I understood correctly, you need two menu buttons in your toolbar.
This works for me, place it in your menu.xml:
<item
android:id="#+id/done"
android:title="#string/done"
app:showAsAction="always|withText"/>
Try to use app:showAsAction instead of android:showAsAction
To expand on the other answers somewhat:
When your activity extends from AppCompatActivity, your menu items should use app:showAsAction. When your activity does not (that is, when you're not using the AppCompat support libraries), your menu items should use android:showAsAction.
Documentation: http://tools.android.com/tips/lint-checks
AppCompatResource
Summary: Menu namespace
Priority: 5 / 10
Severity: Error
Category: Correctness
When using the appcompat library, menu resources should refer to the
showAsAction in the app: namespace, not the android: namespace.
Similarly, when not using the appcompat library, you should be using the android:showAsAction attribute.
I have no custom bar, I just set a delete icon to the actionbar, but now I need to set OnClickListener to this icon. how can I do that without custom bar is this possible. Also the icon not apears on the left side, can I set it on the rightside?
activity.getSupportActionBar().setIcon(R.drawable.ic_delete);
I use Navigation Drawer, when I use custom bar the toggle icon despairs.
Looks like you want to set the ActionBar's home button as your delete button. I would suggest not to do it as that is a bad design decision in my opinion. And moreover you also want to show the button on the right hand side which can be done in a much intuitive manner by using a menu.
Please have a look at the official documentaion for adding ActionBar actions here
Basically you need to add an XML menu resource and declare your actions like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_delete"
android:icon="#drawable/ic_delete"
android:title="#string/action_delete"
app:showAsAction="always"/>
</menu>
Then in your Activity override the OnOptionsItemSelected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_delete:
// Do your stuff here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
actionBar.setDisplayHomeAsUpEnabled(true);
Then you need to override activity method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onIconClicked();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
To create an item in the right side, you need to create custom menu, actually, it is simple.
Here is example how to do this
I would like to add a back arrow button to the right side of action bar.
I have the following code, but it adds the back button to the the left side of the action bar.
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
What you did is enabled the action-bar's back functionality on click/touch event. If you want a button at the right of the action bar, the best/easy thing you can do is to add an overflow menu, for which you can set-up any icon you want.
There are lots of tutorials on how to do this (for ex. http://www.techotopia.com/index.php/Creating_and_Managing_Overflow_Menus_on_Android).
Essential points are as follows.
Create the layout/items for the overflow menu (filename should match with the one in the 2nd step).
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:icon="#drawable/overflow_menu_icon"
android:title="#string/menu_settings" />
</menu>
Init the overflow inside the onCreateOptionsMenu() function, where activity_menu_app is the name of the .xml file created in the previous step.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_menu_app, menu);
return true;
}
Catch the touch events of the menu items inside onOptionsItemSelected() function.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
// do your stuff here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Please go through -Back button is not working properly in Right side of action bar
If you want to add back arrow button on the right side - Toolbar is the best option to add anything in the actionbar or Topbar.
First, you need to initialize the toolbar :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
then call the back button from actionBar :
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Finally, over right this method,
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
Solution collected from,this
Link
I have an action bar with a drawer on the left. I want to add another menu on the right side of the action bar. It can be three dots or a button or anything else. Is there any way to do this?
Thanks :)
For achieving 3 dots menu in your actionBar, in your activity (which extends AppCompatActivity or ActionBarActivity), you override the creation of options menu like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
Where this R.menu.your_menuis a resource item present in your res/menu folder. One sample menu resource file content
<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="your_package.your_activity">
<item
android:id="#+id/action_edit"
android:orderInCategory="100"
android:title="#string/action_edit"
app:showAsAction="collapseActionView"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="101"
android:title="#string/action_settings"
app:showAsAction="collapseActionView"/>
</menu>
This will show a menu with both the options collapsed by default. To listen for clicks on these menu items, you override onOptionsItemSelected and perform the necessary action
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//Do something
...
return true;
case R.id.action_edit:
//Do something else
...
return true;
}
return super.onOptionsItemSelected(item);
}
You can create a layout for your ActionBar and then use something like the following in your activity:
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);
And in your R.layout.custom_action_bar create the buttons you need.
I'm trying to add an 'Add Item' button at the top in the action bar. (To the right of the App Icon and Title).
Right under the action bar, I have two tabs that I can swipe between. I also have a menu XML file defined for the settings menu.
I thought actionbar uses a menu XML as well. So I added a actionbar menu XML, but when I use
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
actionbar.setCustomView(R.menu.actionbar);
my program crashes. I believe I'm doing this totally incorrectly.
My actionbar XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1" android:icon="#android:drawable/ic_menu_add"></item>
</menu>
I read on some tutorials that I'm supposed to add items to the actionbar and populate it via the OnCreateOptionsMenu function in mainActivity. But that's where my options menu is populated, not my actionbar.
An activity populates the ActionBar in its onCreateOptionsMenu() method.
Instead of using setcustomview(), just override onCreateOptionsMenu like this
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
If an actions in the ActionBar is selected, the onOptionsItemSelected() method is called. It receives the selected action as parameter. Based on this information you code can decide what to do for example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuitem1:
Toast.makeText(this,"Menu Item 1 selected",Toast.LENGTH_SHORT).show();
break;
case R.id.menuitem2:
Toast.makeText(this,"Menu item 2 selected",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}