Set Android toolbar title appearance programmatically - android

I have an Android toolbar that I have created like such within a Xamarin Forms project:
var toolbar = activity.LayoutInflater
.Inflate(FormsAppCompatActivity.ToolbarResource, null)
.JavaCast<Android.Support.V7.Widget.Toolbar>();
I can easily set the background color and other layout parameters by doing the following:
toolbar.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent);
toolbar.SetBackgroundColor(backgroundColor.ToAndroid());
However, I can't figure out any way to programmatically set the font size because the method to set that is:
toolbar.SetTitleTextAppearance(Context context, int resId)
And I don't want to set it with a configured resource. I want to dynamically set it with a font size and/or other style attributes. How can I do this?

I do not know if it is the same thing using java. When i had this problem i putted a setSupportActionBar(toolbar); after putting the tittle in my java code. The result was something like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(loggedUserName);
setSupportActionBar(toolbar);
Working great.

Related

Change the toolbar title during execution Android

When I start the app I want to display the app name as a placeholder. After getting the user's info, I want to change the title to a variable related to this user.
If I initially do this:
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
And then after a while do this:
toolbar.setTitle(user.getName());
It works fine.
But! If instead of setting the title to an empty string I set it to a non empty string it doesn't work. I assume Android is checking if the title is empty before placing it or something. Is there a way to get this to work?
Call setSupportActionBar(toolbar); again after setting toolbar title with user's name.
toolbar.setTitle(user.getName());
setSupportActionBar(toolbar);
I use :
getSupportActionBar().setTitle("New title");
and it works. Give it a try.

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.

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.

Xamarin AppCompat textview custom

After updating Android Support Libary v7 AppCompat
I'm not able to change style to the title and the following
is not wotking anymore:
int myTitleId = Resources.GetIdentifier("action_bar_title", "id", "android");
TextView barTitle = FindViewById<TextView>(myTitleId);
barTitle.SetTypeface(FontFactory.GetMyFont(this), TypefaceStyle.Normal);
That's because AppCompat now uses a ToolBar widget as replacement for ActionBar. ToolBar creates a TextView instance for the title on-the-fly; if you dig into the (android.support.v7.widget.)ToolBar code, you'll find something like this:
mTitleTextView = new TextView(context);
(Refer to line 607 of android.support.v7.widget.ToolBar in appcompat-v7-23.0.1-sources.jar)
More importantly, no id is ever assigned to the view. This can also easily be seen by inspecting the view hierarchy:
That TextView that you see is the one that holds the title. The id is a generated value and not predefined (like i.e. the one for the ActionBarContainer), which means you can no longer look it up through some sort of static reference.
That explains why your code, which does an id-lookup-by-name, no longer works: the action_bar_title id is simply no longer being used here.
There are several solutions to make setting a custom font to the ActionBar title work again. The cleanest is probably to leverage the fact that setTitle() takes a CharSequence, which means you can attach a custom typeface span to it that enables the custom font and/or style to work. Doing this is some sort of a 'base' Activity would probably make most sense.
You can of course also iterate over the local view hierarchy, starting at ToolBar, but I'd say that's not quite as robust and prone to suffer from future changes (like your current code ;)).
Alternatively, consider using a library to simplify dealing with applying custom fonts. Calligraphy is usually my first stop for this.
After integrating the new Toolbar this is how I changed style.
inside protected override void OnCreate(Bundle bundle) of my Activity (that now extends AppCompatActivity)
where before there was the previous code now I have:
toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
var f = toolbar.Class.GetDeclaredField("mTitleTextView");
f.Accessible = true;
var barTitle = (TextView)f.Get(toolbar);
barTitle.SetTypeface(FontFactory.GetMyFont(this), TypefaceStyle.Normal);
I needed also to add this on the top:
using Java.Lang; //for the reflection
using Toolbar = Android.Support.V7.Widget.Toolbar;
and in the layout.axml I included the toolbar.axml in this way:
//...
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
//...

Categories

Resources