I am trying to achieve this(A timer in toolbar
with red background):
I am trying to add customView
in toolbar
. It always end's up being on extreme left just beside the back arrow. Like the text YP
in image below is a custom Textview
added to toolbar..
The code for toolbar
:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar android:id="@+id/base_activity_toolbar" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:elevation="4dp" android:minHeight="?attr/actionBarSize" android:popupTheme="@style/ThemeOverlay.AppCompat.Light" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
The code for adding the layout:
LayoutInflater mInflater= LayoutInflater.from(context); View mCustomView = mInflater.inflate(R.layout.textview, null); toolbar.addView(mCustomView);
This is the layout I am adding right now for testing:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:gravity="end" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:text="yp" android:layout_height="wrap_content"/> </LinearLayout>
I am missing something majorly, could not figure out. Pulling my hairs out.
You can define ToolBar custom layout using constraint layout as shown below, see http://www.zoftino.com/android-toolbar-tutorial for more information. Show activity on this post. This is an example of where the answer should display the important stuff from the links, in case links stops working.
You can make other changes like font, size, color, etc in custom_toolbar. xml file. If you want to change the text of custom toolbar, you can do in this way: ....
I just set layout_gravity to the right
and it worked
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="0dp" android:layout_height="?android:actionBarSize" app:title="NEW REQUESTS" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="blahblah" android:layout_gravity="right" /> </android.support.v7.widget.Toolbar>
You can add ViewGroups
inside Toolbar
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="@dimen/toolbar_height" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <RelativeLayout .... </RelativeLayout> </android.support.v7.widget.Toolbar>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With