Good day. I use the MaterialDrawer library to display the side pull-out menu. It is required to add a button in the header menu. Tried it like this: added a button to drawer_header.xml :
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:orientation="vertical" android:scaleType="fitCenter" android:src="@drawable/header" android:id="@+id/imageView" > </ImageView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="plus" android:id="@+id/button_size_plus" android:layout_gravity="right" android:layout_alignBottom="@+id/imageView" /> </RelativeLayout> Then, as usual, I declare a button in MainActivity : Button button_size_plus; and then initialize it to onCreate : button_size_plus = (Button)findViewById(R.id.button_size_plus); . Next, I create a setOnClickListener listener for the button and then when I start the application I get crash with NPE, it means that I had incorrectly initialized the button and could not “reach” it in MaterialDrawer. Please tell me how to do it correctly. I will attach an example of initializing a single field (line) in MaterialDrawer and creating the object itself:
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(22).withName(R.string.about_name).withSelectable(false).withIcon(R.drawable.about); result = new DrawerBuilder() .withActivity(this) .withToolbar(toolbar) .withHeader(R.layout.drawer_header) .withTranslucentStatusBar(true) .withActionBarDrawerToggleAnimated(true) .addDrawerItems( item1 ) .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { @Override public boolean onItemClick(View view, int position, IDrawerItem drawerItem) { } return true; } }) .build(); UPD
View viewInHeader = result.getDrawerLayout().findViewById(R.id.button_size_plus); viewInHeader.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getBaseContext(), "header", Toast.LENGTH_SHORT).show(); } });