There was a problem to be solved. In the application, you need to make a digital clock on the left side of the toolbar that shows a specific time with a change in value. Is it possible to implement this?

    3 answers 3

    This is pretty easy to do. Take your toolbar and assign it a title. There are three options: If you have it in the markup, then just find it by id:

    toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); 

    and then write this line:

     toolbar.setTitle("ваше время"); 

    The second option - if there is no toolbar, then you can find a toolbar for activating it programmatically and refer to it as:

     getSupportActionBar()... //или как-то так) 

    The third option is to use your toolbar as a full view item with a full setting:

      <android.support.v7.widget.Toolbar android:id="@+id/toolbar" style="@style/Toolbar"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_marginEnd="20dp" android:layout_height="wrap_content"/> </RelativeLayout> </android.support.v7.widget.Toolbar> 

    and then find the textView and ask him what text you need. Good luck :)

      You can, in the toolbar, in contrast to the action bar, it is possible to insert View elements.

      From the book Android programming guide, big nerd ranch:

      This is an interesting way to adjust the height of the toolbar. Your app works.

      • Yes, only ActionBar is also a View, which allowed to do something setCustomView. And the translation does not say that it is impossible, it just allows you to do it more flexibly, most likely meaning xml - Shwarz Andrei

      Thanks to all! Found an option using the TextClock view element.