Reputation: 234
I load the title in an Activity dynamically; whenever the title is too long, I would like it to scroll so the whole title can be read.
I've tried in a custom XML file and requestFeature,
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
The other method I've tried
TextView textView = (TextView) findViewById(android.R.id.title);
textView.setSelected(true);
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setMarqueeRepeatLimit(1);
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);
textView.requestFocus();
textView.setSingleLine(true);
gave me nullpointers at ellipsize(). I'm at a loss, really. How can I achieve this effect?
Upvotes: 2
Views: 4105
Reputation: 2373
Your second approach won't work because(TextView) findViewById(android.R.id.title)
returns null.
I suggest follow Bhuro's answer, particularly on how to customize the title bar. Essentially, you would need a custom titlebar.xml that defines what you want in your custom title bar (in your case, just a TextView). An titlebar.xml example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/myTitle"
android:text="This is my new title and it is very very long"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
/>
</LinearLayout>
Then you specify it in your Activity:
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
Upvotes: 1