0

I work on an Android App with Navigation Drawer, but when I added the code for the Drawer to the existing code (a webview and a bottom bar) the toolbar does not display. The Drawer works.

Here is my code:

MainActivity.java

package julians.de.test;

import android.content.res.Configuration;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnMenuTabSelectedListener;

public class MainActivity extends AppCompatActivity {
    private CoordinatorLayout coordinatorLayout;
    private SwipeRefreshLayout swipeContainer;

    Toolbar toolbar;

    DrawerLayout drawerLayoutgesamt;
    ActionBarDrawerToggle drawerToggle;


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

        toolbar = (Toolbar) findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);

        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.three_buttons_activity);
        swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);

        drawerLayoutgesamt = (DrawerLayout) findViewById(R.id.drawerlayoutgesamt);
        drawerToggle = new ActionBarDrawerToggle(MainActivity.this,drawerLayoutgesamt,R.string.auf, R.string.zu);
        drawerLayoutgesamt.setDrawerListener(drawerToggle);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        drawerToggle.syncState();

        swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);


        final WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.getSettings().setLoadWithOverviewMode(true);
        myWebView.getSettings().setUseWideViewPort(true);
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setDisplayZoomControls(true);
        myWebView.loadUrl("http://www.apple.com");

        swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                myWebView.reload();
                swipeContainer.setRefreshing(false);
            }
        });

        BottomBar bottomBar = BottomBar.attach(this, savedInstanceState);
        bottomBar.setItemsFromMenu(R.menu.three_buttons_menu, new OnMenuTabSelectedListener() {
            @Override
            public void onMenuItemSelected(int itemId) {
                switch (itemId) {
                    case R.id.recent_item:
                        myWebView.getSettings().setLoadWithOverviewMode(true);
                        myWebView.getSettings().setUseWideViewPort(true);
                        myWebView.getSettings().setBuiltInZoomControls(true);
                        myWebView.getSettings().setDisplayZoomControls(true);
                        myWebView.loadUrl("http://www.apple.com");
                        break;
                    case R.id.favorite_item:
                        myWebView.getSettings().setLoadWithOverviewMode(false);
                        myWebView.getSettings().setUseWideViewPort(false);
                        myWebView.getSettings().setBuiltInZoomControls(false);
                        myWebView.getSettings().setDisplayZoomControls(false);
                        myWebView.loadUrl("http://www.apple.com");
                        break;
                    case R.id.location_item:
                        myWebView.getSettings().setLoadWithOverviewMode(true);
                        myWebView.getSettings().setUseWideViewPort(true);
                        myWebView.getSettings().setBuiltInZoomControls(true);
                        myWebView.getSettings().setDisplayZoomControls(true);
                        myWebView.loadUrl("http://www.apple.com");
                        break;
                }
            }
        });

        bottomBar.setActiveTabColor("#C2185B");
        bottomBar.useDarkTheme(true);


    }

    @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 (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/three_buttons_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".MainActivity">



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

        <!-- Activity Layout-->

        <RelativeLayout
            android:layout_width="match_parent"
            android:id="@+id/activitylayout"
            android:layout_height="match_parent" >

            <include
                android:id="@+id/toolbar1"
                layout="@layout/tool_bar"
                />

            <android.support.v4.widget.SwipeRefreshLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/swipeContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


                <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/webview"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    />

            </android.support.v4.widget.SwipeRefreshLayout>
        </RelativeLayout>



        <!-- Drawer Layout -->

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/drawerlayoutsingle"
            android:layout_gravity="start"
            android:background="#fff"
            >

            <TextView
                android:text="Drawer Layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"

                />



        </RelativeLayout>





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




</android.support.design.widget.CoordinatorLayout>

tool_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toolbar1"
    android:background="#ffd3d3d3"
    android:layout_alignParentTop="true"
    android:minHeight="?attr/actionBarSize"
    android:fitsSystemWindows="true">
</android.support.v7.widget.Toolbar>

styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="drawerArrowStyle">@style/drawerarrowstyle1</item>

    </style>

    <style name="drawerarrowstyle1" parent="Widget.AppCompat.DrawerArrowToggle">

        <item name="spinBars">true</item>

    </style>
</resources>

Thanks for your help!

  • You've got the coordinator layout and DrawerLayout backwards. Compare here. http://stackoverflow.com/a/33413334/2308683 – OneCricketeer Nov 26 '16 at 16:16
  • When I change the order to Drawer Layout and then coordinator layout, only the content of the drawer is visible and the webview is hidden. –  Nov 26 '16 at 16:27
  • The order is not the only thing you had to change, it was only that start – OneCricketeer Nov 26 '16 at 16:33

2 Answers2

0

Have you tried using the Navigation Drawer template provided by android studio and if so use a fragment as main activity in place of the activity

SubGravity
  • 11
  • 2
  • Yes, but the template was too complicate for me. I found an easy tutorial and then used this code (the example app worked). Here is the [example code](https://github.com/derAndroidPro/Lollipop_NavigationDrawer_Tutorial) –  Nov 26 '16 at 16:10
  • @JulianSchäfer The template is correct, though. You can be sure of that. You can scrap the code you don't understand in the Activity, but the XML is all you're looking for – OneCricketeer Nov 26 '16 at 16:14
0

DrawerLayout acts as a top-level container for window content ... To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity

You should try this structure

DrawerLayout 
    CoordinatorLayout 
        AppBarLayout 
            <include> toolbar 
        <!-- activity layout (without toolbar) -->
    <!-- drawer layout  -->
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245