In My activity I have a Webview at the top which plays the video inside it.
below the Webview I have a list of videos to select. For Android versions before Oreo when I select the new video it always plays in the Webview. But in Oreo it doesn't work, It always plays the first video Which i Selected. I have read the document of Oreo it says that
Calling clearFormData() no longer has any effect.
I am looking for some alternative but couldn't find any solution. Below is the my work which I have done so for
private void initViews() {
toolbar = findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(getString(R.string.app_name));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
recyclerView = findViewById(R.id.recycler_view);
progressBar = findViewById(R.id.progressBar3);
initWebView();
if (recyclerView != null) {
initRecyclerView(model);
}
AdView adView= findViewById(R.id.ad_view);
admobUtils.loadBannerAd(adView);
}
public void initWebView() {
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setSaveFormData(false);
webView.clearFormData();
webView.clearCache(true);
webView.clearHistory();
webView.setWebViewClient(new WebViewClient() {
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
view.clearHistory();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
RXEventBusUtils.getInstance().postEvent(new MediaPlayerEvent());
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
}
});
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setDisplayZoomControls(false);
String htmlData;
if (Constants.isFullScreen) {
htmlData = getHtmlDataLanscape(model.getVideo_url());
} else {
htmlData = getHtmlData(model.getVideo_url());
}
webView.loadData(htmlData, "text/html", null);
}
This is where I am selecting the video from list.
public void selectVideo(){
progressBar.setVisibility(View.VISIBLE);
model = new MyModel();
model = ((SelectVideoEvent) event).getModel();
if (model.getVideo_url() != null && !model.getVideo_url().isEmpty()) {
initRecyclerView(model);
initWebView();
Bundle bundleFire = new Bundle();
Application.getFireBaseInstance().logEvent("video_from_list",bundleFire);
}
}
Please can anybody tell me how to change the webview content in Android Oreo
As clearFormData() no longer has any effect in android version Oreo. The best way is to create a Webview as a new view. For that purpose you should use a frameLayout inside xml file, and inside java file, create webview dynamically and add that webview in framelayout as a view.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<FrameLayout
android:id="#+id/myWeb"
android:layout_width="match_parent"
android:layout_height="200dp"
android:alwaysDrawnWithCache="true" />
</RelativeLayout>
public class TestActivity extends AppCompatActivity {
FrameLayout frameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
frameLayout = findViewById(R.id.myWeb);
setWebView();
/*setWebViewToLoadData*/
}
public void setWebView(){
frameLayout.removeAllViews();
WebView webView = new WebView(this);
webView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
frameLayout.addView(webView);
}
}
And indside your selectVideo method call again setWebView
Related
When I make a video full screen in WebView, the toolbar is not hidden. How can I hide it? Below you can see the codes I use.I'm using fragment and the navigation drawer menu.
When I make a video full screen in WebView, the toolbar is not hidden. How can I hide it? Below you can see the codes I use.I'm using fragment and the navigation drawer menu.
Full screen
public class searchWebFragment extends Fragment {
public searchWebFragment() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.searchwb, container, false);
final ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progressBarHome);
final WebView webView = (WebView) v.findViewById(R.id.wv_home);
final EditText araTxt = (EditText)v.findViewById(R.id.araTxt);
final Button araBtn = (Button)v.findViewById(R.id.araBtn);
final TextView uyariTxt = (TextView)v.findViewById(R.id.uyariTxt);
progressBar.setVisibility(View.INVISIBLE);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int i, String s, String d1) {
webView.loadUrl("file:///android_asset/error.html");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.INVISIBLE);
}
});
webView.getSettings().setBuiltInZoomControls(true);
webView.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, url);
DownloadManager dm = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getActivity().getApplicationContext(), "Dosya Ä°ndiriliyor", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
webView.setVisibility(View.INVISIBLE);
araBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
webView.setVisibility(View.VISIBLE);
String aranacaksey = araTxt.getText().toString();
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
String url = "http://www.ifsalar.16mb.com/?s=" + aranacaksey;
webView.loadUrl(url);
araTxt.setVisibility(View.INVISIBLE);
araBtn.setVisibility(View.INVISIBLE);
uyariTxt.setVisibility(View.INVISIBLE);
}
});
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (webView.canGoBack()) {
webView.goBack();
}
return true;
}
return false;
}
});
return v;
}
}
I would recommend you to try out the follow code and remove the listeners which are related to loading the contents of your WebView.
You need to implement the method to load the website in your webview first.
private void loadWebsite() {
ConnectivityManager connectivityManager = (ConnectivityManager) getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager .getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
mWebView.loadUrl("yourURL");
} else {
mWebView.setVisibility(View.GONE);
}
}
From here, you need to implement 2 different class "Browser_Home" and "MyChrome". For testing purposes, just write it outside searchWebFragment and later on you can implement it as a separate java file. With only the Browser_Home class, the fullscreen button will be disabled. When MyChrome class is added, fullscreen button is enabled and toolbar should disappear as intended.
class Browser_home extends WebViewClient {
Browser_home() {
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
setTitle(view.getTitle());
progressBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
}
private class MyChrome extends WebChromeClient {
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
protected FrameLayout mFullscreenContainer;
private int mOriginalOrientation;
private int mOriginalSystemUiVisibility;
MyChrome() {}
public Bitmap getDefaultVideoPoster()
{
if (mCustomView == null) {
return null;
}
return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
}
public void onHideCustomView()
{
((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
setRequestedOrientation(this.mOriginalOrientation);
this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
{
if (this.mCustomView != null)
{
onHideCustomView();
return;
}
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
this.mOriginalOrientation = getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
getWindow().getDecorView().setSystemUiVisibility(3846);
}
}
And finally, apply the following functions and attributes to your webview and give it another try. Remember to apply these within your OnCreate method:
mWebView.setWebViewClient(new Browser_home());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
loadWebsite();
Let me know if this works and I can credit the corresponding person for the assistance with the code and classes.
create a folder row and copy the video on raw folder on resources and do the following steps,
On your XML file add VideoView,
<VideoView
android:id="#+id/vdoPlayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
On your .java class file do,
Uri uri = Uri.parse("android.resource://Your package name Here/" + R.raw.welcome_video);
vdoPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
vdoPlayer.setVideoURI(uri);
vdoPlayer.start();
I developed a website for my entreprisee and I work almost exclusively with PHP
So the Java language (and android studio) is a really new for me
Despite this I have to create an APK to use the website (in order to block the android home on this site)
I would like to know how to generate errors in order to test this function
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
MainActivity.java
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipe;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.awv_progressBar);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
LoadWeb();
progressBar.setMax(100);
progressBar.setProgress(1);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
}
});
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
public void onPageFinished(WebView view, String url) {
//Don't show ProgressBar
progressBar.setVisibility(View.INVISIBLE);
//Hide the SwipeRefreshLayout
swipe.setRefreshing(false);
}
});
}
public void LoadWeb() {
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("mysite.com");
swipe.setRefreshing(true);
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}
}
One way of doing this could be by adding a proxy(using app like fiddler) which returns custom error code as per your wish for all requests from your device.
In case you do not need specific error codes, just use a random system wide wifi proxy which actually does not exist. In this case, device will try sending data to this proxy but won't find it and you will get a callback in onReceivedError.
I have two ImageButton with two different url. When I click the image, it returns an white screen.I don't know what is the problem. Any edit or suggestions are welcome.
Thanks!
main activity
public class main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ImageButton amazon = (ImageButton)findViewById(R.id.imagebutton1);
ImageButton flipkart = (ImageButton)findViewById(R.id.imagebutton2);
amazon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.setData(Uri.parse("http://www.amazon.com"));
WebView webview = new WebView(main.this);
setContentView(webview);
startActivity(intent);
}
});
flipkart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Flipkart",Toast.LENGTH_LONG).show();// display the toast on you tube button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.setData(Uri.parse("http://www.flipkart.in"));
startActivity(intent);
}
});
}
my webview activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView htmlWebView = (WebView) findViewById(R.id.webView);
htmlWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSetting = htmlWebView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(true);
htmlWebView.loadUrl("");
}
class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Do you want to load Url in your app or want to open via External Browser. anaway I'm given the snipped code for the open the url in webview as given below in your application as:
WebView mynews;
ProgressBar pb;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsreadscreen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
pb= (ProgressBar) findViewById(R.id.progress_bar);
mynews= (WebView) findViewById(R.id.mynews);
mynews.getSettings().setJavaScriptEnabled(true);
mynews.getSettings().setDefaultFontSize(17);
mynews.getSettings().setDisplayZoomControls(true);
mynews.getSettings().setDomStorageEnabled(true);
mynews.getSettings().setLoadsImagesAutomatically(true);
mynews.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pb.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
pb.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pb.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
});
mynews.loadUrl("https://www.google.co.in");
this is the code I have Edited
amazon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.putExtra("url","http://www.amazon.com");
startActivity(intent);
}
});
get the value on the MainActivity.Class onCreateMethod like below
String url=getIntent().getStringExtra("url");
mynews.loadUrl(url);
I need to pull from the top to refresh my web view
I found this on Android Developer site but i don't know how to use it
xml Code
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Java code
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");
// This method performs the actual data-refresh operation.
// The method calls setRefreshing(false) when it's finished.
myUpdateOperation();
}
}
);
You gotta put your WebView inside SwipeRefreshLayout:
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipeRefreshLayout;
String currentUrl = "https://news.ycombinator.com/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
webView.loadUrl(currentUrl);
webView.setWebViewClient(new MyWebViewClient());
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.loadUrl(currentUrl);
}
});
}
public class MyWebViewClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
currentUrl = url;
super.onPageFinished(view, url);
}
}
}
swipeRefreshLayout.setRefreshing(false) stops the animation.
To be able to refresh the page with the same URL running
you gotta save your link in ISharedPreferences when page first loaded
public override void OnPageFinished(WebView view, string url)
{
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("MyURL", url);
editor.Commit();
}
when you load the URL for reffresh use the saved URL
string SaveURL = prefs.GetString("MyURL", "");
webView.loadUrl(SaveURL);
-- Other Solution is
Webview.Reload(); // This code Refreshes the current page loaded
In my app, I load a WebView where users enter username/password and I don't want the webview to close should the users accidentally click outside of the webview since the page will have to make another network call to load this webview.
I know this can be done for Dialog (like so: dialog.setCanceledOnTouchOutside(true);) but couldn't find similar mechanism for webview.
Thanks!
Here is the code:
#SuppressLint("SetJavaScriptEnabled")
public class LoginDialogFragment extends DialogFragment {
private static final String TAG = LoginDialogFragment.class.getSimpleName();
View mainView = null;
WebView webView = null;
OnLoginSucceeded loginSucceeded = null;
ProgressBar progressBar = null;
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.webview, container, false);
if (getDialog() != null)
getDialog().setTitle(getString(R.string.login));
webView = (WebView) mainView.findViewById(R.id.webview);
//display the progress bar
progressBar = (ProgressBar) mainView.findViewById(R.id.user_profile_loading_progress);
progressBar.setVisibility(View.VISIBLE);
//turn caching off
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
});
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i(TAG, "URL received in onCreateView's onPageStarted is: " + url);
new HandleRequest().execute(url);
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
new FetchLoginFormTask().execute();
return mainView;
}
I found my answer here and I just used getDialog().setCanceledOnTouchOutside(false); ensuring that when back key is clicked, the webview is dismissed.
Try with override the method:
#Override
public void setCancelable(boolean cancelable) {
super.setCancelable(false);
}
If your issue is not getting solved by this then please put some code so i can help you.
Feel free for comment.