I'm using android.support.v7.widget.Toolbar and AppCompatActivity. I've enabled back up button like this supportActionBar.setDisplayHomeAsUpEnabled(true);.
The fragment in side the activity will set the title and subtitle in onResume() like this
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setTitle(title);
activity.getSupportActionBar().setSubtitle("Bingo");
The problem is, when the fragment shows up onResume is called but subtitle is not shown. When I press power OFF and ON, means fragment goes to pause and resumes again. Now, subtitle is visible. I've tested on other android phone as well.
Can you please help me finding out the problem?
This is because the toolbar is not rendered when you are setting the subtitle.
Try this code,Set title and subtitle inside this method
private void setupToolbar(){
toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
if(toolbar != null){
setSupportActionBar(toolbar);
}
toolbar.post(new Runnable()
{
#Override
public void run()
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(mTitle);
getSupportActionBar().setSubtitle("Subtitle);
}
});
}
Related
I have a generic
MyActivity extends AppCompatActivity
I don't override the toolbar with a custom xml defined toolbar, just use the generated one Android provides.
I can set the title via your normal
getSupportActionBar().setTitle("foo");
but setting the subtitle via
getSupportActionBar().setSubtitle("bar");
doesn't set it. It remains blank. I'm doing this onCreate()
(I feel I've done this many times before with no fail)
Although I've noticed if I visit another activity, then return, the subtitle would then show... not on orientation change, not on recreate() but only when I'm returning from an activity.
I'm experiencing this on 5.0 and 7.0
For the time being I'll likely define my own Toolbar and move forward since that seems where most people have solutions for this same problem.
Relevant code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_replenishment_list);
ButterKnife.bind(this);
MyApplication.getInstance().getComponent().inject(this);
setupUI();
}
private void setupUI() {
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
//TODO: not working unless activity is recreated...
// explore custom xml defined toolbar
//actionBar.setTitle("different title than what is defined in manifest"); <-- this does work, but not this
actionBar.setSubtitle(UserUtil.getFormattedFirstNameLastName(userService.getUserFromJWT(), this));
}
}
I have put the below code in my onCreate() method.
ActionBar actionBar = getActionBar();
if (actionBar==null) {
System.out.println("TEST NULL");
} else {
System.out.println("TEST NOT NULL");
}
The result is null. When I add the toolbar first it works fine.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("TESTING");
Your getSupportActionBar or getActionBar will return null if you didn't set toolbar to it. You need to set the toolbar to your action bar before using getSupportActionBar or getActionBar.
I'm toggling a DrawerLayout's state from a button's onClick, and disabling its swipe.
That works OK, but when the Activity changes its orientation, the Drawer doesn't retain its state; if it was opened, it will get closed. It even happens adding android:configChanges="keyboardHidden|orientation" .
Code in my Activity:
private DrawerLayout drawer;
private int drawerLayoutGravity = Gravity.RIGHT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
disableDrawer();
View btnOpenDrawer = findViewById(R.id.btn_open_drawer);
btnOpenDrawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleDrawerState();
}
});
}
private void toggleDrawerState() {
if (drawer.isDrawerOpen(drawerLayoutGravity)) {
drawer.closeDrawer(drawerLayoutGravity);
} else {
drawer.openDrawer(drawerLayoutGravity);
}
}
/**
* doesn't let the user swipe to open the drawer
*/
private void disableDrawer() {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
A possible solution is that I re open the DrawerLayout on the Activity's onConfigurationChanged, but I need to avoid showing the DrawerLayout re opening when the configuration changes.
You're setting a closed lock mode on the drawer to disable swiping. Even though you've disabled Activity re-creation, the orientation change will trigger a layout event on your Views, and the DrawerLayout will set the drawer state according to the lock mode when laying itself out.
You need to update the lock mode whenever you programmatically open/close the drawer.
private void toggleDrawerState() {
if (drawer.isDrawerOpen(drawerLayoutGravity)) {
drawer.closeDrawer(drawerLayoutGravity);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
} else {
drawer.openDrawer(drawerLayoutGravity);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
}
}
I have 3 tabs with lists of different video.In first tab when I click the item of my lyst the new activity started and play the video,this activity have parent my main activity with lists and this mainactivity have 1tab as main tab.But when I click in item in 2 tab,thid videoplayer started too,but back arrow go to main activity and main activity have 1 tab main,not second.It's very hard,but Im trying to explain:-)
How can I change this behavior,to go to 2 tab when click back arrow?
If when you have a toolbar then just add the following code in the onCreate. What this will do, it will work as the Back key of the smartphone. so it will close the current activity, and bring you back to what ever was on.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { finish(); } });
}
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'm trying to change the title of activity, where i'm going to the activity from the fragments, so when i click on the hardware back button the title doesn't chnage to the one that i have provided in that activity i.e
this.setTitle("Something");
I have also tried
setTitle("Something");
Update
When i use the below mentioned code i run into a problem where the title that i assign in the mainActivity stay in all the pages, hence in the frgament getActivity().setTitle(""); seems to be useless in this case, so instead of changing everywhere the title using the below mentioned code i.e changing the actionbar title using getsupportactionbar.settitle(""), is there an easy way to do.
By any means can i know if an activity has came from the system back button back state or not?
You can handle this situation with onResume() method
//...
#Override
onResume(){
super.onResume();
this.getActionBar().setTitle("Something");
}
ı think, that will help you.
Try this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar ab = getActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title");
}
EDIT:
For SupportActionBar use:
ActionBar actionBar = getSupportActionBar();
EDIT2:
I use this to set Title from every fragment
#Override
public void onResume() {
super.onResume();
// Set the title
getActivity().getActionBar() //getSupportActionBar()
.setTitle(R.string.fragment_title);
}
If you always add your fragment to backstack when changing, then backpress can be override as follows to always get your previous fragment's title:
#Override
public void onBackPressed() {
int T= getFragmentManager().getBackStackEntryCount();
if (getFragmentManager().getBackStackEntryCount() == 0) {
finish();
}
else if (getFragmentManager().getBackStackEntryCount() == 1) {
finish();
}
else {
String tr = getFragmentManager().getBackStackEntryAt(T-2).getName();
setTitle(tr);
getFragmentManager().popBackStack();
}
}
If you are occured this in Kotlin just remove Label from destination in Your navigation graph.
getSupportActionbar().setTitle("Your Title")
You have to call this whenever you want to change your title.
If you are switching between fragments call the method again and switch it back to your old title.