package com.averi.worldscribe.views;

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

import java.lang.reflect.Field;

/**
 * A Marquee-able Android Toolbar.
 *
 * <p>
 *     Credit to InsanityOnABun. The Gist for this class can be found
 *     <a href='https://gist.github.com/InsanityOnABun/95c0757f2f527cc50e39'>here</a>.
 * </p>
 */
public class MarqueeToolbar extends Toolbar {

    TextView title;

    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);
        selectTitle();
    }

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

    boolean reflected = false;
    private boolean reflectTitle() {
        try {
            Field field = Toolbar.class.getDeclaredField("mTitleTextView");
            field.setAccessible(true);
            title = (TextView) field.get(this);
            title.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            title.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;
        }
    }

    public void selectTitle() {
        if (title != null)
            title.setSelected(true);
    }
}

@@ -1000,24 +1000,430 @@
 tle(title);%0A
+        // Due to postDelayed(), selectTitle() will cause an exception if it's called before%0A        // the title TextView has been created. In Activities, setSupportActionBar() might%0A        // call selectTitle() too early, thus causing an exception.%0A        // We can prevent this by checking reflected, which can only be set%0A        // to true if the title TextView exists.%0A        if (reflected) %7B%0A    
         sele
@@ -1425,32 +1425,42 @@
  selectTitle();%0A
+        %7D%0A
     %7D%0A%0A    @Over
@@ -1609,16 +1609,422 @@
 resId);%0A
+        // Due to postDelayed(), selectTitle() will cause an exception if it's called before%0A        // the title TextView has been created. In Activities, setSupportActionBar() might%0A        // call selectTitle() too early, thus causing an exception.%0A        // We can prevent this by checking reflected, which can only be set%0A        // to true if the title TextView exists.%0A        if (reflected) %7B%0A    
         
@@ -2034,24 +2034,34 @@
 ectTitle();%0A
+        %7D%0A
     %7D%0A%0A    b
@@ -2801,27 +2801,101 @@
     
-if (
 title
- != null)%0A
+.postDelayed(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A    
     
@@ -2923,20 +2923,52 @@
 cted(true);%0A
+            %7D%0A        %7D, 1000);%0A
     %7D%0A%7D%0A

Add 1s delay before title begins scrolling