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
Related
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 want to just add back button in Right side of action bar and I found many link for this.
This is my code which is app->res->menu->main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/backAction"
android:icon="#drawable/ic_launcher"
android:title="back"
android:showAsAction="always"/>
</menu>
Optionmenuactivity.java
Oncreate event:
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
onoptionItemselected()
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.backAction:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Manifest File :
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
Problem with this code:
Activity Flow is: (Main)Activity1-> Activity2 ->Activity3 ->Activity4
after Activity4, if we press backbutton, it should go on Activity3 but with this code it goes Mainactivity which is Activity1
Please help me ..Thank you
There are a lot of problems here.
Firstly I recommend you, to understand the difference between Back and Up
Up navigation explained with examples
Back navigation explained with examples
Secondly, it's not very easy to understand what's happening in your code, but as far as I see, you HAVE NOT declared in the manifest.xml the parent elements.
This is a nice and simple example:
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
A parent activity definition can EASE your life. This way you can define which activity will be loaded on UP NAVIGATION interaction.
Hope that helps :)
First of all i wanted to say what you are trying to do is not a good practice, why would you want a back button in your action bar if Android devices already have one on the bottom of the screen and you can turn on Up-Navigation at the left side of the Action Bar?
Custom App-Navigation clutters your code, confuses users and most of the time is just ugly.
But if you really need a button that does the navigation, you want it to access the standard android navigation methods.
For Back Navigation:
Just use the standard android back action onBackPressed().
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.backAction:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If this function doesn't do what you want, you can just override it with your own wanted behavior.
For Up Navigation:
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.backAction:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You will have to configure your parent activites in the manifest, but because of that the behavior of this Navigation is easier to change.
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
I've been trying to get my actionbar buttons to show on click but can't get it to work. I got 2 buttons and if i click on one i want the other to show and the other to get invinsible.
Here is my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MenuItem brandsMenu = (MenuItem)findViewById(R.id.action_brands);
MenuItem categoryMenu = (MenuItem)findViewById(R.id.action_category);
switch (item.getItemId()) {
case R.id.action_category:
brandsMenu.setVisible(true);
return true;
case R.id.action_brands:
categoryMenu.setVisible(true);
}
This only shows errors. Any Suggestions?
You need to call InvalidateOptionsMenu when you want to make changes to your menu.
You then use the onCreateOptionsMenu override to apply those changes.
Define MenuItems named brandsMenu and categoryMenu and initialize them onPrepareOptionsMenu
categoryMenu = menu.findItem(R.id.action_category);
brandsMenu = menu.findItem(R.id.action_brands);
You should be able to change visibility such as categoryMenu.setVisible(true);
You can't set visibility on menuitems.
You should invalidate the options menu and add only the menuItems you want to be visible
Is there any way to use the standard menu button in the action bar to do something? In my case a sliding menu, I already have code that works in the onClick of a normal button. I just want to use the menu button in the action bar instead. Is this possible? Or will I have to customize the action bar and not use the button that is already there?
I believe you mean the ActionBar Home icon.
First you have to enable it
getActionBar().setHomeButtonEnabled(true);
Then you have to handle the event
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//do your work
return true;
default:
return super.onOptionsItemSelected(item);
}
}
FYI if you want to follow the Navigation drawer pattern you should read this tutorial.