Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marquee title in Toolbar / ActionBar in Android with Lollipop SDK?

I've tried several different approaches, including the one found here (which in turn led me to trying both of the top answers to this question), as well as using reflection to get access to the TextView and setting the relevant methods. Both attempts failed, the former resulting in no text at all being set to the title (and I was setting the text to the proper textview element), the latter setting the text and removing the ellipse, but not marqueeing at all. Below is my reflection attempt.

import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;

public class MarqueeToolbar extends Toolbar {

    public MarqueeToolbar(Context context) {
        super(context);
    }

    public MarqueeToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setTitle(CharSequence title) {
        if (!reflected) {
            reflected = reflectTitle();
        }
        super.setTitle(title);
    }

    @Override
    public void setTitle(int resId) {
        if (!reflected) {
            reflected = reflectTitle();
        }
        super.setTitle(resId);
    }

    boolean reflected = false;
    private boolean reflectTitle() {
        try {
            Field field = Toolbar.class.getDeclaredField("mTitleTextView");
            field.setAccessible(true);
            TextView titleView = (TextView) field.get(this);
            titleView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            titleView.setMarqueeRepeatLimit(-1);
            return true;
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            return false;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return false;
        } catch (NullPointerException e) {
            e.printStackTrace();
            return false;
        }
    }
}
like image 382
InsanityOnABun Avatar asked Nov 24 '14 02:11

InsanityOnABun


People also ask

How to set title for action bar in Android?

This example demonstrates How to set title for action bar in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How to add marquee in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view and ellipsize property as marquee as shown below - Step 3 − Add the following code to src/MainActivity.java

How to create custom appbar/Actionbar/toolbar in Android Studio | Java?

How to Create Custom AppBar/ActionBar/ToolBar in Android Studio | Java 1 Gradle dependency 2 Set the styles. Base application theme. --> ... Customize your theme here. --> ... 3 Design our AppBar/ActionBar/ToolBar. Look at the below image. Already add the dependency. Put it inside the toolbar and do the adjustment. 4 Follow me on IG and FB

How to show status bar Tittle in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show status bar tittle.


1 Answers

Kotlin solution to set MARQUEE for both Title and Subtitle TextViews (it just finds all TextViews inside Toolbar):

findViewById<Toolbar>(R.id.action_bar)?.let {
    setToolbarTextViewsMarquee(it)
}

fun setToolbarTextViewsMarquee(toolbar: Toolbar) {
    for (child in toolbar.children) {
        if (child is TextView) {
            setMarquee(child)
        }
    }
}

fun setMarquee(textView: TextView) {
    textView.ellipsize = TextUtils.TruncateAt.MARQUEE
    textView.isSelected = true
    textView.marqueeRepeatLimit = -1
}

So it's not necessary to add Toolbar view (android.support.v7.widget.Toolbar or androidx.appcompat.widget.Toolbar) to xml layout

You can use the default Toolbar which AppCompat theme automatically adds:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
like image 139
user25 Avatar answered Sep 29 '22 21:09

user25