I've made a custom toolbar and its Right-To-Left. and it looks like this:
I want a navigation drawer appears when I click on the right side icon. But when I add the navigation drawer to app, it crashes. I've tried many things and also searched a lot, but it didn't work. It'd be GREAT it someone would help me.
Here is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.playpersia.mycustomizedtoolbar.MainActivity"
>
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
android:fitsSystemWindows="true"
android:id="@+id/right_drawer">
<!-- Main layout -->
<FrameLayout
android:id="@+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
android:text="Hello"/>
</FrameLayout>
<!-- Nav drawer -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|end" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header_drawer"
android:src="@drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="@+id/list_view"
android:entries="@array/pages" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
right_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
android:fitsSystemWindows="true"
android:id="@+id/right_drawer"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header_drawer"
android:src="@drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="@+id/list_view"
android:entries="@array/pages" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java
package com.playpersia.mycustomizedtoolbar;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.zip.Inflater;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
public ListView listView;
public String[] pages;
ArrayAdapter<String> arrayAdapter;
public ImageView menu_icon;
public ImageView back_arrow;
public boolean mSlideState;
private DrawerLayout drawerLayout;
public ActionBarDrawerToggle _mDrawerTg;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
menu_icon = (ImageView) findViewById(R.id.menu_icon);
back_arrow = (ImageView) findViewById(R.id.back_button);
drawerLayout =(DrawerLayout) findViewById(R.id.right_drawer);
pages =getResources().getStringArray(R.array.pages);
_mDrawerTg =new ActionBarDrawerToggle(this, drawerLayout,
null, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view){
mSlideState=false;
}
public void onDrawerOpened (View drawerView){
super.onDrawerOpened(drawerView);
mSlideState=true;
} };
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item,pages));
drawerLayout.setDrawerListener(_mDrawerTg);
_mDrawerTg.syncState();
menu_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mSlideState){
drawerLayout.closeDrawer(GravityCompat.END);
}else{
drawerLayout.openDrawer(GravityCompat.END); } } }); }
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
_mDrawerTg.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
_mDrawerTg.onConfigurationChanged(newConfig);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (_mDrawerTg.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
}
UPDATE logcat:
01-18 12:34:25.386 29949-29949/com.playpersia.mycustomizedtoolbar
E/AndroidRuntime: FATAL EXCEPTION: main Process:
com.playpersia.mycustomizedtoolbar, PID: 29949 java.lang.RuntimeException:
Unable to start activityComponentInfo{com.playpersia.mycustomizedtoolbar/com.playpersia.mycustomizedtool
bar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)at
android.app.ActivityThread.access$800(ActivityThread.java:156)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)at
android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.playpersia.mycustomizedtoolbar.MainActivity.onCreate(MainActivity.java:53)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
UPDATE 2
now I have this toolbar when I click on the right side icon the drawer opens & closes, right side drawer