Issue
I have a webview in a fragment, the webview has multiple links. I want when the user clicks on one of those links, for example, a Readmore link it opens the in-app browser I created (BrowserActivity). How can I achieve this?
BrowserActivity.java
public class BrowserActivity extends AppCompatActivity {
private String Url ;
private WebView Tecwebview;
private ProgressBar progressBar;
ImageButton back, forward, stop, refresh, homeButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
back = (ImageButton) findViewById(R.id.back_arrow);
forward = (ImageButton) findViewById(R.id.forward_arrow);
stop = (ImageButton) findViewById(R.id.stop);
refresh = (ImageButton) findViewById(R.id.refresh);
homeButton = (ImageButton) findViewById(R.id.home);
progressBar = (ProgressBar) findViewById(R.id.probar);
progressBar.setMax(100);
Tecwebview = (WebView) findViewById(R.id.tecwebview);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Tecwebview.loadUrl(Url);
if (savedInstanceState != null) {
Tecwebview.restoreState(savedInstanceState);
} else {
Tecwebview.getSettings().setJavaScriptEnabled(true);
Tecwebview.getSettings().setUseWideViewPort(true);
Tecwebview.getSettings().setLoadWithOverviewMode(true);
Tecwebview.getSettings().setSupportZoom(true);
Tecwebview.getSettings().setSupportMultipleWindows(true);
Tecwebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
Tecwebview.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
if (newProgress < 100 && progressBar.getVisibility() == ProgressBar.GONE) {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
if (newProgress == 100) {
progressBar.setVisibility(ProgressBar.GONE);
} else {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
}
});
}
Tecwebview.setWebViewClient(new MyWebViewClient());
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Tecwebview.canGoBack()) {
Tecwebview.goBack();
}
}
});
forward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Tecwebview.canGoForward()) {
Tecwebview.goForward();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Tecwebview.stopLoading();
}
});
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Tecwebview.reload();
}
});
homeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Tecwebview.loadUrl("https://google.com");
}
});
}
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.browser, menu);
return true;
}
Fragment
public class NewsFragment extends Fragment {
private WebView Tecwebview;
public NewsFragment() {
}
LinearLayout Weblinearlayout;
SwipeRefreshLayout Webswipelayout;
ProgressBar progressBar;
String url = "http://someweb.com/news/";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
Tecwebview = (WebView) v.findViewById(R.id.tecwebview);
progressBar = (ProgressBar) v.findViewById(R.id.probar);
Weblinearlayout = v.findViewById(R.id.layout);
Webswipelayout = v.findViewById(R.id.swipe);
progressBar.setMax(100);
WebSettings Websettings = Tecwebview.getSettings();
Websettings.setJavaScriptEnabled(true);
Tecwebview.setWebViewClient(new WebViewClient());
Tecwebview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress < 100 && progressBar.getVisibility() == ProgressBar.GONE) {
progressBar.setVisibility(ProgressBar.VISIBLE);
Tecwebview.setVisibility(View.GONE);
}
if (progress == 100) {
progressBar.setVisibility(ProgressBar.GONE);
Tecwebview.setVisibility(View.VISIBLE);
}
}
});
Webswipelayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Tecwebview.reload();
LoadWeb();
}
});
Tecwebview.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (Tecwebview.getScrollY()==0){
Webswipelayout.setEnabled(true);
} else {
Webswipelayout.setEnabled(false);
}
}
});
Tecwebview.setWebViewClient(new HomeFragment.MyCustomWebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Weblinearlayout.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
Weblinearlayout.setVisibility(View.GONE);
Webswipelayout.setRefreshing(false);
super.onPageFinished(view, url);
}
});
LoadWeb();
// This code if for the on back press within webview
Tecwebview.canGoBack();
Tecwebview.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& Tecwebview.canGoBack()) {
Tecwebview.goBack();
return true;
}
return false;
}
});
return v;
}
public void LoadWeb() {
Tecwebview.loadUrl(url);
Webswipelayout.setRefreshing(true);
}
This is for browser activity to make the links in browser activity webview only opens in the webview
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
CookieManager.getInstance().setAcceptCookie(true);
return true;
}
Solution
According to my understanding you have a url e.g, www.xyz.com and you want to load your Activity
when the user clicks on it.
If that is true then try following code but change the value of targeUrl
to your link address and also launch the activity in the area where I have wrote comment...
Tecwebview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String targetUrl = "https//www.somelinksaddress.com/me/you";
if (targetUrl.equals(url)) {
// Start your activity here
} else {
view.loadUrl(url);
}
return true;
}
});
Answered By - SickBoy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.