WebView Upload "choose file" does nothing - android

I would like to be able to upload a file from an Android 4.x device, whenever I select "choose file" nothing happens.
It works when I use Chrome directly but not with the WebViewClient from within the app.
My code:
public class ActivityWebView extends MainActivity {
private WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String websiteURL = "url";
wv = (WebView) findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(websiteURL);
wv.setWebViewClient(new WebViewClientClass());
wv.getSettings().setSupportZoom(false);
wv.getSettings().setBuiltInZoomControls(false);
}
public class WebViewClientClass extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event)
{
if ((KeyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack())
{
wv.goBack();
return true;
}
return super.onKeyDown(KeyCode, event);
}

Related

How can I solve WebViewClient breaking application? [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I am trying to add a WebViewClient to my WebView application. But every time I try to start it with WebViewClient, it stops working.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyWebViewClient webViewClient = new MyWebViewClient(this);
myWebView.setWebViewClient(webViewClient);
myWebView = findViewById(R.id.webview);
myWebView.loadUrl("https://www.google.com.br/");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
MyWebViewClient.java
public class MyWebViewClient extends WebViewClient {
private Activity activity = null;
public MyWebViewClient(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.contains("https://google.com/")) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
}
When I comment the line myWebView.setWebViewClient(webViewClient); in the MainActivity.java the application works again. Any ideas on how to solve this WebVIewClient problem?
Here you go, you are setting the client on myWebView before its
initialization.
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//should be initialized first
myWebView = findViewById(R.id.webview);
MyWebViewClient webViewClient = new MyWebViewClient(this);
myWebView.setWebViewClient(webViewClient);
myWebView.loadUrl("https://www.google.com.br/");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

How can I block a URL in a WebView?

public class WebViewActivity extends AppCompatActivity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webview = new WebView(this);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);
}
private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
return true;
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://stackoverflow.com")) {
return false;
}
return true;
}
}
I want to block an url in the WebView, how to block the url? This code doesn't work.
You should return true if you want specific behavior according to documentation.
Returns true if the host application wants to leave the current
WebView and handle the url itself, otherwise return false.
block all the urls working solution even i tried it.
public class MainActivity extends Activity {
private WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wv1 = new WebView(this);
setContentView(wv1);
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl("http://www.kart.epizy.com");
wv1.setWebViewClient(new MyWebViewClient());
wv1.setClickable(true);
}
public class MyWebViewClient extends WebViewClient {
public boolean shuldOverrideKeyEvent(WebView view, KeyEvent event) {
// Do something with the event here
return true;
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//return false to allow urls or add your condition like
//return url.equals("https://play.google.com/store/apps/developer?id=kArt+Developers");
return true;
}
} }

Android WebView Stops while clicking Back

My webview module isn't working. Whenever I try to navigate back from any webpage, the app crashes giving following error
"Unfortunately the app has stopped".
Can anyone please explain what is the reason?
public class MainActivity extends ActionBarActivity {
WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("http://nahdaschools.com/v4/index.php/apps");
}
private class MyCustomWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed() {
if (myWebView.copyBackForwardList().getCurrentIndex() > 0) {
myWebView.goBack();
}
else {
// Your exit alert code, or alternatively line below to finish
super.onBackPressed(); // finishes activity
}
}}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(myWebView.canGoBack()){
myWebView.goBack();
}
else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
with this you will be able to come on your last activity.
#Override
public void onBackPressed() {
super.onBackPressed();
moveTaskToBack(false);// finishes activity
}
}}

WebView also Gone when Sreen Time-Out

I would like to find a solution to allow my WebView still alive when screen time-out.
I am program to play music using WebView, it's working very properly if the screen alive but when the screen time-out my music in WebView also GONE.
Here is my code:
public class WebViewActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl("http://mp3skull.com/usher.html");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onPause()
{
mWebView.setVisibility(View.GONE);
mWebView.destroy();
super.onPause();
}
}
I am very shy to tell you that i tried to find this solution over a week but still can't done :(.
Hope you give me solution on this matter.
Thanks you.
Remove onPause() method in your class like below code:
public class WebViewActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl("http://mp3skull.com/usher.html");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

Stop the Music in WebView when Screen Time-Out

I am programming that allow users can listen music in WebView via the URL web link.
But I wonder why when the android phone user screen Time-Out my music in WebView also stop.
Do you have any solution to solve this problem?
Here is my code:
public class PlayMusicActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl("http://www.myweb.com/music.html");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onPause() {
mWebView.setVisibility(View.GONE);
mWebView.destroy();
super.onPause();
}
}
Appreciate for your help.
Best regards,
Virak

Categories

Resources