Quacksilber Quacksilber - 9 months ago 55
Android Question

Drawer not responiding after "launching" fragment

I've been working on an app which contains a WebView. I wasn't able to make the WebView-content fit the device screen on it's own (website doesn't support mobile phones) so I use DisplayMetrics to get the screen dimensions of the device and the scale the website manualy with an if-clause depending on the screen size.

Everything worked properly until I decided to use an drawer for navigation and thus, use fragments.

My Problem is, by opening the "FirstFragment" which should contain the Webview (LayoutEmulator show the Webview) and the drawer can't be used anymore from this point on. The drawer shows up but there is no possiblity to click on it to open up other fragments, also the hamburger icon in the toolbar disapperas after opening "FirstFragment".


package user.app;

import android.app.FragmentManager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

{unnecessary code}

public boolean onNavigationItemSelected(MenuItem item) {

// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentManager = getFragmentManager();

if (id == R.id.nav_first_layout) {
, new FirstFragment())
} else if (id == R.id.nav_second_layout) {
, new SecondFragment())
} else if (id == R.id.nav_third_layout) {
, new ThirdFragment())
} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {


DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
return true;

FirstFragment.java: (The Url contains a variable)

package user.app;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import java.util.Calendar;

public class FirstFragment extends Fragment {

View myView;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.first_layout, container, false);

{from here to the end old code that worked properly in a public class}
//get calendar week
int kw = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR) - 1;

//get screen meassures
DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels;
int height = dm.heightPixels;
int dens = dm.densityDpi;
double wi = (double) width / (double) dens;
double hi = (double) height / (double) dens;
double x = Math.pow(wi, 2);
double y = Math.pow(hi, 2);
double screenInches = Math.sqrt(x + y);

{if-clause that sets scale depending on screen size,
int scale}

WebView myWebView = (WebView) myView.findViewById(R.id.webview);
myWebView.loadUrl("http://www.website" + kw + "/w/example.htm");
WebSettings webSettings = myWebView.getSettings();


//WebView set scale

return myView;


Answer Source

In your current example, try replacing the code

WebView myWebView = (WebView) findViewById(R.id.webview);


WebView myWebView = (WebView) myView.findViewById(R.id.webview);