This Question have 2 answers right now.

How to dynamically change custom ActionBar title?

Question!

I'm trying to change a custom ActionBar title dynamically, when certain things happen in the main part of the view. And even though I've been able to initially set the title correctly, using the following code ...

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.activity_scanning_title);

I cannot seem to get access to the right object from the ActionBar in order to modify it when needed. Because the intuitive option was getCustomView(), I've gotten as far as:

ActionBar bar = getSupportActionBar();
android.view.View v = bar.getCustomView();
if (v instanceof android.widget.LinearLayout)
{
     LinearLayout layt = (LinearLayout)v;
     // ???
     . . .
}

and it turns that getCustomView() indeed returns a LinearLayout, but I cannot get at the TextView that is part of the layout, which properly displayed the original title, only I need to change it.

The xml for the LinearLayout that defines my custom title is here:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Scan Assessment"
    android:textColor="#ffffff"
    android:textSize="18sp" />
</LinearLayout>
By : Alyoshak


Answers

Give the TextView an ID:

<TextView
    android:id="@+id/title"
    ... />

Call findViewById() on the custom View to get it:

TextView title = (TextView) bar.getCustomView().findViewById(R.id.title);

And use the setText() method to change your title:

title.setText("New title");
By : Mike M.


Is this what you are looking for?

import textwrap
from string import join, split

text = """This is a block of text. It keeps going on
            and on and on. It has    some line breaks \n
            but mostly just keeps going on without
            breaks. The lines are sometimes too long,
            so they wrap to the next line, but they 
            don't indent. I need to fix this"""

print "\nPrinted as one line:\n"
t=join(text.split())
print t

print "\nPrinted as formatted paragraph:\n"
t=join(text.split())
t=textwrap.wrap(t,width=70,initial_indent=' '*4,subsequent_indent=' '*8)
t=join(t,"\n")
print t

Results:

Printed as one line:                                                                                                                                           

This is a block of text. It keeps going on and on and on. It has some line breaks but mostly just keeps going on without breaks. The lines are sometimes too lo
ng, so they wrap to the next line, but they don't indent. I need to fix this                                                                                   

Printed as formatted paragraph:                                                                                                                                

    This is a block of text. It keeps                                                                                                                          
        going on and on and on. It has                                                                                                                         
        some line breaks but mostly just                                                                                                                       
        keeps going on without breaks.                                                                                                                         
        The lines are sometimes too                                                                                                                            
        long, so they wrap to the next                                                                                                                         
        line, but they don't indent. I                                                                                                                         
        need to fix this                                                                                                                                       
By : blackpen


Video about How to dynamically change custom ActionBar title?