WebView videos on fullscreen - java

Please see below my code: I have tried several codes that fulllscreen for videos and I did not succeed, or I did not know how to install.
I need enable fullscreen for videos for any player video
package com.androidapp.www.WEBSITE;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView)findViewById(R.id.webView);
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
wv.loadUrl("https://www.mfshd.net");
wv.setWebViewClient(new MobWebViewClient());
}
#Override
public void onBackPressed() {
if(wv.canGoBack()){
wv.goBack();
}else{
super.onBackPressed();
}
}
#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);
}
private class MobWebViewClient extends WebViewClient {
}
}

Firstly you have to add a FrameLayout in your activity_main and make its visibility "gone"
Secondly add this code to your MainActivity:
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
wv.setWebChromeClient(new WebChromeClient() {
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
wv.setVisibility(View.GONE);
frameLayout.setVisibility(View.VISIBLE);
frameLayout.addView(view);
mCustomViewCallback = callback;
}
#Override
public void onHideCustomView() {
super.onHideCustomView();
if (mCustomView == null)
return;
wv.setVisibility(View.VISIBLE);
frameLayout.setVisibility(View.GONE);
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
frameLayout.removeView(mCustomView);
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
}
});

Related

Android WebView app resetting on rotation

I am having some issues with webview where on rotation the app resets to the default URL. I have hunted round for a solution and tried a number on here and run into different issues with each.
This is what is in my .java code
package uk.co.grcade.grcade;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebViewClient;
public class GRcade extends Activity {
WebView web;
String webURL = "http://grcade.co.uk";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grcade);
if (savedInstanceState != null)
{
web.loadUrl(webURL);
}
web = (WebView) findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl(webURL);
web.setPadding(0, 0, 0, 0);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setSupportZoom(true);
web.getSettings().setBuiltInZoomControls(true);
web.getSettings().setDisplayZoomControls(false);
}
#Override
protected void onSaveInstanceState (Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}
#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_grcade, menu);
return true;
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView web, String url) {
web.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int ketCode, KeyEvent event) {
if ((ketCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
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);
}
}
I got the code from a post on here but can't comment to ask why the line:
web = (WebView) findViewById(R.id.web);
is showing an unable to resolve symbol error, I am new to this whole thing so would appreciate some help on how I can get that error to go away and allow me to have a webview app that can be rotated without resetting back to the default page.
Thanks.
Your problem is in OnCreate method.
Your code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grcade);
if (savedInstanceState != null)
{
web.loadUrl(webURL);
}
.........
.........
You check if your Bundle object is not null and you load the URL. Instead, you should be restoring the state when the bundle object is not null. Something like this
if (savedInstanceState != null) {
webView.restoreState(savedInstanceState);
} else {
webView.loadUrl("http://grcade.co.uk");
}

Configuring Braintree

I am trying to accept BrainTree on my app. I don't understand the Async HTTP Client part.
Where do I place this in my app? Currently, I have it in the MainActivity and it is giving me errors for 'get', 'TextHttpResponseHandler' as well as 'clientToken'.
Any ideas?
package com.example.android.payments;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.loopj.android.http.*;
import com.braintreepayments.api.dropin.BraintreePaymentActivity;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://your-server/client_token", new TextHttpResponseHandler() {
#Override
public void onSuccess(String clientToken) {
this.clientToken = clientToken;
}
});
public void onBraintreeSubmit(View v) {
Intent intent = new Intent(context, BraintreePaymentActivity.class);
intent.putExtra(BraintreePaymentActivity.EXTRA_CLIENT_TOKEN, clientToken);
// REQUEST_CODE is arbitrary and is only used within this activity.
startActivityForResult(intent, REQUEST_CODE);
}
#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);
}
}
Your call to client.get() is not in a method body, so your code is not syntactically valid. Try putting it in a method (or constructor or initializer block - but it's a bad idea to do work in either of those places).
For example, you could put that statement in a new method called fetchClientToken():
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) { ... }
AsyncHttpClient client = new AsyncHttpClient();
public void fetchClientToken() {
client.get("https://your-server/client_token", new TextHttpResponseHandler() {
#Override
public void onSuccess(String clientToken) {
this.clientToken = clientToken;
}
});
}
public void onBraintreeSubmit(View v) { ... }
#Override public boolean onCreateOptionsMenu(Menu menu) { ... }
#Override public boolean onOptionsItemSelected(MenuItem item) { ... }
}
You then need to invoke fetchClientToken somehow, just like you would with any other method.

How to prevent reload of Webview page

