I thought it was a very easy problem... but I haven't solved for quite some time.
For now, pressing back button will turn off the app. I want to go back by pressing the back button.
Fragment containing the webview.
HomeFragment.java
public class HomeFragment extends Fragment {
WebView webView;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
webView = view.findViewById(R.id.home_webview);
String url = "https://galaxyhub.kr/starcitizen";
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
final ProgressDialog dialog = ProgressDialog.show(getActivity(),"", "Loading", true);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Toast.makeText(getActivity(), "URL ERROR", Toast.LENGTH_SHORT).show();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
dialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dialog.dismiss();
}
});
}
}
I heard that I should also write in the MainActivity.
I'll post it right away if needed.
package com.example.starcitizen.fragment;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.starcitizen.R;
/**
* A simple {#link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
WebView webView;
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
#Override
public void handleOnBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
//use this if you need to call the Activity
//backpress for default back action
requireActivity().onBackPressed();
}
}
};
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
webView = view.findViewById(R.id.home_webview);
String url = "https://galaxyhub.kr/starcitizen";
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
final ProgressDialog dialog = ProgressDialog.show(getActivity(),"", "Loading",
true);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Toast.makeText(getActivity(), "URL ERROR", Toast.LENGTH_SHORT).show();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
dialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dialog.dismiss();
}
});
}
}
Now this code is running!
Add the fragment to the back stack if you need to navigate back from your fragment when the WebView can't go back more.
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit();
Then in your fragment:
public class HomeFragment extends Fragment {
private WebView webView;
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
#Override
public void handleOnBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
//use this if you need to call the Activity
//backpress for default back action
requireActivity().onBackPressed();
}
}
};
requireActivity().getOnBackPressedDispatcher()
.addCallback(getViewLifecycleOwner(), callback);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
webView = view.findViewById(R.id.home_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
//callbacks omitted...
});
String url = "https://galaxyhub.kr/starcitizen";
//call this after WebViewClient setup
webView.loadUrl(url);
}
}
Remember that Activity on onBackPressed will be called before the one added in the Fragment.
More info on back press handling here: https://developer.android.com/guide/navigation/navigation-custom-back#java
For this to work use at least:
androidx.fragment:fragment:1.1.0
Related
I want to save instant of my fragment and then restore back on reopening the tab. when switching from one tab to other.
here is my fragment code:
package com.mhm.universityofwah.fragments;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.mhm.universityofwah.R;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout;
public class WebFragment extends Fragment implements WaveSwipeRefreshLayout.OnRefreshListener {
private WaveSwipeRefreshLayout swipeRefreshLayout;
private WebView mWebView;
private Activity context;
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setRetainInstance(true);
View view = inflater.inflate(R.layout.fragment_avicenna, container, false);
mWebView = (WebView) view.findViewById(R.id.webview);
swipeRefreshLayout = (WaveSwipeRefreshLayout) view.findViewById(R.id.swipeContainer);
swipeRefreshLayout.setOnRefreshListener((WaveSwipeRefreshLayout.OnRefreshListener) WebFragment.this);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
swipeRefreshLayout.setRefreshing(true);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
});
BottomNavigationView bottomNavigationView=view.findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(navListner);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.setWebViewClient(new Callback());
if(savedInstanceState==null){
loadWebsite();
}
return view;
}
private BottomNavigationView.OnNavigationItemSelectedListener navListner=
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_refresh:
mWebView.reload();
break;
case R.id.nav_back:
if(mWebView.canGoBack()){
mWebView.goBack();
}
break;
case R.id.nav_forward:
if(mWebView.canGoForward()){
mWebView.goForward();
}
break;
}
return true;
}
};
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void loadWebsite() {
ConnectivityManager cm = (ConnectivityManager) Objects.requireNonNull(getActivity()).getSystemService(Context.CONNECTIVITY_SERVICE);
assert cm != null;
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
mWebView.loadUrl("http://uow.edu.pk");
} else {
Toast.makeText(Objects.requireNonNull(getActivity()).getApplicationContext(), "Please Check Network Connection!", Toast.LENGTH_SHORT).show();
swipeRefreshLayout.setRefreshing(false);
}
}
public class Callback extends WebViewClient {
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(Objects.requireNonNull(getActivity()).getApplicationContext(), "Please Check Network Connection!", Toast.LENGTH_SHORT).show();
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".pdf")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}else if (url.contains("mailto:")) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}else if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}else {
view.loadUrl(url);
return true;
}
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
swipeRefreshLayout.setRefreshing(true);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
}
#Override
public void onRefresh() {
mWebView.reload();
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
}
}
#Override
public void onSaveInstanceState(#NotNull Bundle outState )
{
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
public boolean onKey(View v, int keyCode, KeyEvent event ) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
Objects.requireNonNull(getActivity()).finish();
}
return true;
}
}
// here your code
return false;
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
this.context = (Activity) context;
}
}
I have 4 tabs but when i switch from one tab to other and come back to first one, it always reload. i tried every thing but my problem is not solved. Please help me to get this thing done.
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setRetainInstance(true);
if(view == null){
view = inflater.inflate(R.layout.fragment_avicenna, container, false);
mWebView = (WebView) view.findViewById(R.id.webview);
swipeRefreshLayout = (WaveSwipeRefreshLayout) view.findViewById(R.id.swipeContainer);
swipeRefreshLayout.setOnRefreshListener((WaveSwipeRefreshLayout.OnRefreshListener) WebFragment.this);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
swipeRefreshLayout.setRefreshing(true);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
});
BottomNavigationView bottomNavigationView=view.findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(navListner);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.setWebViewClient(new Callback());
if(savedInstanceState==null){
loadWebsite();
}
}
return view;
}
please make view a global variable and add null check when your onCreate view recalls it recreates the view so add check that if view != null then only return already initialized view instead of creating again
so i'm trying to do 3 fragments with differents webview each. But i cant manage to put them to work.
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Page2Fragment extends Fragment {
public Page2Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_page2, container, false);
}
}
And for the webview+cache i have this code
public class MainActivity extends AppCompatActivity {
private Context mContext;
private Activity mActivity;
private RelativeLayout mRelativeLayout;
private WebView mWebView;
private String mUrl="https://www.google.pt";
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mActivity = MainActivity.this;
mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
mWebView = (WebView) findViewById(R.id.web_view);
renderWebPage(mUrl);
}
protected void renderWebPage(String urlToRender) {
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// Do something on page loading started
}
#Override
public void onPageFinished(WebView view, String url) {
// Do something when page loading finished
});
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int newProgress) {
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setAppCachePath(mContext.getCacheDir().getPath());
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
mWebView.loadUrl(urlToRender);
}
}`
I can put the code to work alone, in another app, but not with fragments.
With a simple webview it's ok too,but i really want to "cache" the website or display an error message saying that the internet is not on.
I am trying to load the site in an android app web view.
The site loads without the images ,all the images from the site are not loaded and i can't add to the cart product or even open details of other product
The code for MainActivity.java is shown below.
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ShareActionProvider;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://shopliek.com");
mWebView.setWebViewClient(new MyAppWebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.progressBar1).setVisibility(View.GONE);
//show webview
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}});
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.menu_main, menu);
/** Getting the actionprovider associated with the menu item whose id is share */
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Make your Life Easier");
intent.putExtra(Intent.EXTRA_TEXT," Visit google");
return intent;
}
}
and also this MyAppWebViewClient:
public class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("www.shopliek.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
I'm beginner so please explain kindly with simple instruction and thanks
Try this
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
Hope this will work. Let me know if it works.
I've used the below code & everything worked correctly: show pictures, details page ... ...
public class WebviewActivity extends Activity {
#BindView(R.id.webview)
WebView web;
#BindView(R.id.progressBar)
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
ButterKnife.bind(this);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl(WebserviceUrl.BASE_URL);
} catch (Exception e) {
e.printStackTrace();
}
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE);
web.loadUrl(url);
return true;
}
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
final AlertDialog.Builder builder = new AlertDialog.Builder(WebviewActivity.this);
builder.setMessage(R.string.notification_error_ssl_cert_invalid);
builder.setPositiveButton(R.string.continue_to_page, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
handler.proceed();
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
}
try it & let me know the result
Im new to android and im trying to create a web View in with fragments
and i am getting an error in the Adapter class at the WebViewFragment getitem(),
Saying" THE RETURN TYPE IS INCOMPATIBLE WITH THE fragmentPagerAdapter" please help because im stuck
import homePage.HotDeals;
import homePage.NewArrivals;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.webkit.WebViewFragment;
public class HomePageAdapter extends FragmentPagerAdapter{
public HomePageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public WebViewFragment getItem(int index) {
// TODO Auto-generated method stub
switch(index){
case 0:
return new NewArrivals();
case 1:
return new HotDeals();
}
return null;
}
Try this:
public class MainActivity extends Activity {
static public class MyWebViewFragment extends Fragment {
WebView myWebView;
final static String myBlogAddr = "http://android-er.blogspot.com";
String myUrl;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_webfragment, container, false);
myWebView = (WebView)view.findViewById(R.id.mywebview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
if(myUrl == null){
myUrl = myBlogAddr;
}
myWebView.loadUrl(myUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myUrl = url;
view.loadUrl(url);
return true;
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onBackPressed() {
MyWebViewFragment fragment =
(MyWebViewFragment)getFragmentManager().findFragmentById(R.id.myweb_fragment);
WebView webView = fragment.myWebView;
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
}
For further reference :
http://android-er.blogspot.in/2013/04/embed-webview-in-fragment.html
You can also use WebViewFragment.
http://developer.android.com/reference/android/webkit/WebViewFragment.html
Hello everyone i got a question regarding how to enable full screen support in html embed videos on my webview i have hardware accelerated true on manifest and the video works ok but wen pressed to go fullscreen the video stops.
here is my code
import com.photoshop.video.tutorials.R;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
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 WebViewFragment extends Fragment {
public final static String TAG = WebViewFragment.class.getSimpleName();
private WebView viewContentWebView;
private String url;
private boolean resetHistory = true;
#SuppressLint("SetJavaScriptEnabled")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.webview, container, false);
final ProgressBar viewContentProgress = (ProgressBar) v.findViewById(R.id.progress);
viewContentWebView = (WebView) v.findViewById(R.id.webview);
viewContentWebView.getSettings().setJavaScriptEnabled(true);
viewContentWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
viewContentWebView.loadUrl("file:///android_asset/myerrorpage.html");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
});
viewContentWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
viewContentProgress.setProgress(newProgress);
viewContentProgress.setVisibility(newProgress == 100 ? View.GONE : View.VISIBLE);
if (newProgress == 100 && resetHistory) {
viewContentWebView.clearHistory();
resetHistory = false;
}
}
});
return v;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
reload();
}
#Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden)
viewContentWebView.stopLoading();
else
reload();
}
public void setUrl(String url) {
this.url = url;
if (viewContentWebView != null)
viewContentWebView.stopLoading();
resetHistory = true;
}
public void reload() {
if (TextUtils.isEmpty(url))
return;
viewContentWebView.loadUrl(url);
}
public boolean onBackPressed() {
if (viewContentWebView.canGoBack()) {
viewContentWebView.goBack();
return true;
}
return false;
}
}
How can i achieve this
many thanks in advance and a little patience with me because i am very very VERY new to android
You should override the two following methods in your WebChromeClient:
public void onShowCustomView(View view, CustomViewCallback callback);
public void onHideCustomView();
This is an extract of the code I'm using:
private View mCustomView;
private class MyWebChromeClient extends WebChromeClient {
private int mOriginalOrientation;
private FullscreenHolder mFullscreenContainer;
private CustomViewCallback mCustomViewCollback;
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mOriginalOrientation = mActivity.getRequestedOrientation();
FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
mFullscreenContainer = new FullscreenHolder(mActivity);
mFullscreenContainer.addView(view, ViewGroup.LayoutParams.MATCH_PARENT);
decor.addView(mFullscreenContainer, ViewGroup.LayoutParams.MATCH_PARENT);
mCustomView = view;
mCustomViewCollback = callback;
mActivity.setRequestedOrientation(mOriginalOrientation);
}
#Override
public void onHideCustomView() {
if (mCustomView == null) {
return;
}
FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
decor.removeView(mFullscreenContainer);
mFullscreenContainer = null;
mCustomView = null;
mCustomViewCollback.onCustomViewHidden();
// show the content view.
mActivity.setRequestedOrientation(mOriginalOrientation);
}
static class FullscreenHolder extends FrameLayout {
public FullscreenHolder(Context ctx) {
super(ctx);
setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
}
#Override
public boolean onTouchEvent(MotionEvent evt) {
return true;
}
}