Search code examples
androidtoolbartogglebuttontitlebar

Titlebar get into ToggleButton in Toolbar


i'm tyring to create a togglebutton in toolbar with this code : enter image description here

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#acacac">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#acacac"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_alignParentBottom="true"
            android:background="#202020" />
        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/check"
            android:layout_margin="10dp"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textOn=""
            android:textOff=""
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>
</android.support.design.widget.AppBarLayout>

but when i set title the title get into or becomes merged with a togglebutton:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
settitle("stackoverflow");

Solution

  • The good news about Toolbar not like the old ActionBar is that Toolbars are easily customizable. This is how I have customized your layout. First I have added the horizontal linear layout Linear Layout as a view in your Toolbar. And I have removed the ToggleButton and added it inside in LinearLayout just after the a TextView which will now be your app title. And If you don't mind you can set the title directly using XML So this is your new Toolbar Please first delete ( or hide) your ToogleButton and Toolbar and paste the following code in place where your Toolbar was.

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="4dp"
                android:layout_weight="1"
                android:ellipsize="end"
                android:lines="1"
                android:text="title here or even @string/Something"
                android:textColor="#ffffff"
                android:textSize="20sp" />
    
            <ToggleButton
                android:id="@+id/toggleButton"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/check"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:textOff=""
                android:textOn="" />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>
    

    This is how it looks in my Android Studio So far:
    When you want to set your text is longer you will see the dots (...) When its smaller you see it all.

    Showing Longer words cropped

    From now one lets Talks about changes in your code.

    Your Toolbar title will be the Value in your TextView you no longer need the line.

    toolbar.setTitle("stackoverflow");
    

    You can use your TextView directly in xml or in Java code. Remember you can also increase TextSize just like a normal Layout. In code you can use

    TextView text_title=(TextView)findViewById(R.id.title);
    text_title.setText("stack Overflow");
    

    Happy Coding!