I’m trying to build a navigation drawer with few items in it. but it’s getting me error for the addDrawerListener method at code below:
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else{
super.onBackPressed();
}
}
}
here is the AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity android:name=".MainActivity"
android:theme="@style/Theme.MyApplication.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
this is the menu ( drawer_menu.xml
) which seems to be working just ok
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn = "navigation_view">
<group android:checkableBehavior="single">
<item android:id="@+id/nav_people"
android:icon="@drawable/ic_people"
android:title="@string/people" />
<item android:id="@+id/nav_goods"
android:icon="@drawable/ic_goods"
android:title="@string/goods" />
<item android:id="@+id/nav_delivery"
android:icon="@drawable/ic_delivery"
android:title="@string/delivery" />
<item android:id="@+id/nav_saloon"
android:icon="@drawable/ic_saloon"
android:title="@string/saloon" />
<item android:id="@+id/nav_setting"
android:icon="@drawable/ic_setting"
android:title="@string/setting" />
<item android:id="@+id/nav_help"
android:icon="@drawable/ic_help"
android:title="@string/help" />
<item android:id="@+id/nav_update"
android:icon="@drawable/ic_update"
android:title="@string/update" />
<item android:id="@+id/nav_about_us"
android:icon="@drawable/ic_about_us"
android:title="@string/about_us" />
<item android:id="@+id/nav_quit"
android:icon="@drawable/ic_exit"
android:title="@string/exit" />
</group>
</menu>
there’s also a header ( nav_header.xml
) which has a small picture and two textviews
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/nav_header"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="@string/strong_text"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/weak_text" />
</LinearLayout>
here is the themes.xml
file. the tutorial which I’m trying to follow is a little outdated and has styles.xml file instead of themes.xml but I’m not sure. Im copy-pasting the <style name="Theme.MyApplication.NoActionBar">
part in themes.xml (night)
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="Theme.MyApplication.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
3
Answers
You need to initialize drawer before using it.
(Note : in param you need to pass the id of DrawerLayout from xml as you have for Toolbar).
this should be before you use drawer, before the line below in your case.
You have to bind the drawerLayout id as you did it with toolbar.
Just bind the id you have for drawer in XML before using the "drawer" object.
Add below line under find toolbar id –