I working on host a website that include videos , when I run the video and click to fullscreen player button the white color fill all screen but the video sound still playing
MainActivity.class
mWebview = (WebView) findViewById(R.id.web);
mWebview.setWebViewClient(new MyWebViewClient());
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setDomStorageEnabled(true);
mWebview.loadUrl("http://www.my-site.com/");
mWebview.setWebChromeClient(new WebChromeClient() {
public void onShowCustomView (View view, WebChromeClient.CustomViewCallback callback) {
//do stuff
}
public void onHideCustomView () {
//do stuff
}
});
MyWebViewClient.class
public class MyWebViewClient extends WebViewClient {
public boolean shuldOverrideKeyEvent(WebView view, KeyEvent event) {
return true;
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
String s = Uri.parse(url).getHost();
if((s.contains("rulanerder"))|| (s.contains("disqus"))){
view.stopLoading();
view.goBack();
return true; }
}
// reject anything other
} catch (Exception e) {
Log.d("Error", "shouldOverrideUrlLoading");
}
return false;
}
}
how can fixed that ?
Related
I have created an android application with simple webview function. In my html page which webview loads, I am using following script.
var message="!!YOU CANNOT COPY ANY TEXT OR IMAGE FROM THIS APP!";
function clickIE4()
{
if (event.button==2)
{
alert(message);
return false;
}
}
function clickNS4(e)
{
if (document.layers||document.getElementById&&!document.all)
{
if (e.which==2||e.which==3)
{
alert(message);
return false;
}
}
}
if (document.layers)
{
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById)
{
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("alert(message);return false")
//for disable select option
//document.onselectstart = new Function("alert(message);return false");
When I open this in android webview application, it does not show the alert message. It works fine when I open it in normal browser.
You need to create WebChromeClient and set to WebiView. Also override onJsAlert method.
WebChromeClient webChromeClient = new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
})
.create()
.show();
return true;
}
}
webView.setWebChromeClient(webChromeClient);
I have a webview in my android app. I want to show a banner only if the web page is viewed on browser. I created an interface and it tests fine but because the web page doesn't recognize the function the other javascript on the page doesn't load. Here is my Code:
Interface Class
import android.content.Context;
import android.webkit.JavascriptInterface;
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public Boolean getUser() {
return true;
}
}
Activity Class
public class MainActivity extends Activity {
ProgressBar progressBar;
WebView webview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
webview.addJavascriptInterface(new WebAppInterface(this),"Android");
this.webview.getSettings().setUserAgentString(
this.webview.getSettings().getUserAgentString()
+ " "
+ getString(R.string.user_agent_suffix)
);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.VISIBLE);
}
}
});
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl("http://www.getonfleek.com");
ParsePush.subscribeInBackground("test");
}
#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
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
}
HTML
</script>
<script type="text/javascript">
$(document).ready(function(){
var user = Android.getUser();
if(user == null){
//do something on web
}else{
//do something in app
}
});
</script>
Check to make sure the interface exists before trying to call it.
<script type="text/javascript">
$(document).ready(function(){
var user = null;
if(typeof Android !== 'undefined'){
user = Android.getUser();
//do something in app
}else{
//do something on web
}
});
</script>
i want to make toggle button that can play background sound service. here is my code.
public class MainActivity extends Activity {
private Switch mySwitch;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void playRain(View view) {
// Is the toggle on?
boolean on = ((Switch) view).isChecked();
if (on) {
Intent objIntent = new Intent(this, PlayRain.class);
startService(objIntent);
} else {
Intent objIntent = new Intent(this, PlayRain.class);
stopService(objIntent);
}
}
}
when i switch the toggle button to on, the sound play. but when i hit the home button and re open the app the toggle button go back to off. how to save changes to toggle button? so that the toggle button does not go back to off state by itself. sorry for my english.
You might need to save the state of the toggle button in SharedPreferences.
This might help.
i have written a sample code. I hope it helps
public class MainActivity1 extends Activity {
private Switch mySwitch;
SharedPreferences pref;
Editor edit;
final String SOUND_PREF_KEY = "background_sound";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySwitch = (Switch) findViewById(R.id.switch1);
pref = getPreferences(MODE_PRIVATE);
edit = pref.edit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mySwitch.setChecked(pref.getBoolean(SOUND_PREF_KEY, false));
}
#Override
protected void onStop() {
super.onStop();
if (mySwitch.isChecked()) {
edit.putBoolean(SOUND_PREF_KEY, true);
} else {
edit.putBoolean(SOUND_PREF_KEY, false);
}
edit.commit();
}
public void playRain(View view) {
// Is the toggle on?
boolean on = ((Switch) view).isChecked();
if (on) {
Intent objIntent = new Intent(this, PlayRain.class);
startService(objIntent);
} else {
Intent objIntent = new Intent(this, PlayRain.class);
stopService(objIntent);
}
}
}
I am trying to show a preloader before loading the complete page as webview but here is a small problem which I already made a preloader but it goes on and on and there is no end to it .
How can I tell the script to stop showing dialog when the page is loaded .
public class EzFragment extends Fragment {
public ProgressDialog pDialog;
public EzFragment(){}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new getFeed().execute();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_ez, container, false);
WebView myWebView = (WebView) rootView.findViewById(R.id.webView1);
String summary = "<html><head> <link rel=\"stylesheet\" type=\"text/css\"
href=\"http://www.example.com/demo/android_connect/assets/style/jquery.fullPage.css\" /></head></html>";
myWebView.loadData(summary, "text/html", null);
myWebView.loadUrl("http://www.example.com/demo/android_connect/get_ez_webmob.php");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
return rootView;
}
private class getFeed extends AsyncTask<Void, Void, Document> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setTitle("Connect to Server");
pDialog.setMessage("This process can take a few seconds to a few minutes, depending on your Internet Connection Speed.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Document doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return null;
}
}
Is it possible to add webview to Document doInBackground in order to load the page in background ?!
How can I tell the script to stop showing dialog when the page is
loaded .
Use onPageFinished to dismiss ProgressDialog because onPageFinished method is called when page is fully loaded in WebView :
myWebView.setWebViewClient(new WebViewClient(){
//.....
#Override
public void onPageFinished(WebView view, String url) {
Log.d("mytag","Page Loading Finished!");
super.onPageFinished(myWebView, url);
// dismiss ProgressDialog here
}
});
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
Stop the dialog in postexecute..
I have a webView in an Activity. Inside, the webView I have a button that opens a new HTML page. When this button is pressed and the new html page opens I would like the screen orientation to change to horizontal.
var searchButton = $('<button/>',
{
text:'Search!',
"class":"buttons",
id: "searchButtonID",
click: function(){
var select = document.getElementById("subCategory");
var option = select.options[select.selectedIndex];
var subCategoryId = option.id;
window.location.href = "deals.html?subCategoryId=" + subCategoryId;
Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
I recently learnt about WebAppInterfaces that allows Javascript to interact with Android. I tried adding the line:
Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
However, I get an error message saying:
Uncaught ReferenceError: ActivityInfo is not defined
I imported import android.content.pm.ActivityInfo; in the relevant class.
Not sure if what I'm doing is even possible...
Any help would be greatly appreciated!
private void rotate(float degree) {
final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(0);
rotateAnim.setFillAfter(true);
oWebView.startAnimation(rotateAnim);
}
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
rotate(90);
}
});
try this
Do something like this
boolean rotationRequired = false;
private void openWebviewDialog(String days)
{
dialog = new Dialog(MainActivity.GLOBAL_CONTEXT);
dialog.setContentView(R.layout.simple_webview);
WebView wb = (WebView) dialog.findViewById(R.id.simple_webview);
WebSettings webSettings = wb.getSettings();
wb.setWebViewClient(new MyWebViewClient());
webSettings.setJavaScriptEnabled(true);
wb.addJavascriptInterface(this, "javaScriptInterface");
String url = "http://www.yourdomain.com" // your url here
String title = "Hello";
wb.loadUrl(url);
dialog.setCancelable(true);
dialog.setTitle(title);
dialog.show();
}
class MyWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.contains("openExternal"))
{
return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
}
else
{
view.loadUrl(url); // Stay within this webview and load url
return true;
}
}
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
if(rotationRequired)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
}
}
#JavascriptInterface
public void passResult(String status)
{
rotationRequired = status.equalsIgnoreCase("true") ? true : false;
}
and don't forget to add this to your manifest:
android:configChanges = "orientation"
control the rotation as
<p>rotate...</p><script type='text/javascript' language='javascript'>javaScriptInterface.passResult('true');</script>
Please define and import packages
WebView myWebView;
WebAppInterface mWebAppInterface;
add this code to onCreate() of your activity
mWebAppInterface = new WebAppInterface(this);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.addJavascriptInterface(mWebAppInterface, "Android1");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//load your URL by myWebView.loadUrl("www.example.com");
Add this public function also
#JavascriptInterface
public void setorientation(){
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
Now you can call this public function for setting orientation from javascript button click function
Android1.setorientation();
visit also
http://developer.android.com/guide/webapps/webview.html
using javascript in android webview