Updated 13 April 2023
A Navigation component includes NavigationUI
.
We can use the top app bar which provides a consistent place along the top of your app for displaying information and actions from the current screen. Also, we can use the collapsing toolbar.
activity_main.xml
module:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/drawer_bg" android:fitsSystemWindows="true" tools:openDrawer="end"> <include layout="@layout/toolbar_custom_layout" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.google.android.material.navigation.NavigationView android:id="@+id/drawer" android:layout_width="273dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@color/drawer_bg" android:orientation="vertical"> <include layout="@layout/drawer_layout" /> </com.google.android.material.navigation.NavigationView> </androidx.drawerlayout.widget.DrawerLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
<?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/cardview_content" android:layout_width="match_parent" android:layout_height="match_parent" android:focusableInTouchMode="true" android:gravity="center_horizontal" android:orientation="vertical" app:cardElevation="10dp"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constraint_header_root" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" android:elevation="10dp"> <androidx.constraintlayout.widget.Guideline android:id="@+id/guidelinestart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintGuide_begin="18sp" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <!--Header--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constraint_main_head" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/rounded_bottom" android:paddingBottom="@dimen/margin_20" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar1" android:layout_width="match_parent" android:layout_height="30dp" android:layout_marginTop="36sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constraint_header" android:layout_width="match_parent" android:layout_height="@dimen/dimens_30dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@+id/image_drawer" android:layout_width="18sp" android:layout_height="match_parent" android:layout_marginStart="19dp" android:visibility="gone" android:src="@drawable/ic_menu" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginLeft="19dp" /> <TextView android:id="@+id/text_hea" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:gravity="start" android:maxLines="1" android:text="Demo" android:textAlignment="viewStart" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.appcompat.widget.Toolbar> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
private fun callSideDrawer() { cardview_content?.radius = 0f actionBarDrawerToggle = object : ActionBarDrawerToggle(this, drawerLayout, toolbar1, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { private val scaleFactor = 6f override fun onDrawerSlide(drawerView: View, slideOffset: Float) { super.onDrawerSlide(drawerView, slideOffset) val slideX = drawerView.width * slideOffset if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { cardview_content?.translationX = slideX if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { drawerLayout?.layoutDirection = View.LAYOUT_DIRECTION_LTR } } cardview_content?.scaleX = 1 - slideOffset / scaleFactor cardview_content?.scaleY = 1 - slideOffset / scaleFactor cardview_content?.radius = 30f Log.e("qwerty", "onDrawerSlide") } override fun onDrawerClosed(drawerView: View) { super.onDrawerClosed(drawerView) cardview_content?.radius = 0f Log.e("qwerty", "onDrawerClosed") } override fun onDrawerOpened(drawerView: View) { super.onDrawerOpened(drawerView) flagDrawer = true cardview_content?.radius = 30f Log.e("qwerty", "onDrawerOpened") } override fun onDrawerStateChanged(newState: Int) { super.onDrawerStateChanged(newState) if (!flagDrawer) { cardview_content?.radius = 0f } else { cardview_content?.radius = 30f } flagDrawer = false Log.e("qwerty", "onDrawerStateChanged") } } if (supportActionBar != null) { supportActionBar!!.setHomeButtonEnabled(true) supportActionBar!!.setDisplayHomeAsUpEnabled(true) } drawerLayout!!.setScrimColor(Color.TRANSPARENT) drawerLayout!!.drawerElevation = 0f drawerLayout!!.addDrawerListener(actionBarDrawerToggle as ActionBarDrawerToggle) (actionBarDrawerToggle as ActionBarDrawerToggle).isDrawerSlideAnimationEnabled = true //disable "hamburger to arrow" drawable (actionBarDrawerToggle as ActionBarDrawerToggle).isDrawerIndicatorEnabled = true //disable "hamburger to arrow" drawable (actionBarDrawerToggle as ActionBarDrawerToggle).syncState() } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
package com.example.myapplication import android.graphics.Color import android.os.Build import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.view.View import androidx.appcompat.app.ActionBarDrawerToggle import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.toolbar_custom_layout.* class MainActivity : AppCompatActivity() { private var actionBarDrawerToggle: ActionBarDrawerToggle? = null private var flagDrawer = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) callSideDrawer() } private fun callSideDrawer() { cardview_content?.radius = 0f actionBarDrawerToggle = object : ActionBarDrawerToggle(this, drawerLayout, toolbar1, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { private val scaleFactor = 6f override fun onDrawerSlide(drawerView: View, slideOffset: Float) { super.onDrawerSlide(drawerView, slideOffset) val slideX = drawerView.width * slideOffset if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { cardview_content?.translationX = slideX if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { drawerLayout?.layoutDirection = View.LAYOUT_DIRECTION_LTR } } cardview_content?.scaleX = 1 - slideOffset / scaleFactor cardview_content?.scaleY = 1 - slideOffset / scaleFactor cardview_content?.radius = 30f Log.e("qwerty", "onDrawerSlide") } override fun onDrawerClosed(drawerView: View) { super.onDrawerClosed(drawerView) cardview_content?.radius = 0f Log.e("qwerty", "onDrawerClosed") } override fun onDrawerOpened(drawerView: View) { super.onDrawerOpened(drawerView) flagDrawer = true cardview_content?.radius = 30f Log.e("qwerty", "onDrawerOpened") } override fun onDrawerStateChanged(newState: Int) { super.onDrawerStateChanged(newState) if (!flagDrawer) { cardview_content?.radius = 0f } else { cardview_content?.radius = 30f } flagDrawer = false Log.e("qwerty", "onDrawerStateChanged") } } if (supportActionBar != null) { supportActionBar!!.setHomeButtonEnabled(true) supportActionBar!!.setDisplayHomeAsUpEnabled(true) } drawerLayout!!.setScrimColor(Color.TRANSPARENT) drawerLayout!!.drawerElevation = 0f drawerLayout!!.addDrawerListener(actionBarDrawerToggle as ActionBarDrawerToggle) (actionBarDrawerToggle as ActionBarDrawerToggle).isDrawerSlideAnimationEnabled = true //disable "hamburger to arrow" drawable (actionBarDrawerToggle as ActionBarDrawerToggle).isDrawerIndicatorEnabled = true //disable "hamburger to arrow" drawable (actionBarDrawerToggle as ActionBarDrawerToggle).syncState() } } |
Another mentioned URL
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.