My embed video is not playing in WebView - android

I want to play this video in webview in android
http://www.youtube.com/embed/hLw8RZ10PLo?showsearch=0&autoplay=1&rel=0
But searched for it and i have implemented everything what i got
here is my code
package com.example.webapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.PluginState;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
final WebSettings.PluginState ON = webSettings.getPluginState().ON;
webSettings.setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.36 (KHTML, like Gecko) Chrome/13.0.766.0 Safari/534.36");
webSettings.setPluginState(ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginsEnabled(true);
webSettings.setAllowFileAccess(true);
myWebView.loadUrl("http://www.youtube.com/embed/hLw8RZ10PLo?showsearch=0&autoplay=1&rel=0");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and this is my xml
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webview" >
</WebView>
I am playing this video in my android 2.3.6 version and in my manifest file, I have used
android:harwareAccelerated="true"
But still my video is not running.
Can Anybody help me, I am stuck in this problem from yesterday.

Use Webview, Create a string for html tags, add iframe tage and pass the height and width of the video and url. and load the string in webview
<iframe width="420" height="345"
src="http://www.youtube.com/embed/XGSy3_Czz8k">
</iframe>
Add the string in loaddata of webview
webView.loadData(strHTMLTags, "text/html", "UTF-8");

Related

Firebase authentication not working when I use my website as a web app in android studio

I have a webpage that uses Firebase auth to register/login users. I decided to convert the webpage into an app by using Android Studio's webView feature. I've been able to load the web url but when i try to log in, it doesn't work. I don't think firebase auth is working here.
I'm a web dev and not an app developer but I'm following some online tutorials for converting websites to apps using WebView in Android Studio.
Is there any thing I need to add in order to make firebase work in web apps?
Android Studio Code (MainActivity.java):
package com.example.indianrobostore;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
WebSettings webSettings;
private String webURL = "my website url";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.myWebView);
webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(webURL);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
}
XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myWebView"/>
</LinearLayout>
Any help would be appreciated. Thanks!
Okay, I went through a few articles and found I need to enable DOM Storage for this to work.
Code : webSettings.setDomStorageEnabled(true);

WebView won't scroll for certain web page

I'm trying to load Time.com on my WebView.
Loading itself works fine, but the loaded page is not scrollable.
It works well with built-in browser, chrome browser
If I use other url(ex. http://www.google.com/), it works fine.
I think the problem is in the web page(html), but I can't figure out what's the problem.
Below is the code, though there's nothing special.
MainActivity.java
package com.yooiistudios.webviewperformance;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView)findViewById(R.id.webView);
mWebView.setWebViewClient(new NewsWebViewClient());
mWebView.loadUrl("http://www.time.com/");
}
private class NewsWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#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;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
After some research, I found out that HTML5WebView works for my case.
Also I had a problem that for some video in webpage won't play on default android webview, and HTML5WebView also solved that problem.
Source code it self is a bit dirty and cluttered, it works fine.
Anyway, I leave a link here. Hope this helps someone who has same problem.
link: https://code.google.com/p/html5webview/source/browse/trunk/HTML5WebView/src/org/itri/html5webview/HTML5WebView.java

Enable Zoom Option In WebView Android

I am developing the application using PhoneGap. I cannot enable built in zoom in/out in the webview.
I used Following code in onCreate Function
WebView web = (WebView) findViewById(R.id.webview);
web.getSettings().setBuiltInZoomControls(true);
But it did not work.
And The Activity class is
activity_main.xml
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Check if you don't have a ScrollView wrapping your Webview.
It seems ScrollView gets in the way of the pinch gesture.
To fix it, just take your Webview outside the ScrollView nd then use the same line:
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
For Cordova 5
This has changed slightly for Cordova 5.1 (I think it changed with 5.0 actually).
To enable Android zooming for Cordova 5, add these lines :
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;
and these
WebView webView = (WebView) appView.getEngine().getView();
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
A full sample of your src/com/YOURPACKAGE.java file:
package com.YOURPACKAGE;
import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;
public class MainActivity extends CordovaActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
WebView webView = (WebView) appView.getEngine().getView();
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
//settings.setDefaultZoom(ZoomDensity.FAR);
}
}
Use ==>>
web.getSettings().setBuiltInZoomControls(true);
before the line ==>>
web.loadDataWithBaseURL("Your Required params");

How can I hide the URL bar from an Android browser

I want to hide the URL bar from the browser permanently, and it should not appear even if the person gets redirected from one page to another.
Here is my .java code:
package com.example.com.android.royalcastleapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.support.v4.app.NavUtils;
public class Bookinghome extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookinghome);
//Get a reference of WebView holder
WebView webview = (WebView) this.findViewById(R.id.webview);
//Get the settings
WebSettings websettings = webview.getSettings();
//Enable Javascript
websettings.setJavaScriptEnabled(true);
//Make the zoom controls visible
websettings.setBuiltInZoomControls(true);
//Load the default url.
webview.loadUrl("http//okRoom.aspx");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_bookinghome, menu);
return true;
}
}
And here is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".Bookinghome"
android:id= "#+id/webview"/>
</RelativeLayout>
If anyone could suggest any changes I could make, that would be great!
Your application starts default browser.
You need to use WebViewClient.shouldOverrideUrlLoading like described here: https://stackoverflow.com/a/2379054/2183804

Prevent Chrome from reloading page on re-orientation?

This is my first app, it is a web app, it simply loads a webView to a url where stuff happens.
It works fine in android browser(gingerbread), but in Chrome(ICS, JB) it was going back to the initial webView url when the device is rotated. In gingerbread you account for this by setting android:configChanges="keyboard|keyboardHidden|orientation" and overriding onConfigurationChanged.
After searching the web, I re-wrote my original activity as per the example here: http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/ , in which you essentially handle the re-orientation yourself.
However now my app is crashing when I rotate the device.
Testing with Android 4.1.2.
Anyone see anything off with my code? Sorry if the answer is obvious, but I've been stuck on this bug for a couple of days now. Maybe I just need another set of eyes on it. Thanks in advance!
Note: the line #SuppressWarnings("deprecation") is not in the example, it was suggested by the editor in order to compile.
The Main Activity:
package com.example.xxx.xxx;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
protected FrameLayout webViewPlaceholder;
protected WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
#SuppressWarnings("deprecation")
protected void initUI() {
webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));
if (myWebView == null){
//Create It
myWebView = new WebView(this);
myWebView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setLoadsImagesAutomatically(true);
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setScrollbarFadingEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setInitialScale(1);
myWebView.loadUrl("http://www.theurl.com");
}
webViewPlaceholder.addView(myWebView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onConfigurationChanged(Configuration newConfig){
if (myWebView != null) {
webViewPlaceholder.removeView(myWebView);
}
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
initUI();
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// Save the state of the WebView
myWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of the WebView
myWebView.restoreState(savedInstanceState);
}
}
Main Activity XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout android:id="#+id/webViewPlaceholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
try adding orientation to you activity definition in manifest file as follows:
<activity android:screenOrientation="portrait" ...>
</activity>
I realized I don't even need WebChromeClient at all if I'm not using any of it's features. Found answer here: How to prevent WebView auto refresh when screen rotation
simply add "screenSize" to your configchanges in manifest.
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

Categories

Resources