How to add another main item on Toolbar (SupportActionBar)? - android

i want to add another one main item icon(the left one from title, hamburger icon) at Toolbar (SupportActionBar).
I want to hide it in one case and show in another (for back arrow in one case, and change it to hamburger icon in another). So is there a way to do so?
And i want it as menu item, not just change the icon at item. I want to use it this way
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.hamburger:
setActionBarMenu();
return true;
case android.R.id.back:
setActionBarMenu();
return true;}}

Related

Add icon to fragment actionbar and get reference to it

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

Android Toolbar with both home and back button

Is it possible to display both home icon and back icon in the toolbar?
1) Is it possible change the order of display of back button icon and home icon.
Currently it displays the arrow button first and then the logo (home button)
2) Second requirement is to clear the activity stack on clicking the home icon and going back to previous screen in case of back button.
I have the following code which will display a arrow back key and home icon which is set as logo. Is it possible to handle click events on both these icons:
Toolbar toolbar = (Toolbar)findByViewID(R.id.toolbar);
toolbar.setNavigationIcon(R.drwable.btn_back);
setSuppportActionBar(toolbar);
getSupportActionBar().setLogo(R.drawable.home_icon);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I am able to handle to the click on arrow icon by handling it in onOptionsITemSelected method.
Is there a way to handle click on logo icon?
My idea is to use the home button click to clear the stack of activities and use the back button to navigate back to previous screen.
I tried with
toolbar.setNavigationOnClickListener()
but it has no effect on back button click.
Handling android.R.id.home works when handled in
onOptionsItemSelected()
For navigating back. This worked for me.
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return (super.onOptionsItemSelected(menuItem));
}
try with this
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
getActivity().finish();
}
return true;
}
});
Design our custom layout as a separate "toolbar_content.xml" and include this layout inside toolbar tag in your "main_layout.xml".
Write click listeners for your items in "toolbar_content.xml" in your base activity so that listeners will be available thru out the app.

Lollipop activity transitions: Back button vs. back from toolbar differences?

I have an app where I'm doing activity transitions for a company directory. When a search result is selected, the animation of their photo to the detail screen works. And if I hit the back button, the reverse animation occurs. However, if I hit the back arrow from the Toolbar, the reverse animation never occurs.
The detail screen is a new DetailActivity with a single fragment in it called DetailFragment. I'm not doing anything special on "onBackPressed". Should I be?
If you want the return transition to play, you'll need to listen for the up-navigation button click and call finishAfterTransition() directly:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finishAfterTransition();
return true;
}
return super.onOptionsItemSelected(item);
}

Actionbar menu onclick

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.

Call listview selected item click, Using Android menu item

In my android application I have a listview with set of records. I have implemented the OnItemClickListener and set it to the listview. So when user click any listview Item it redirects to next activity and do the stuff I need.
The problem is, I have to implement the same functionality to be performed using android Menu.(When click menu button). When click menu button it displays a menu item, to perform Listviews focused item to call click event. Simply when click menu item it should perform the the focused listview item click, as when we normally click the listview item.
Have no idea of how to call click event
Can anyone guide me how to do this please....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Taken from http://developer.android.com/guide/topics/ui/menus.html.

Categories

Resources