I created a toolbar in my xml file by using
<android.support.v7.widget.Toolbar
android:id="#+id/app_bar_settings"
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:elevation="4dp"
/>
I set an NavigationIcon by using this:
mToolbarSettigs.setNavigationIcon(R.drawable.ic_done_black_24dp);
But now there is no margin between the icon and the sides of the toolbar.
How can I change that?
Thanks!
Related
I'm using navigation component and material toolbar.
I've just setup navigation component with material toolbar. By doing so whenever fragment changed the back button shown and the title of material toolbar changed by navigation component automatically.
The question is I just want to change the title gravity into center without removing those navigation components toolbar support.
How can I do that ?
Note : I`ve tried to change the toolbar style, but it seems does not work.
And creating an extra text view into toolbar view its not a solution since I want to use navigation component toolbar support.
Thanks in advance.
Starting from com.google.android.material:material:1.4.0 you can use app:titleCentered="true" to center the title.
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/materialToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="Toolbar"
app:titleCentered="true"/>
I hope this helps. You can mark it as accepted if it matches your needs.
You can use a Custom Toolbar
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/action_bar_bkgnd"
app:theme="#style/ToolBarTheme" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
<androidx.appcompat.widget.Toolbar/>
It's just a part of view Group, You can style the TextView however you would like because it's just a regular TextView. So in your activity you can access the title like so:
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
As a tricky way you can use: com.google.android.material.appbar.CollapsingToolbarLayout
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:collapsedTitleGravity="center"
app:collapsedTitleTextAppearance="?attr/textAppearanceHeadline3"
app:expandedTitleGravity="center"
app:expandedTitleTextAppearance="?attr/textAppearanceHeadline1"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:layout_collapseMode="pin"
app:navigationIcon="#drawable/ic_back_24dp"
app:title="#string/parvaneh_personal_info" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
I want to customize toolbar title text color dynamically.
I had tried following methods, but all methods below failed.
I had also tried to set color before running setSupportActionBar(toolbar), but still failed.
I had also searched and tried methods from Stack Overflow, but all also failed.
How can I do?
textColor = Color.parseColor("#00FF00")
toolbar.setTitleTextColor(textColour)
textColor = Color.parseColor("#00FF00")
(toolbar::class.java.getDeclaredField("mTitleTextView")
.apply { isAccessible = true }
.get(toolbar) as TextView)
.setTextColor(textColour)
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
app:titleTextColor="#00FF00"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:layout_collapseMode="pin" />
The problem was solved that I read through source code of CollapsingToolbarLayout, when there is CollapsingToolbarLayout applied, should not change title text color with Toolbar function setTitleTextColor(), but with methods setCollapsedTitleTextColor() and setExpandedTitleColor() of CollapsingToolbarLayout.
Following is my code.
In activity
val textColor = Color.parseColor("#00FF00")
collapsingToolbarLayout.setExpandedTitleColor(textColor)
collapsingToolbarLayout.setCollapsedTitleTextColor(textColor)
In xml layout
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="270dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#id/toolbar"
android:id="#+id/collapsingToolbarLayout">
...
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:titleTextColor="#color/black"
app:layout_collapseMode="pin" />
...
</com.google.android.material.appbar.CollapsingToolbarLayout>
Right now, as an example, the app looks like this:
How do I programatically or using xml make the appbar look like this:
I've tried setting the appbar icon as if it were a background, but it looks rather odd.
You can add the application icon as a menu item to appbar with android:show="always".
Another workaround is to place an imageView in your toolbar, which is inside the appBar. You can then change the position of this imageview through xml code or programmatically.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ToolbarColoredBackArrow"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<ImageView
android:id="#+id/indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_small"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
I'm newbie in Android so this might be the easy question for you guys but I'm stuck. I want to remove shadow of Toolbar so I followed thread how to remove shadow below actionbar with AppCompat.Light.NoActionBar? and using app:elevation="0dp" I was able to remove the shadow of Toolbar but that change the current height of Toolbar as displayed in [Image 2]. I want maintain the same exact size of Toolbar but without shadow. I also used android:fitsSystemWindows="true" from thread Android toolbar height difference but it move Profile text on top and also create white line at bottom [Image 3] .
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:minHeight="?attr/actionBarSize"/>
</android.support.design.widget.AppBarLayout>
I guess we can use android:layout_height:Xdp or android:layout_height:Xpx but is this the last option or there is default way to do this.
In pre-5.0 documentation there is an example that shows tabs inside the Action bar:
https://developer.android.com/guide/topics/ui/actionbar.html#Tabs
What is the correct way to achieve that in 5.0+ interface with new Toolbar?
Do I just place widget.TabLayout inside widget.Toolbar?
You can place the tablayout widget inside the toolbarwidget like
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
android:background="?attr/colorPrimary">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</android.support.v7.widget.Toolbar>