Reputation: 1937
I'm trying this to get title as TextView but it's not working:
TextView title = (TextView) findViewById(R.id.action_bar_title);
And title is null. How to get title as TextView using AppCompat?
Upvotes: 5
Views: 7922
Reputation: 295
The textView in Toolbar is added by code, so you cannot find it by view id.
With Kotlin, you can find the title textView by this way.
toolbar.forEachIndexed { _, v ->
if (v is TextView && v.text == "Your title name") {
v.setOnClickListener {
// ...
}
return@forEachIndexed
}
}
Upvotes: 0
Reputation: 180
im using AppCompatActivity , this works for me
val titleId :Int = resources.getIdentifier("title","id",BuildConfig.APPLICATION_ID )
val title :TextView = findViewById(titleId)
Upvotes: 1
Reputation: 638
Hey thanks for the help everyone here, here's the implementation i ended up with.
// USAGE
Toolbar yourToolbar = (Toolbar) findViewById(R.id.your_toolbar);
TextView actionTitle = ActionBarTitle(toolbar);
actionTitle.setTypeface(font("Snake"));
// IMPLEMENTATION
private TextView ActionBarTitle(Toolbar toolbarForRead)
{
TextView title = null;
if (toolbarForRead != null)
{
for (int i= 0; i < toolbarForRead.getChildCount(); i++)
{
if (toolbarForRead.getChildAt(i) instanceof
TextView)
{
title = (TextView)
toolbarForRead.getChildAt(i);
return title;
}
}
}
return null;
}
private Typeface font(String fontName)
{
return Typeface.createFromAsset(this.getAssets(), "fonts/" + fontName + ".ttf");
}
Upvotes: 0
Reputation: 3584
Here is how to get the TextView from the ActionBar. This method ensures that the TextView is selected even if the ActionBar orientation changes. It also allows you to get the icon if you ever want - just check for child instanceof ImageView.
//If you just want to set the text
ActionBar actBar = getSupportActionBar();
if(actBar != null) {
actBar.setTitle(R.string.your_ab_title);
}
//If you want to customize more than the text
Toolbar ab = findViewById(R.id.action_bar);
if(ab != null){
for (int i= 0; i < ab.getChildCount(); i++){
if(ab.getChildAt(i) instanceof TextView) {
TextView title = (TextView) ab.getChildAt(i);
//You now have the title textView. Do something with it
title.setText("Your Custom AB Title");
}
}
}
Upvotes: 3
Reputation: 91
Brute force seems to be one solution:
private void findTextViewTitle() {
String title = "title";
ActionBar ab = getSupportActionBar();
ab.setTitle(title);
Window window = getWindow();
View decor = window.getDecorView();
ArrayList<View> views = new ArrayList<View>();
decor.findViewsWithText(views, title, View.FIND_VIEWS_WITH_TEXT);
for (View view : views) {
Log.d(TAG, "view " + view.toString());
}
TextView tvTitle = (TextView) decor.findViewById(views.get(0).getId());
tvTitle.setBackgroundColor(Color.RED);
}
Upvotes: 4
Reputation: 832
Typically when using the Toolbar in my cases, if I am doing something custom with the title, I will just inflate the title view manually, then set its attributes in XML. The point of Toolbar is to prevent things like this from happening, so you can have more control over what your toolbar looks like
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_height="@dimen/standard_vertical_increment"
android:layout_width="match_parent"
android:minHeight="@dimen/standard_vertical_increment"
android:background="@drawable/actionbar_background">
<TextView
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Red" />
</android.support.v7.widget.Toolbar>
Then in code, you would do something like this:
Toolbar toolbar = findViewById(R.id.my_awesome_toolbar);
//Get rid of the title drawn by the toolbar automatically
toolbar.setTitle("");
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setTextColor(Color.BLUE);
Taken from Getting ActionBar Title TextView with AppCompat v7 r21
Upvotes: 8