Setting toolbar title not working in onCreate() - android

I am having issues setting the title of my toolbar, which only seems to work when there is a delay.
For example a simple oncreate() method for my app 'Forecast'
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(_toolbar);
setTitle("ABCDEFG");
}
When the app is run, the toolbar has the name of my app, not 'ABCDEFG'.
However, if I put a 200 millisecond delay before setting the title..
Observable.just("ABCDEFG")
.delay(200,TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::setTitle);
My Toolbar title is correctly displayed as "ABCDEFG"
this is the same when it comes to doing anything with the Toolbar, does anyone know what is actually going on here? and how I can solve this without having to put a delay in?
Here is my toolbar xml..
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
Thanks for your help!
EDIT forgot to mention setTitle() simply calls _toolbar.setTitle(title)

If you call setSupportActionBar(Toolbar), then the Action Bar is then responsible for handling the title, therefore you need to call getSupportActionBar().setTitle("My Title"); to set a custom title.
Also check this link where toolbar.setTitle("My title"); may cause problem like below:- In android app Toolbar.setTitle method has no effect – application name is shown as title
And toolbar is the general form of action bar.
We can have multiple toolbars as layout widget but action is not.
Thus better approach is to use getSupportActionBar().setTitle("My Title");

As maRShmallow stated, I changed _toolbar.setTitle() to getSupportActionBar().setTitle()

Related

Android ActionBar missing after extending Appcompatactivity

I've recently updated my app extending Appcompatactivity in my Activities. Since then, the Actionbar is gone when I launch an external library Intent.
For example, I'm using the HockeyApp SDK to launch their FeedbackActivity
Here is my code:
FeedbackManager.showFeedbackActivity(this, Uri.fromFile(file));
And here a screenshot (you can see the ActionBar is gone).
It used to work before until I started extending Appcompatactivity.
For the rest of Activities it works. The ActionBar is gone only when I launch an external library Intent.
Any ideas?
First, check your theme it may be like below ("NoActionBar"). Then the action bar is not appearing. If this is your issue. please add an appropriate theme for your application
<application
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
/>
if your theme is not a problem, you can add below content to your XML file. (add this as a first child of your XML file)
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
and add below content to your activity on create method
protected void onCreate(Bundle savedInstanceState) {
.......
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
The reason is probably that FeedbackManager.showFeedbackActivity(this, Uri.fromFile(file)) opens a new FeedbackActivity.class which is subclass of Activity.class instead of AppCompatActivity.class, so it can not show the ActionBar.Here is a link https://stackoverflow.com/questions/30681918/nullpointerexception-with-actionbar-setdisplayhomeasupenabledboolean-on-a-nu that explains some reasons.

How we can change the text on the menu bar for different files

I have a registration form and a login form in my Android Studio project. At the moment the text at the top of each is the same, but I want different text on each page.
How can I change this so that the highlighted text on each page is different? Where do I configure this?
First, you have to create a toolbar.xml file and insert it into your all activity where you want to change the name of the title bar.
This is how to create Custom toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
then find the toolbar into java file and set the name to title bar like
this:-
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");
If any query regarding this,ask happy to help.

Toolbar logo taking up too much space

Im dealing with a strange toolbar behavior in which if I set a logo with
getSupportActionBar().setLogo(R.drawable.logo_toolbar);
The Toolbar components (in my case the logo itself and the title) get disaligned from the standard gravity start behavior.
This is my toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/grey_900"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"/>
The toolbar has a SearchView at the right and no "home button", only title.
Graphic explanation:
1) Behavior without calling setLogo(...)
2) Behavior when calling setLogo(...)
3) What I'm trying to achieve
I've already tried playing with gravity but nothing happened.
Also, please, I know that Toolbar is a ViewGroup and I can customize it, but I'm looking for a clean code, also this should be a default behavior and but it isn't working so I would like to know if I'm doing something wrong.
How can I fix this? Thanks in advance!
getSupportActionBar().setTitle("Home");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
If you use getSupportActionBar , No need to use toolbar again.

How to make Toolbar transparent and put an image backwards it?

I want the ToolBar to be transparent. I have an Activity that is showing a Toolbar and looks like this:
I have this on my xml:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent" />
</android.support.design.widget.AppBarLayout>
But I can't make my Activity be on top and be visible at the back of my ToolBar...
How can I achieve this?
StackOverflow does not allow me to comment because I don't have 50 reputation that why I am commenting in answer box
You can find you answer here:
How to make the support Toolbar background transparent?
How to make Toolbar transparent?
#RAP's answer seems work. But note that if you set the background to transparent, means that the toolbar is still clickable when user touch on it and doing some actions you don't want. Other alternative to achieve your goal is by setting its visibility to INVISIBLE:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setVisibility(View.INVISIBLE);
// to make it appear again:
toolbar.setVisibility(View.VISIBLE);

Android Toolbar - How to implement Spinner for navigation mode?

What is currently the correct way to implement the View Control (No. 2 from the below screenshot taken from Android's design guide):
I found this example but when I tried to replicate it, I noticed that methods like:
actionBar.setNavigationMode() are already deprecated.
So how should I implement it? I thought at first that it's a Spinner but I see apparently that it's not exactly the same
and can I still use ActionBar or should I better move to use Toolbar (yes, I am confused...)
As you rightly said, the setNavigationMode() method is now considered passé. To get the spinner in API 21, you need to use the Toolbar in this way:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_actionbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary">
<Spinner
android:id="#+id/spinner_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar
Add the above code to your Activity's layout. To set up the Toolbar in this Activity, you need to do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(toolbar);
}
Try this. This will work.

Categories

Resources