0

I have a hamburger button in custom toolbar. when I click button, I want to open slide menu. My project is debug, but when I click the hamburger button , slide menu not open. Any idea?

enter image description here

MainActivity:

       class MainActivity : AppCompatActivity() {
       private lateinit var userPresenter: UserPresenterInterface
       private var drawerLayout: DrawerLayout? = null
       private var toggle: ActionBarDrawerToggle? = null
       private var rightIcon: Toolbar? = null
       private var navigationView: NavigationView? = null


    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    userPresenter = UserPresenter(resources)
    supportActionBar?.elevation = 0f

        rightIcon?.setOnClickListener {

        drawerLayout = findViewById(R.id.drawer)
        rightIcon = findViewById(R.id.right_icon)
        setSupportActionBar(rightIcon)
        toggle = ActionBarDrawerToggle(this, drawerLayout, rightIcon, 
        R.string.open, R.string.close)
        drawerLayout!!.addDrawerListener(toggle!!)
        toggle!!.syncState()

        navigationView = findViewById(R.id.nav_view)
        }

custom_toolbar.xml:

   <RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/white">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:id="@+id/right_icon"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_baseline_menu_24"
        android:layout_alignParentRight="true" />
      </RelativeLayout>
     </RelativeLayout>
  • Why are u making a custom toobar for it . You can change the Gravity of drawer layout to End and it will look same as in the question . – ADM Mar 08 '21 at 09:01
  • @ADM I need hamberger menu like that positiom. that is why ı need custom toolbar. –  Mar 08 '21 at 09:03
  • Does this answer your question? [How to open navigation drawer on button click in main fragment?](https://stackoverflow.com/questions/19442841/how-to-open-navigation-drawer-on-button-click-in-main-fragment) – ADM Mar 08 '21 at 09:06
  • An imageview is not clickable by default - try to add "Clickable=true" and perhaps "Focusable=true" as well. Hope it helps. – Mathias Kirkegaard Mar 08 '21 at 09:12
  • @ADM I dont understand how to implement to my code, can u do it* –  Mar 08 '21 at 09:18

2 Answers2

0

try to add Image click listner and show and hide your drawer layout like this

rightIcon.setOnClickListener {
        if (drawerLayout.isOpen) {
            drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            drawerLayout.openDrawer(GravityCompat.START)
        }
    }
Amit pandey
  • 1,149
  • 1
  • 4
  • 15
-1

An imageview is not clickable by default - try to add "Clickable=true" and perhaps "Focusable=true" as well.

Dharman
  • 30,962
  • 25
  • 85
  • 135