I added a icon on the action bar. And when i click on it, it opens another activity via intent. However, in some source, people add getSupportActionBar and setDisplayHomeAsUpEnable. Even if without those, it still works.
My question is what is the meaning og these 2 API?
If you have included actionBar or toolbar in your activity and if you want to go to previous activity on the tap of back arrow on the left side (in LTR configuration) of your actionbar, you will have to first get actionbar as,
ActionBar ab = getSupportActionBar();
and then provide action which is go to previous activity and for that you will have to call setDisplayHomeAsUpEnabled() method as,
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
Related
I'm trying to customize the font in one activity action bar. I managed to do it by using this code during activity's onCreate:
if(getSupportActionBar()!=null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.secondary_toolbar);
TextView actionBarTitle = getSupportActionBar().getCustomView().findViewById(R.id.toolbar_title);
actionBarTitle.setText(R.string.new_payment_method);
}
The problem is that if I change the toolbar to a custom layout, the code lines which enables the arrow to let the activity to navigate to the previous activity doesn't work anymore. I'm stuck finding a way to modify the font in the action bar and showing the back arrow which allows me to navigate back, at the same time.
I have an Android App which consists of several Fragments.
Each time the fragment is shown I set the ActionBar title to this fragment.
I did this with
getActivity().setTitle("abc");
Later in the App I needed to work a bit more with the ActionBar of a Fragment. So I had to change the title like this:
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null)
actionBar.setTitle("xyz");
This works perfectly fine and changes the title to "xyz".
Still when changing to another Fragment that uses setTitle("abc") from the Activity itself, the title still stays at "xyz". Once I've done this for the first time, I can only change the title with the getSupportActionBar() object.
My question is: is this normal? Does this call transform / invalidate the normal Activity-Title somehow?
If you check the Activity Source Code, setTitle() method sets the text to actionBar itself.
Consider there are two fragments, A and B.
Fragment A will be shown by default in the Activity.
So later when you update the title from fragment B, you use getSupportActionBar() or getActionBar() method and update the title.
Since this is the recent change that is occured, even if you go back to the previous fragment,that is fragment A, the title will be same, as set in the fragment B.
If you want to change the title again, do it in onResume() method of the fragment A.
I created two blank activities in android studio and it looks like it adds back arrow by default. My MainActivity is parent ofResultActivity. I want to maintain this hierarchy but want to get rid of back arrow.
If you're on API level 14 or above and are not using ActionbarSherlock, this code in onCreateOptionsMenu should disable the up button;
ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.setHomeButtonEnabled(false); // Disable the button
actionBar.setDisplayHomeAsUpEnabled(false); // Remove the left caret
actionBar.setDisplayShowHomeEnabled(false); // Remove the icon
}
If you're using a support lib such as ActionbarSherlock, then use;
getSupportActionBar().setHomeButtonEnabled(false); // Disable the button
getSupportActionBar().setDisplayHomeAsUpEnabled(false); // Remove the left caret
getSupportActionBar().setDisplayShowHomeEnabled(false); // Remove the icon
getActionBar().setDisplayHomeAsUpEnabled(false);
I know this is an old question but I came across this now and had to take some additional action to remove the back arrow.
So in addition to this piece of code as indicated the correct answer
getActionBar().setDisplayHomeAsUpEnabled(false);
you will also need to remove the parent-child relationship in the AndroidManifest.xml file . Your activity should not have the following entry
android:parentActivityName
Might help someone else who stumbles across this one.
I am using ActionBarSherlock on my simple app and would like to hide the HOME button if the user is on the home/main activity. I understand how to do so with the setHomeButtonEnabled(false), however, I am extending a class that contains my navigation and has setHomeButtonEnabled(true) and I cannot seem to overwrite that setting in my main activity.
Thanks to #andy I am able to get rid of the icon, however, I cannot get rid of the < arrow. Any ideas?
Thanks for any help.
With ActionBarSherlock just put this in your home/main activity onCreate() method:
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
The only way I have found to do this is to not have this set on your baseActivity:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and then set it as you will for each activity that requires the menu:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
or
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
Get the home button view and apply this
mHomeButton.setVisibility(View.INVISIBLE);
I want to remove the back icon and the application icon from the Actionbar. I am using ActionBarSherlock. I have seen many applications that dont show an application icon or a back button ? Is this the best implementation ?
Kind Regards,
I think by "back" button you mean up button that is used to navigate up the application's structural hierarchy. Anyway hope this piece of code solves your problem if you don't want to keep the app icon on top.
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(true); // if you want to show title on Actionbar
the action bar doesn't have a back button since all android devices should include a back button in some way or another.
what you probably mean is the up button , which should go to the "home page" of the app.
you don't have to use it . it's just a nice thing to have. use setDisplayHomeAsUpEnabled for showing and hiding it .