I have a webview, how can I find out which link was clicked in this webview?
Android/Java
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
tools:ignore="MissingConstraints">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:popupTheme="@style/AppTheme.PopupOverlay">
<include
android:id="@+id/back"
layout="@layout/top_menu_back" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<WebView
android:id="@+id/newsdetail"
android:layout_marginTop="60dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
hello, I have a webview, how can I find out which link was clicked in this webview?
Just use this code snippet. :)
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String link = url + "?header=0";
Log.d("getUrl", link);
view.loadUrl(link);
return false;
}
}
Example :
public class NewsDetail extends AppCompatActivity {
WebView newsdetail;
RelativeLayout navigation_icon;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_detail);
navigation_icon = findViewById(R.id.navigation_icon);
newsdetail = findViewById(R.id.newsdetail);
newsdetail.loadUrl(getIntent().getStringExtra("newsdetail"));
newsdetail.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = newsdetail.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String link = url + "?header=0";
Log.d("getUrl", link);
view.loadUrl(link);
return false;
}
}
}
Note: If the header parameter is allowed by the backend, it does not show the header in the application. To be able to use it I guess it must also be allowed by the backend. Like the header=0 parameter, as in the example.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments