I have made an android app by using the android webview. This app is actually a mobile site which contains the different courses.
In the website: When I open the course page there is a course pdf link which when clicked opens in a new tab.
In the android app When the site is opened in the mobile app, the course link (In menu) itself is getting opened in a browser.
My requirement is that the pdf link in the course page should open in a new tab.
Below is the code which I have tried.
MainActivity.java
package com.education.xxxxxxx;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "";
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// myWebView.loadUrl("http://xxxxxxx.com/");
// myWebView.setWebViewClient(new WebViewClient());
String myPdfUrl = "http://xxxxxxx.com/";
if(myPdfUrl.endsWith(".pdf")){
String googledocs="https://docs.google.com/viewer?url=";
String pdf_url=googledocs+myPdfUrl;
myWebView.loadUrl(pdf_url);
}
else{
myWebView.loadUrl(myPdfUrl);
}
}
@Override
public void onBackPressed() {
if(myWebView.canGoBack()){
myWebView.goBack();
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}