Reputation: 703
I am developing an Android app and I have stuck with a strange issue.
I have two buttons : button1 and button2
On clicking on buttons, I am loading different html files from asset folder.
This is working fine on emulators for all versions but on real device which have Android 4.0 or greater, performing a strange behavior.
For button2, It is working fine and showing html pages dynamically in web view but on clicking on button1 it is showing a white page in web view.
I am surprised why so is happening while xml and activities has same code except their ids and name.
I have tried a lot of things and searched but did not get why it is happening with one but not with another?
Xml for button1 is:
<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:background="@drawable/xxx">
<WebView
android:id="@+id/webViewButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:layerType="software" />
</LinearLayout>
Xml for button2 is:
<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:background="@drawable/xxxx">
<WebView
android:id="@+id/webViewButton2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:layerType="software" />
</LinearLayout>
Button1 Activity code:
@SuppressLint("DefaultLocale")
public class Button1Activity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_button1);
savedInstanceState = getIntent().getExtras();
String sign = null;
sign = savedInstanceState.getString("button1");
webView = (WebView) findViewById(R.id.webViewButton1);
webView.setBackgroundColor(0);
webView.loadUrl("file:///android_asset/" + sign + ".html");
}
}
Button2 Activity code:
@SuppressLint("DefaultLocale")
public class Button1Activity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_button2);
savedInstanceState = getIntent().getExtras();
String sign = null;
sign = savedInstanceState.getString("button2");
webView = (WebView) findViewById(R.id.webViewButton2);
webView.setBackgroundColor(0);
webView.loadUrl("file:///android_asset/" + sign + ".html");
}
}
Please guide me.
Upvotes: 1
Views: 1206
Reputation: 703
I solved my problem.
Actually I was putting the above Linear layout under the Scroll View to scroll the Linear layout. As I removed this Scroll view, my web view started working fine and it add vertical scrolling to the contents automatically as it fill the web view height.
So I learnt from this that WebView does not work properly under scroll view.
Upvotes: 2