On the top left hand side of my toolbar I want a default Android back arrow (in white) that takes you to the previous page. For some reason it doesn't show. Here is what I'm doing
Toolbar toolbar = (Toolbar) findViewById(R.id.sign_up_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3f9845")));
getSupportActionBar().setDisplayShowTitleEnabled(false);
Am I missing a line or something? Thanks for any help.
Please make sure you have extends ActionBarActivity.
basically given code are used to show back arrow and it running perfectly into my phone
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
and below code is used to perform the action on through back button
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
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
Hi I am having trouble while going back to parent activity from fragment activity. I want back arrow at top left corner inside the action bar.
I am able to show it in action bar activity using this code
getSupportActionBar().setDisplayShowHomeEnabled(true);
But I am not able to do it in tabbed activity's fragment.
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
Please Help me!!
Add this inside onCreate() of your tabbed activity,
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
And for navigating back, you need to override following method in your tabbed activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
Happy coding.
Add this method to your activity to navigate back.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();// or the action you want to do eg. Removing fragment
break;
}
return super.onOptionsItemSelected(item);
}
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.
I am using a NavigationDrawer with my ActionBar. It needs the home button and configured as up.
I try to put a button in the middle of the action. The only way I found is to use a custom layout.
But when I use it, my home title is erased. Even if it has the space to be displayed.
Is there a way to set the home button to always show his title ?
If not is there an other trick ?
Thanks in advance :)
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setHomeButtonEnabled(true);
and add
// For back Button
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, DashbordActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return (super.onOptionsItemSelected(menuItem));
}
try following code
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setHomeButtonEnabled(true);
In my project I have used action bar sherlock library. I want to make back button in action bar I used following code
getSupportActionBar().setHomeButtonEnabled(true);
And
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
Every thing is working fine, but back Arrow (to the left of icon) is not displaying. I want to show Back Arrow also. Can anyone tell me what I am doing wrong.
You have to set it up this way:
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
Hope that helps.