Unable To Make Toolbar Transparent In Android
My tool bar always stays gray when I try to set the background as transparent. Here is my XML.
- Define a layout for your activity like below
- Make sure the Toolbar is at the bottom of the XML so it'll be on top in the z-order.
- Use RelativeLayout so you can make sure the Toolbar is visually at the top left on the screen.
- @color/primary should be transparent (#00000000) OR you can set it in code if you need to use other colors as well.
(OPTIONALLY) Either add "android:layout_marginTop="?android:attr/actionBarSize" to the container below OR add it in your code. Otherwise, some of the content in the FrameLayout will be underneath the action bar.
<android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <ListView android:id="@+id/left_drawer" android:layout_width="260dp" android:layout_height="match_parent" android:layout_gravity="start" android:layout_marginRight="8dp" android:layout_marginTop="?android:attr/actionBarSize" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="@color/drawer_background" /> </android.support.v4.widget.DrawerLayout> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="@color/primary" />
Solution 2:
for the latest update of android x
use @color/transparent = #00000000
use of
android:background="@color/transparent"android:elevation="0dp"app:elevation="0dp"
on appbar
example layout
<?xml version="1.0" encoding="utf-8"?><layoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"><androidx.drawerlayout.widget.DrawerLayoutandroid:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><com.google.android.material.appbar.AppBarLayoutandroid:id="@+id/appbar"android:layout_width="match_parent"android:layout_height="wrap_content"app:layout_constraintTop_toTopOf="parent"android:background="@color/transparent"android:elevation="0dp"app:elevation="0dp"
><androidx.appcompat.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"app:titleMarginStart="@dimen/margin_big"
/></com.google.android.material.appbar.AppBarLayout><fragmentandroid:id="@+id/nav_fragment"android:name="androidx.navigation.fragment.NavHostFragment"android:layout_width="match_parent"android:layout_height="match_parent"app:defaultNavHost="true"app:navGraph="@navigation/nav_main"
/><com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/bottom_nav"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:background="@color/bottom"app:menu="@menu/navigation"app:layout_constraintBottom_toBottomOf="parent"
/></androidx.constraintlayout.widget.ConstraintLayout><com.google.android.material.navigation.NavigationViewandroid:id="@+id/nav_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="start"app:menu="@menu/navigation"
/></androidx.drawerlayout.widget.DrawerLayout></layout>
Post a Comment for "Unable To Make Toolbar Transparent In Android"