Reputation: 395
I am trying to start an activity with webview layout, without opening up a new webbrowser. So it would be embeded in the app.
Seems like it worked for a few days and now... I'm only getting a white blank screen. Could you please give me a hand here?
Here's my xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/backBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:height="50dip"
android:background="#4d867f"
android:text="Back"
android:textColor="#f9f9f9"
android:textSize="20dip" />
</LinearLayout>
</RelativeLayout>
Here's how I am calling the activity:
promotionBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, PromotionActivity.class);
intent.putExtra("url", "https://www.test.com/apppromotion.html");
startActivity(intent);
}
});
And finally the activity:
package br.com.test.androidtools;
import android.content.Intent;
import android.net.Uri;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
import android.webkit.WebViewClient;
public class PromotionActivity extends Activity {
private WebView webView;
private Button backBtn;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
Intent it = new Intent(PromotionActivity.this, MainActivity.class);
startActivity((Intent)it);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.promotion_layout);
backBtn = (Button) findViewById(R.id.backBtn);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
Bundle bundle = getIntent().getExtras();
webView.loadUrl(bundle.getString("url"));
webView.setWebViewClient(new myWebViewClient());
backBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent it = new Intent(PromotionActivity.this, MainActivity.class);
startActivity((Intent)it);
}
});
}
public class myWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Any ideas of what could fix this? Thanks!
Upvotes: 1
Views: 6652
Reputation: 395
The url target had its SSL "Lets Encrypt" incorrectly configured. So simple, but it took me a while to find out! So before looking for a hard to crack solution, just point webviewer to another url.
Upvotes: 2
Reputation: 8237
Change
Bundle bundle = getIntent().getExtras();
webView.loadUrl(bundle.getString("url"));
To
webView.loadUrl(getIntent().getStringExtra("url"));
Note
If you use Bundle
,the next Activity can use Bundle bundle = getIntent().getExtras();
.
Edit
Add permission in your manifest .
<uses-permission android:name="android.permission.INTERNET" />
Add setting
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setJavaScriptEnabled(true);
Make sure that https://www.test.com/apppromotion.html
can load normally .
Edit
Make sure that the back button can display in the screen .
<RelativeLayout 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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="@+id/backBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:height="50dip"
android:background="#4d867f"
android:text="Back"
android:textColor="#f9f9f9"
android:textSize="20dip" />
</LinearLayout>
</RelativeLayout>
Edit
You can try this .
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layerType="hardware"/>
You can use android:layerType
in the xml code .
Upvotes: 0