I have created an app in which i have 3 tabs to display webpages using fragment. Every time i switch to tab 2 from tab 1 and then back to tab 1, tab 1 reloads. I have searched google and solutions which i found didn't work. So far i have tried this:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mWebView.restoreState(savedInstanceState);
}
And this:
<activity ...
android:launchMode="singleInstance"
android:alwaysRetainTaskState="true">
Here is my Main Activity
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.tech.slidechat.adaptor.SlidingTabLayout;
import com.tech.slidechat.adaptor.TabsPagerAdapter;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends ActionBarActivity {
SlidingTabLayout tabs;
ViewPager viewPager;
TabsPagerAdapter adapter;
Toolbar toolbar;
CharSequence Titles[]={ "tab1", "tab2", "tab3" };
int Numboftabs =3;
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
adapter = new TabsPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
// Assigning ViewPager View and setting the adapter
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
#Override
public int getDividerColor(int i) {
return 0;
}
});
tabs.setViewPager(viewPager);
}
#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);
}
}
And this is one of my Webview Fragments:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class GamesFragment extends Fragment {
private ProgressBar progress;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
String url = "http://myurl.com";
WebView myWebView = (WebView) rootView.findViewById(R.id.webViewGames);
myWebView.setWebChromeClient(new myWebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
progress = (ProgressBar) rootView.findViewById(R.id.progressBar);
progress.setMax(100);
myWebView.loadUrl(url);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView View, String url) {
View.loadUrl(url);
GamesFragment.this.progress.setProgress(0);
return true;
}
});
myWebView.setOnKeyListener(new android.view.View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
return rootView;
}
private class myWebViewClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
GamesFragment.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
}
One interesting thing is that whenever i use Tab1 and Tab2 non of them reloads but when i switch to Tab3 and then goes back to Tab1 then only it reloads.
Any Help would deeply appreciated.
Can you tried adding:
viewPager.setOffscreenPageLimit(2);
According to documentation, setOffscreenPageLimit is to:
Set the number of pages that should be retained to either side of the
current page in the view hierarchy in an idle state. Pages beyond this
limit will be recreated from the adapter when needed.
...
You should keep this limit low, especially if your pages have complex >layouts. This setting defaults to 1.
The default is 1 so when you go to tab 3 there are two tabs the the left of the current tab so tab 1 gets recreated.

Android Studio, Call to different class in different file

I have written the piece of code below to reference to the class called Forecast Fragment, and the app runs. but on line 33, i am getting a error for "cannot resolve symbol 'android' "
package com.example.sunshinepre_beta;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
int commit = getSupportFragmentManager().beginTransaction()
.add(R.id.container, new com.example.android.sunshinepre_beta.app.ForecastFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Replace new com.example.android.sunshinepre_beta.app.ForecastFragment() with new ForecastFragment()
Also move ForecastFragment.java to the same folder where MainActivity is.

java - need help for keycode_home [duplicate]

This question already has answers here:
Keycode_home doesn't get called ANDROID
(4 answers)
Closed 8 years ago.
i need help in my app which shows a toast when home button is pressed, back button is pressed and it check if the phone have got the nav bar
MainActivity.java
package com.example.myapp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Toast.makeText(getApplicationContext(), "goodbye! (home button pressed)", Toast.LENGTH_LONG).show(); //doesn't work here
finish();
return true;
}
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Toast.makeText(getApplicationContext(), "goodbye! (back button pressed)", Toast.LENGTH_LONG).show();
finish();
return true;
}
return false;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
if (hasBackKey && hasHomeKey) {
// no navigation bar
Toast.makeText(getApplicationContext(), "no nav bar", Toast.LENGTH_LONG).show();
} else {
// navigation bar
Toast.makeText(getApplicationContext(), "nav bar", Toast.LENGTH_LONG).show();
}
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
I have got a nexus 5 so it has the nav bar, and I think this is the problem
how can I fix the prolem?
nexus 5, android 4.4.3
thanks
The framework never sends KEYCODE_HOME to apps.
Right now your best indicator of when the user leaves your app (as opposed to a single activity), is listening for TRIM_MEMORY_UI_HIDDEN in onTrimMemory(int).
public class MainActivity extends Activity {
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_UI_HIDDEN) {
Context ctx = getApplicationContext();
Toast.makeText(ctx, "gone", Toast.LENGTH_SHORT).show();
}
}
}
Also you should not finish() just because the user leaves, then when the user gets back the activity will always have to be recreated from scratch, without state.

Categories