Webview start always in desktop mode - java

It's two days i can't start my webview directly in mobile mode. When i open the application for the first time starts the webview displays the desktop. My code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS); // Request progress circle
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
setProgressBarIndeterminateVisibility(true); // Show progress circle
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
setProgressBarIndeterminateVisibility(false); // Hide progress circle when page loaded
activity.setTitle("Title");
}
});
if (savedInstanceState == null)
{
webview.loadUrl("http://forum.ubuntu-it.org/");
}
}
Even if i changed the User Agent doesn't work! I tryied every way. I hope in a help from someone, Thanks.

Add this line before loadUrl()
webview.setWebViewClient(new WebViewClient());

Related

Android Studio WebView Youtube Video BlackScreen

I want to play a video in the WebView but somehow I get an blackscreen on the video but I can hear the sound of it. Would be really glad if someone can help me
public class MainActivity extends AppCompatActivity {
WebView displayYoutubeVideo;
TextView button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String frameVideo = "<html><body>Video From YouTube<br><iframe
width=\"560\" height=\"315\"
src=\"https://www.youtube.com/embed/WtO3_tF8niU\" frameborder=\"0\"
allowfullscreen></iframe></body></html>";
WebView displayYoutubeVideo = (WebView) findViewById(R.id.youtube_view);
displayYoutubeVideo.getSettings().setUserAgentString("Mozilla/5.0
(Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0");
displayYoutubeVideo.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = displayYoutubeVideo.getSettings();
webSettings.setJavaScriptEnabled(true);
displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8");
}
}
I guess you might be missing android:hardwareAccelerated="true" in the permissions of your activity in the manifest file. Since you are playing a flash video this should work out to make the WebView flash enabled. This is something that I encountered and the resolution was to add this permission.
This link may help you.It seems you missed some properties for the web view.
https://inducesmile.com/android/embed-and-play-youtube-video-in-android-webview/
You must activate Hardware acceleration if your android:targetSdkVersion is lower than 11 and have to set webChromeclient to your webview
displayYoutubeVideo.setWebChromeClient(new WebChromeClient());
and in your manifest file add below line
<application android:hardwareAccelerated="true" ...>

WebView is closing when I press the button back android keyboard

I'm trying to program for android and I'm having a little problem. I made an app, but I can not make the back of the phone button, return to the previous page without completely closing the app. I'm working with WebView.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
// CODIGO DO WEB VIEW
final WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://www.idestudos.com.br");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
myWebView.setWebViewClient(new MyBrowser());
myWebView.setWebViewClient(new WebViewClient() { //CODE WEBVIEW}
Easy implemented by calling canGoBack.
if(webView.canGoBack()) {
webView.goBack();
}
And calling it from inside onBackPressed method in activity like this:
#Override public void onBackPressed() {
if(webView != null && webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
Here's a solution:
When your WebView overrides URL loading, it automatically accumulates a history of visited web pages. You can navigate backward and forward through the history with goBack() and goForward().
For example, here's how your Activity can use the device Back button to navigate backward:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
Check out the Building Web Apps in WebView Android Developers Guide for more info
Hope it helps you!

Playing youtube video using webview in an android app

I am trying to play an youtube video inside an android app by using the url of the video.I have tried using the following code:
new Thread(new Runnable() {
#Override
public void run() {
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient());
try {
myWebView.loadUrl(icobj.video_URL);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
It works, but the problem is that there is a black screen shown for about 4 seconds before the page opens.How do i get rid of the black screen before the page opens?
Check this link. You can listen the webview and show a placeholder or progress bar until loaded. It is all up to you.

How to setup Android WebView to make it work identically Android browser?

What are the settings to enable or disable in WebView to do this?
If you want to use a webview with exactly the same features as the native android browser, you have to check MANY options and settings. A WebView is made to NOT use all of the browsers options and settings for better performance and for having more controll on what the users can and can not do while browsing. to figure out all the opportunities you should read through the api documentation here:
http://developer.android.com/reference/android/webkit/WebView.html
For some further dealing and handling with the WebView class also here are some usefull sample codelines:
http://pankajchunchun.wordpress.com/2013/01/09/example-of-webview-with-result-handler-and-connection-timeout-handler/
I hope this will help you.
this is the webview example source..
what kind of setting do you wanna know??
public class MainActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
webview.setWebViewClient(new WebClient());
WebSettings set = webview.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
webview.loadUrl("http://www.google.com");
findViewById(R.id.btnStart).setOnClickListener(onclick);
}
OnClickListener onclick =new OnClickListener() {
#Override
public void onClick(View v) {
String url= null;
EditText add = (EditText)findViewById(R.id.add);
url = add.getText().toString();
webview.loadUrl(url);
}
};
class WebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
In addition to #Ssam's answer, you might want to manage the back button press so your app/Activity doesn't get closed on back press.
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
}else
super.onBackPressed();
}
where webview is your webview

Mobile site in webview doesn't work

Well i'm working for a simple webview to display a website of course..this website has a mobile version but when i try to run the application the webview displays the desktop mode. I tryied every way but nothing seems to work.. I tryied to change the user agent but nothing..
This is the part of user agent i'm using now
webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U;`
Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like
Gecko) Version/4.0 Mobile Safari/530.17");
Of course doesn't work.
Another one (doesn't work)
webview.getSettings().setUserAgentString("Mozilla/5.0 (Linux; Android 4.2.2; Nexus 7Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19");
And many others but none works. What can i do?
EDIT with my code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS); // Request progress circle
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
setProgressBarIndeterminateVisibility(true); // Show progress circle
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
setProgressBarIndeterminateVisibility(false); // Hide progress circle when page loaded
activity.setTitle("Title");
}
});
if (savedInstanceState == null)
{
webview.loadUrl("http://forum.ubuntu-it.org/");
}
}
Use the Following Snippet hope this will work with you
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
[...] when i try to run the application the webview displays the desktop
mode.. [...] I tryied to change the user agent but nothing..
Try to remove "webview.getSettings().setUserAgentString(.. "..
Android open in "mobile mode" by default..
[...] when i try navigate and click in some link it ask if i want open
it with a browser.
To open links clicked by the user inside your WebView, simply provide a WebViewClient for your WebView, using setWebViewClient(). For example:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
http://developer.android.com/guide/webapps/webview.html

Categories