0

I'm creating an app that has a navigation drawer. First it is working but when i edit the layout_gravity of my toolbar to end, i got an error

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT

MainActivity

I dont know what's wrong in my code but i change only the layout_gravity to end.

    public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private CharSequence mTitle;
    private CharSequence mDrawerTitle;
    private ActionBarDrawerToggle mDrawerToggle;
    private Toolbar topToolBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /****************************************************************************************
         *                                      DRAWER
         ****************************************************************************************/
        mTitle = mDrawerTitle = " ";

        topToolBar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(topToolBar);
        topToolBar.setLogo(R.drawable.logo);
        topToolBar.setLogoDescription(" ");

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        List<DrawerItem> drawerItems = new ArrayList<DrawerItem>();
        drawerItems.add(new DrawerItem("Home"));
        drawerItems.add(new DrawerItem("Student"));
        drawerItems.add(new DrawerItem("Teacher"));

        mDrawerList.setAdapter(new DrawerAdapter(this, drawerItems));
        mDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

            /**
             * Called when a drawer has settled in a completely closed state.
             */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /**
             * Called when a drawer has settled in a completely open state.
             */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.setDrawerIndicatorEnabled(true);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                selectItem(position);
            }
        });

    }

    private void selectItem(int position){

        switch(position) {
            default:
            case 0:
                break;
            case 1:
                Intent intent2 = new Intent(MainActivity.this, StudentActivity.class);
                startActivity(intent2);
                finish();
                break;
            case 2:
                Intent intent3 = new Intent(MainActivity.this, TeacherActivity.class);
                startActivity(intent3);
                finish();
                break;
        }

        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

xml

<LinearLayout 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"
    android:orientation="vertical"
    android:background="#ffffff">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="end"
        android:background="#ffffff"
        android:minHeight="?attr/actionBarSize" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#ffffff">


        </LinearLayout>

        <!-- NAVIGATION DRAWER -->
        <ListView android:id="@+id/left_drawer"
            android:layout_width="160dp"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:choiceMode="singleChoice"
            android:divider="#b1b1b1"
            android:dividerHeight="0.5dp"
            android:background="#ffffff"
            />

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

logcat error

FATAL EXCEPTION: main
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: Process: com.example.jathniel.drawertesting, PID: 13881
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1464)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at android.support.v7.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:288)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at android.support.v7.app.ActionBarDrawerToggle.onOptionsItemSelected(ActionBarDrawerToggle.java:278)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at com.example.jathniel.drawertesting.MainActivity.onOptionsItemSelected(MainActivity.java:141)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at android.app.Activity.onMenuItemSelected(Activity.java:3024)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
A. Mallavo
  • 29
  • 1
  • 5

3 Answers3

2

Add android:layout_gravity="left" in your DrawerLayout,

Nisarg
  • 1,358
  • 14
  • 30
2

check the following code. It is working.

<android.support.v4.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/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</RelativeLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view_left"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_marginBottom="50dp"
    android:layout_gravity="start"//I think you modify this line
    app:menu="@menu/drawer"/>

Prathap Badavath
  • 1,621
  • 2
  • 20
  • 24
1

It appears ActionBarDrawerToggle expects the drawer to be on the same side as the icon. It tries to open the drawer with GRAVITY_START (resolves to left in LTR layouts, right in RTL layouts).

You could copy the source of ActionBarDrawerToggle code and provide a way to specify which side the drawer is on when you construct it, and then use yours instead of the standard one. Other than that, I'm not sure how best to work around this since the method being called is private.

Karakuri
  • 38,365
  • 12
  • 84
  • 104