whayway
whayway

Reputation: 33

Android studio can't see id of layout inside class

I'm creating sliding menu, this is how looks like my activity_main.xml file


<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".app.MainActivity">

   (...)

</androidx.drawerlayout.widget.DrawerLayout>

And inside MainActivity when I try to create ActionBarDrawToggle like that:

toggle = ActionBarDrawerToggle(this, drawerLayout , R.string.open, R.string.close)

but drawerLayout is not know, highlighted on red. I've try to change that by "androidx.drawerlayout.widget.DrawerLayout", invalidate cache, but without result.

Upvotes: 0

Views: 246

Answers (1)

mhdwajeeh.95
mhdwajeeh.95

Reputation: 427

You need to pass your DrawerLayout View Object after you inflate your activity in the onCreate method, the code would look like this:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.your_activity_layout)

        val drawerLayout = findViewById<DrawerLayout>(R.id.drawerLayout)
        toggle = ActionBarDrawerToggle(this, drawerLayout , R.string.open, R.string.close)

    }

}

Upvotes: 1

Related Questions