android webview to download pdf in webview app - java

I have created an app with android webview. It loads google chrome website. How can I restrict it to open other links in the same browser app and not go to phone's default browser? Also with the below code I am able to open and download random pdf by search on google(webview). I need to be able to open in same webview and also be able to download pdf in the same webview app. How?
public class WebViewPage extends AppCompatActivity {
WebView webView;
Button closeweb;
WebViewClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view_page);
webView =findViewById(R.id.webview);
closeweb = findViewById(R.id.closeweb);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl("https://www.google.in");
closeweb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(WebViewPage.this, HomePage.class);
startActivity(intent);
}
});
}
#Override
public void onBackPressed() {
}
}
WebViewClientImpl.class
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.indexOf("https://www.google.in") > -1 ) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
}

Related

pass a link in a webview

I am trying when someone click for example : eee.com , to open it in my Webview app
public class MainActivity extends AppCompatActivity {
public static WebView webView;
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.mainWebview);
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.eee.com/");
webView.setWebViewClient(new WebViewClient(){
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("eee.com")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
});
}
#Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
webView.loadUrl(uri.toString());
}
but the app crashes on open it right away , I tried this : :
#Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
if(getIntent().getData() != null){
webView.loadUrl(uri.toString());
}
}
app works but on link click it just open the app without passing the link
I solved it my self with :
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
Uri x = intent.getData();
webView.loadUrl(x.toString());
}

How to use 'Service' in an internet browser App

I am newbie so please forgive me for anything wrong that I am doing or if I am asking the question in the wrong way.
I recently created a simple media player on android studio and used Java Service to play the media when I closed the app and it works fine. Now I have created a simple web browser app using WebView and I want to use the same "Java Service" to keep my web browser app running even when it is minimized but I have no idea how to convert the media player code to use on the web browser app. I have researched a lot and nobody else seems to want to create a browser that keeps running in the background. Please can you assist me in solving this?
Because I am new to Java, it is difficult for me to try and convert the code from media player to web browser
So here is my media player code:
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button start, stop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.buttonStartService);
stop = (Button) findViewById(R.id.buttonStopService);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (view == start){
startService(new Intent(this, MyService.class));
}else if(view == stop){
stopService(new Intent(this, MyService.class));
}
}
}
MyService.java
public class MyService extends Service {
private MediaPlayer player;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
player.setLooping(true);
player.start();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
}
Here is my MainActivity for the Webbrowser that I have created:
public class MainActivity extends AppCompatActivity {
WebView browser;
EditText link;
Button btn_visit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView) findViewById(R.id.webview);
link = (EditText) findViewById(R.id.editTextLink);
btn_visit = (Button) findViewById(R.id.btn_view);
browser.setWebViewClient(new myWebViewClient());
btn_visit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String url = link.getText().toString();
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.loadUrl(url);
}
});
}
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
```
So now I need to create another MyService that will allow the webbrowser to run in the
background but dont know how to go about it:
```
public class MyService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
}
#Override
public void onDestroy() {
}
}
As I said, there no errors that I am getting on the media player app. I just want to be able to use the same code on the internet browser app but don't know how to convert it.

Prevent Double Tap on Android WebView

