I have a toolbar in CollapsingToolbarLayout inside AppBarLayout. I set title in onResume(). When I open activity and fragment, the title is shown correctly. But if I collapse it and press home button and open it again, it is not there anymore. However if I press home button when layout is expanded, the title appears as expected. Here is the code of setting the title:
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("TITLE HERE");
toolbar.setTitle("TITLE HERE");
}
What is wrong? Is it a bug in support library or am I doing something wrong?
try setting the title of the collapsing toolbar layout. Example:
collapsingToolbarLayout = (CollapsingToolbarLayout)view.findViewById(R.id.collapsing_toolbar_layout);
collapsingToolbarLayout.setTitle("title");
Related
Is there any way to change the ActionBar color of fragments and change the title displayed in the ActionBar to the title of the menu option that was pressed?
If you are using AppCompat you always have to call getSupportActionBar()
Change Title :
getSupportActionBar().setTitle("Your Title");
Change Color :
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.YOUR_COLOR));
If in Fragment :
Change Title :
getActivity().getActionBar().setTitle("YOUR TITLE");
Change Color :
getActivity().getAcationBar().setBackgroundDrawable(new ColorDrawable(Color.YOUR_COLOR));
Hope this help you
This is the suggested answer written in java :
getActionBar().setTitle("Test");
getActionBar().setBackgroundDrawable(new ColorDrawable("COLOR"));
If you're using support libraries:
android.support.v7.app.ActionBar supportActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setTitle("My action bar title");
View view = new View(getContext());
view.setBackgroundColor(getResources().getColor(R.color.my_color));
supportActionBar.setBackgroundDrawable(view.getBackground());
}
Otherwise:
android.app.ActionBar actionBar = getActivity().getActionBar();
if (actionBar != null) {
actionBar.setTitle("My action bar title");
View view = new View(getContext());
view.setBackgroundColor(getResources().getColor(R.color.my_color));
actionBar.setBackgroundDrawable(view.getBackground());
}
I want to create some animation on my activity and for this reason I need to be able to show the actionbar menu icon (the three dots one) but in same time to block programmatically any click event on it.
I need something alike:
setClicksOnActionbarMenuEnable(false); // any click on my actionbar menu icon will be disabled
doMyAnimation();
setClicksOnActionbarMenuEnable(true); // actionbar menu icon will accept again clicks.
I have searched everywhere and couldn't find any way to accomplish this.
I got this solution:
final Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
final View child = toolbar.getChildAt(2);
if (child instanceof ActionMenuView)
{
final ActionMenuView actionMenuView = ((ActionMenuView) child);
actionMenuView.getChildAt(actionMenuView.getChildCount() - 1).setEnabled(false);
}
If don't have a navigation drawer replace with:
final View child = toolbar.getChildAt(1);
I have an application, that at the moment only has 2 fragments. Fragment 1, this has the nav drawer and the title.
Fragment 2 requires a custom view as adding menu items won't work as I need alignment. So I add the view as follows:
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.LEFT);
//Remove nav drawer "hamburger"
mMainActivity.mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
//Remove title from Toolbar
bar.setDisplayShowTitleEnabled(false);
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View postToolBar = layoutInflater.inflate(R.layout.upload_content_toolbar, null);
mMainActivity.mToolBar.addView(postToolBar, params);
That is fine and it displays correctly. However, when I want to return to the previous fragment Fragment 1, I then call mMainActivity.mToolBar.removeView(postToolBar); I call this on return to Fragment 1 as the user can navigate either by the back button or by button in the postToolBar. However, the view is still in place. I can't get rid of it. I have now tried setting the visibility to GONE but that won't work either.
This was pretty simple with the Action Bar, however things seem to have gotten a bit complicated with the Tool bar.
I must add that in each of my two fragments I extend a BaseFragment in which I declare the toolbar view.
Can any one help or send me in the direction of a tutorial?
This is how i achieved it at this time:
in activity onCreateView();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowCustomEnabled(true); // enable overriding the default toolbar_home layout
getSupportActionBar().setDisplayShowTitleEnabled(false); // disable the default title element here (for centered title)
getSupportActionBar().setDisplayShowHomeEnabled(false);
cutomToolbarView=getLayoutInflater().inflate(R.layout.custom_toolbar_home, null);
getSupportActionBar().setCustomView(cutomToolbarView);
}
and here is two simple method to do the magic
public void setToolbarTitleEnabled(String title) {
getSupportActionBar().setDisplayShowCustomEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(title);
}
public void setCustomToolbarEnabled() {
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
then simply when you can change actionbar like these:
when to select HomeFragment.java or whatever in your case
setToolbarTitleEnabled(CURRENT_TAG);
and when to select other fragment:
setCustomToolbarEnabled();
I have an application with the following hierarchy:
MainActivity (Shows list of dates)
|
ViewPagerFragment (Shows list of children for those dates)
|
ChildFragment (Detail View)
I am trying to implement the navigation view from the design support library, but am having trouble getting the actual navigation on the toolbar to work.
Here is the Main Activity toolbar:
Here is the ViewPagerFragment after navigating there from the main activity, note that there is no back button...
Here is the desired toolbar:
I am adding fragments using the following code:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment, fragment.getClass().getName())
.addToBackStack(fragment.getClass().getName())
.commitAllowingStateLoss();
Here is the code related to activity startup:
protected void setupActionBar() {
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
And my navigation drawer setup:
protected void setupNavigationDrawer() {
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
Hitting the hardware back button on the device navigates back properly. I just can't seem to get the back arrow to show up on the drawer toggle... Any suggestions?
To disable the navigation drawer icon and show a different icon instead, you need to call setDrawerIndicatorEnabled(false) on your ActionBarDrawerToggle.
You may also need to call setHomeAsUpIndicator() to specify the icon you want to use instead of the "hamburger" icon.
Have you tried to reach the ActionBar of the Activity ?
in onViewCreated:
Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
((Activity) getActivity()).setSupportActionBar(mToolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
How can I remove the back button icon when search view shows up in Toolbar (AppCompat)?
toolbar = (Toolbar) findViewById(R.id.tool_bar);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.menu_main);
toolbar.setTitle("");
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
// this works for normal back button but not for one appears on tapping SearchView
actionBar.setDisplayHomeAsUpEnabled(false);
Use:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
toolbar.setNavigationIcon(null);
OR
toolbar.setNavigationIcon(getResources().getColor(android.R.color.transparent));
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
There is no way to remove the back arrow from a SearchView.
you can try finding it yourself
in the class (of the support.v7 lib or the main Android project) it looks like this:
mCloseButton = (ImageView) findViewById(R.id.search_close_btn);
But it's a private member and the visibility changes upon text input