I have been researching for 2 days and I haven\'t found a similar problem anywhere on the internet!
From the main activity, I\'m trying to open a new activity contai
Don't try to load the given url in shouldOverrideUrlLoading
, and don't return true from it in your case either. Really, from what it looks like you're trying to do, you shouldn't need to override it at all.
Returning true from shouldOverrideUrlLoading
indicates to the WebView that it should not load the specified URL. What you're doing - telling the WebView to begin loading it again - is likely to cause some sort of internal loop inside the WebView, which I'd guess is why you're seeing odd behavior.
I can give the another way to solve this problem. Just copy the asset file into the data path
"/data/data/org.test.test/" and load it to WebView
in the following way.
String strHTMLFilePath = "file:///data/data/org.test.test/Files/help_android.html";
webView.loadUrl(strHTMLFilePath);
Use the following code to copy the HTML file from asset to data path
void getAssetFiles() {
String WriteFileName = "/data/data/org.test.test/";
try {
String list[] = thisActivity.getAssets().list("Files");
String outFileName = WriteFileName+"Files/";
if (list != null)
for (int i=0; i<list.length; ++i){
if(!new File(outFileName).exists()){
new File(outFileName).mkdirs();
}
OutputStream myOutput = new FileOutputStream(outFileName+list[i]);
byte[] buffer = new byte[1024];
int length;
String strWritePath = "Files" + "/" + list[i];
InputStream myInput = thisActivity.getAssets().open(strWritePath);
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
} catch (IOException e) {
}
}
May be it was due to hardware acceleration
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
worked for me.
https://developer.android.com/guide/topics/graphics/hardware-accel.html
Try this change in your text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.test"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".text" > <!-- Here is the change -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/green"
>
<TextView
android:id="@+id/apppagetitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="Odd News"
android:textSize="25px"
android:padding="10px"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/webkit"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
Just delete your virtual device and create a new one or test on your own Phone.
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}