I have a fullscreen WebView and I want to;
If my user double tap the screen, prevent second tap.
I am very very new on Android, please be very clear. I pasted my codes;
MainActivity.java
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// REMOTE RESOURCE
mWebView.loadUrl("https://www.example.com");
mWebView.setWebViewClient(new MyWebViewClient());
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
MyWebViewClient.java
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().endsWith("www.example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}

Enable/Disable WebView In another activity

I want to implement WebView in a such way that it should get instantiated in one Activity (along with loading of html) it should not be visible on first page and only webview is visible in another activity. I have tried following way, but unable to achieve it, following is my MainActivity
public class MainActivity extends Activity {
private WebView webView = null;
private Button startAppBtn = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createWebView();
startAppBtn = (Button) findViewById(R.id.button1);
startAppBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SecondActivity.class);
Bundle bundle = new Bundle();
intent.putExtra("state",bundle);
webView.saveState(bundle);
startActivity(intent);
}
});
}
public void createWebView(){
setWebView((WebView) findViewById(R.id.webview1));
getWebView().getSettings().setJavaScriptEnabled(true);
getWebView().getSettings().setDatabaseEnabled(true);
getWebView().getSettings().setLoadWithOverviewMode(true);
getWebView().setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
getWebView().setScrollbarFadingEnabled(false);
getWebView().loadUrl("file:///android_asset/My.html");
getWebView().setVisibility(View.GONE);
}
public WebView getWebView() {
return webView;
}
public void setWebView(WebView webView) {
this.webView = webView;
}
}
and following is SecondActivity,
public class SecondActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Intent intent = getIntent();
WebView webview = (WebView) findViewById(R.id.webview1);//at this line I am getting exception, I think because webview is in activity_main layout.
webview.restoreState((Bundle) intent.getParcelableExtra("state"));
webview.getSettings().setJavaScriptEnabled(true);
webview.setVisibility(View.VISIBLE);
}
}
I am new to android world, struggling on it please anybody can help me to fix this issue.
Hope this helps
First Activity
startAppBtn = (Button) findViewById(R.id.button1);
startAppBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("websiteURL", "file:///android_asset/My.html");
intent.putExtras(bundle);
startActivity(intent);
}
});
Second Activity
String sWebsite;
Bundle bundle = getIntent().getExtras();
if (bundle != null)
{
sWebsite = bundle.getString("websiteURL");
}
else if (bundle== null)
{
System.out.println("Null");
}
WebView webView = (WebView) findViewById(R.id.webview1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(sWebsite);
webView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
});

onBackPressed() crashes my webview app

I am new to android development, I have tried to override onBackPress() to implement webView.GoBack(). But on pressing back key my apps getting crashed. Here is my MainActivity.java code. Am I doing something wrong ??
public class MainActivity extends AppCompatActivity {
private WebView webview;
/** Called when the activity is first created. */
public void onBackPressed (){
if (webview.isFocused() && webview.canGoBack()) {
webview.goBack();
}
else {
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyCustomWebViewClient());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
view.loadUrl("http://url");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (view.canGoBack()) {
view.goBack();
} else {
view.loadUrl("file:///android_asset/index.html");
}
Toast.makeText(getBaseContext(), description, Toast.LENGTH_LONG).show();
}
}
}
Just because your webView is null. You are not referencing it anywhere. Modified your code.
public class MainActivity extends AppCompatActivity {
private WebView view;
/** Called when the activity is first created. */
public void onBackPressed (){
if (view.isFocused() && view.canGoBack()) {
view.goBack();
}
else {
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyCustomWebViewClient());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
view.loadUrl("http://url");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (view.canGoBack()) {
view.goBack();
} else {
view.loadUrl("file:///android_asset/index.html");
}
Toast.makeText(getBaseContext(), description, Toast.LENGTH_LONG).show();
}
}
}
In onCreate you have:
WebView view = (WebView) findViewById(R.id.webView);
The WebView you have defined at the top has not been initialized, and is the one being used by the onBackPressed method, to fix this all you need to do is remove WebView before initializing the variable in onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyCustomWebViewClient());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
view.loadUrl("http://url");
}
Actually you can not do directly inside the fragment. The onBackPressed can be overridden in the FragmentActivity. What you can do is:
1.Override the onBackPressed inside the activity.
2.When the onBackPressed is called, check if the instance of the current fragment is the instance showing the webview.
3.If it is, ask the fragment if the webview can go back.
4.If it is not, call the super or whatever you need
#Override
public void onBackPressed() {
Fragment webview = getSupportFragmentManager().findFragmentByTag("webview");
if (webview instanceof MyWebViewFragment) {
boolean goback = ((MyWebViewFragment)webview).canGoBack();
if (!goback)
super.onBackPressed();
}
}

Categories