Search code examples
androidandroid-toolbar

title in toolbar is too big i want title to move right to left


Toolbar title is too big whole title cant be seen. Is it possible to move title right to left like marque in HTML? it should look like animation I've seen much application with moving title. But I've no Idea how they did that.

toolbar.setTitle(""+Common.downloadsList.get(index).getName());

Solution

  • You can achieve your requirement like this.

    These are the main properties to achieve marquee text.

    android:maxLines="1"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    

    XML code:

    <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/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/actionBarSize"
    app:popupTheme="@style/AppTheme.PopupOverlay">
    
    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/toolbar_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:maxLines="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true" />
    

    Your Activity should look like this.

    Toolbar toolbar=findViewById(R.id.toolbar);
    AppCompatTextView toolbar_title = findViewById(R.id.toolbar_title);
    setSupportActionBar(toolbar);
    
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar_title.setText(""+Common.downloadsList.get(index).getName());
    }