how to set title in toolbar? - android

Seems pretty simple question, but could't find any answer.
How to change the toolbar title.
The toolbar is created by AppCompat Activity,not a custom created one.
It has the app name as default.I would like to change its Name corresponding to the action the activity performs.
Note : I can change custom toolbar title using the below code,so please i am not looking for that.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("My Title");

You can set default Toolbar title as following:
YourActivity.this.setTitle("Your Title");
For fragment:
getActivity().setTitle("Your Title");

getSupportActionBar().setTitle("Title")

Set ActionBar title based on your need
ActionBar ab = getActionBar();
ab.setTitle("My Title");

Related

Change the App name to some other text in the top bar

Can please someone tell me how to change the App name in the top bar of my android App that gets displayed by default to some other text? I have seen this in a few Apps I used before. I'm building my first Android App and don't have that much experience with this stuff... thank you :)
after
setContentView(R.layout.layout_name);
simply add
setTitle("title_name");
this will change the name of your current activity in top bar or toolbar and i think your activity should extend AppCompactActivity
it is easier if you convert the actionbar (which I think is the bar you mean) into a toolbar if you want to do further modifications to it (as I assume):
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
to change the title then simply call actionbar.setTitle as shown here:
actionBar = getSupportActionBar();
actionBar.setTitle("Your Title");
and if you want a subtitle add:
actionBar.setSubtitle("Your Subtitle");
If you are using the new support library, you should use this:
Toolbar toolber = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(Toolbar);
getSupportActionBar().setTitle("App Title");

How To Change Included Toolbar Title And Drawable?

I have a hard time finding on how to change the Toolbar title and it's drawable in every and each Activity.
There is something on Android Documentation that states that to minimize APK size. It is recommended to re-used components.
I create a separate layout, and <include layout=""/> to each of my Activities like Help, About , and etc. I also put android:label="Title" on Manifest File.
Toolbar.xml
My Main:
How do I access this included Toolbar DRAWABLE and TITLE in my Activities ?
Update: I removed ActionBar .
You are using Toolbar. So no need to do anything with ActionBar. Just set your Toolbar as supportActionBar. Then set title and icon to your Toolbar.
Use the below code:
mToolbar = (Toolbar)findViewById(R.id.Toolbar);
setSupportActionBar(mToolbar);
setTitle("About");
getSupportActionBar.setIcon(R.id.ic_arrow_back_black_24dp);
And, Remove all your mActionBar codes. Hope this helps.
well you set the support action to your custom toolbar,yet you ignore it and use mActionBar, see where i am going with this? setIcon , and setTitle should all be called relative to your custom toolbar.

How to decrease padding between title an subtitle in toolbar

Does anybody know how to decrease the pading between the title and subtitle in the toolbar?
I'm using an AppCompatActivity toolbar and setting title and subtitle through the following methods:
toolbar.setTitle("Title");
toolbar.setSubtitle("Subtitle");
One way to do this is to disable toolbar title display and create your own toolbar layout.
getSupportActionBar().setDisplayShowTitleEnabled(false);
This way you will have full control over paddings, style etc.

Toolbar with text

I have a Toolbar with text "App de prueba" and a menu icon, but the app name is on the left side, how can delete the app name?
Thanks!
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Just call the setTitle() method in your activity's onCreate and pass an empty String in it - setTitle("") (assuming you are using a custom layout as title in your toolbar). You cannot remove or change the visibility of the title view in the activity as the id of this view is not public. There are ways by which you can still access the id by using reflection but that makes the code messy and therefore is not needed.

How to change Toolbar Color with different views?

I am setting up my Toolbar in my main activity and trying to change the background color with different fragments.
So basically, I am trying to access the Toolbar object inside fragment and set different background color.
Few things which I have tried to do is :
Access toolbar like:
((ActionBarActivity)getActivity()).getSupportActionBar().setBackgroundColor(XXX);
But I am unable to access the setBackgroundColor function inside fragment. It is perfectly working inside the Main Activity.
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));
or
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
Have a look This or This
It is very easy to change the ToolBar, Actionbar color.
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));
or
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));

Categories

Resources