Quacksilber Quacksilber - 8 days ago 5
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".

MainActivity.java:

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}

@SuppressWarnings("StatementWithEmptyBody")
@Override
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) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new FirstFragment())
.commit();
} else if (id == R.id.nav_second_layout) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new SecondFragment())
.commit();
} else if (id == R.id.nav_third_layout) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new ThirdFragment())
.commit();
} else if (id == R.id.nav_share) {

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

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
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;

@Nullable
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();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
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}

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) myView.findViewById(R.id.webview);
myWebView.loadUrl("http://www.website" + kw + "/w/example.htm");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(false);

//WebView set scale
myWebView.setInitialScale(scale);

return myView;

}
}

Answer

In your current example, try replacing the code

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

with

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