Here is my code
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends ActionBarActivity {
private WebView view;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
String url = "http://www.carsaleindiaofficial.com/?m=1";
view = (WebView) this.findViewById(R.id.webView);
view.setWebViewClient(new WebViewClient());
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.loadUrl(url);
}
public class MyAppWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.findViewById(R.id.progressBar).setVisibility(View.GONE);
//if (progressBar.getVisibility() == View.VISIBLE)
//progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().endsWith("carsaleindiaofficial.com/?m=1")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
#Override
public void onBackPressed() {
if (view.canGoBack()) {
view.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings)
view.loadUrl(view.getUrl());
return super.onOptionsItemSelected(item);
}
}
The progress bar and webview works fine but when the page is finished loading the progress bar still remains? When I have loaded the page the progress bar visibility doesn't dissapear/change to GONE? Where is the error?
Ive tried both progressBar.setVisibility(View.INVISIBLE) and progressBar.setVisibility(View.GONE); but nothin seems to work.
Instead of using customizing webclient. you use default webclient. your problem is replace this.
view.setWebViewClient(new WebViewClient());
add this
view.setWebViewClient(new MyAppWebViewClient());
and also
progressBar.setVisibility(View.INVISIBLE);
not
view.findViewById(R.id.progressBar1).setVisibility(View.GONE);
full code
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends ActionBarActivity {
private WebView view;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maine);
progressBar = (ProgressBar) this.findViewById(R.id.progressBar1);
String url = "http://www.carsaleindiaofficial.com/?m=1";
view = (WebView) this.findViewById(R.id.webView1);
view.setWebViewClient(new MyAppWebViewClient());
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
//view.loadUrl(url);
view.loadUrl("http://m.facebook.com/");
// view.loadUrl("file:///android_asset/web.html");
}
public class MyAppWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//view.findViewById(R.id.progressBar1).setVisibility(View.GONE);
Log.i("pageFinished", "yesss");
progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed() {
if (view.canGoBack()) {
view.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.maine, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings)
view.loadUrl(view.getUrl());
return super.onOptionsItemSelected(item);
}
}
just changed WebViewClient to MyWebViewClient, progressBar to progressBar1 & ActionBarActivity to AppCompatActivity and every code seems to work :) thank again every1.. :) #Devendra & #akhil :)
my final code
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends AppCompatActivity {
private WebView view;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) this.findViewById(R.id.progressBar1);
String url = "http://www.carsaleindiaofficial.com/?m=1";
view = (WebView) this.findViewById(R.id.webView);
view.setWebViewClient(new MyWebViewClient());
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.loadUrl(url);
}
public class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("pageFinished", "yesss");
progressBar.setVisibility(View.GONE);
Log.i("progressBar", "Gone");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
Log.i("progressBar", "Visible");
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Log.i("ShldOvrideUrl", "working");
return true;
}
}
#Override
public void onBackPressed() {
if (view.canGoBack()) {
view.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings)
view.loadUrl(view.getUrl());
return super.onOptionsItemSelected(item);
}
}
After intent of webview url you have to write progressbar.visibility="View.GONE"
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
FGallery.this.progressBar.setProgress(100);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
FGallery.this.progressBar.setProgress(0);
super.onPageStarted(view, url, favicon);
}
}
This is working for me. you can set it as WebViewClient.
Related
I'm beginner in Android development
i have botoom navigation bar and fragment
the one of the fragment
is Webview
but the upload file button not working but it working on browser
how can i enable it? can help me with the code i need to add it on webview client or webchrome client
i have blank fragment
and i my project work using androidx last version and the sdk api is 19 android 4.4
i watch video on YouTube but i geet color red error i think the code didn't work with fragment
i removed it
this is my fragment webview
thank you
and this is the code
package mypacgaename.com;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
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.LinearLayout;
import android.widget.ProgressBar;
import androidx.fragment.app.Fragment;
public class chatFragment extends Fragment {
LinearLayout eLinearLayout_chat;
WebView webView_chat;
ProgressBar mProgressBar_chat;
//SwipeRefreshLayout swipeRefreshLayout_chat;
public chatFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_chat, container, false);
webView_chat = (WebView) v.findViewById(R.id.webview_chat);
eLinearLayout_chat = (LinearLayout) v.findViewById(R.id.LinearWebView_chat);
mProgressBar_chat = (ProgressBar) v.findViewById(R.id.progressBar_chat);
//swipeRefreshLayout_chat = (SwipeRefreshLayout) v.findViewById(R.id.swipeweb_chat);
webView_chat.loadUrl("https://www.url-no-need-to-show-it.com");
webView_chat.getSettings().setJavaScriptEnabled(true);
webView_chat.getSettings().setUseWideViewPort(true);
webView_chat.getSettings().setDomStorageEnabled(true);
webView_chat.getSettings().setSupportZoom(false);
webView_chat.getSettings().setAppCacheEnabled(true);
webView_chat.getSettings().setDatabaseEnabled(true);
webView_chat.getSettings().setAllowFileAccess(true);
webView_chat.getSettings().setAllowContentAccess(true);
webView_chat.getSettings().setSupportMultipleWindows(true);
webView_chat.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView_chat.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar_chat.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar_chat.setVisibility(View.GONE);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
eLinearLayout_chat.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("https://www.tidio.com/powered-by-tidio/live-chat/?platform=others&project=dc71bf5k0ncictpdhdyhujy2hzf9zn7x&device=mobile&utm_source=plugin_ref&utm_medium=widget_v4&utm_campaign=plugin_ref&utm_referrer=drriyadh-lab.xyz")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
if (url.startsWith("tel:") || url.startsWith("whatsapp:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
// all the rest open in Webview
return false;
}
});
return v;
}
}
edit: i tried this but it didnt work
Hello i have java code where progressBar Loads just for first time only.
second time when surfing inner pages it does not shows at all.
package ext.packagename.apk;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
WebView web;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
OneSignal.startInit(this).init();
web = (WebView) findViewById(R.id.activity_main_webview);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//open in webview
//web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://example1.com");
// opening in browser instead of WebView
web.setWebViewClient(new MyAppWebViewClient());
}
public class MyAppWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("exmaple1.com") || Uri.parse(url).getHost().endsWith("example2.com") || Uri.parse(url).getHost().endsWith("example3.com") ) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
} else {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Alert")
.setMessage("exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
return super.onKeyDown(keyCode, event);
}
}
Progressbar is loading very fine at first time when main page loads. after that it disappear.
Use
progressBar.setVisibility(View.VISIBLE);
in onPageStarted method
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import com.gc.materialdesign.views.ProgressBarDeterminate;
import java.util.logging.Handler;
import javax.security.auth.Destroyable;
/**
* Created by myozawoo on 4/9/15.
*/
public class HomeActivity extends ActionBarActivity{
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
wv = (WebView) findViewById(R.id.myWeb);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wv.getSettings().setDomStorageEnabled(true);
wv.setWebViewClient(new MyWebViewClient());
wv.setWebChromeClient(new MyWebChromeClient());
wv.loadUrl("http://www.mmcloudschooling.com");
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
wv.restoreState(savedInstanceState);
}
#Override
public void onBackPressed() {
if (wv.canGoBack()) {
wv.goBack();
} else {
finish();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
ProgressBarDeterminate pb = (ProgressBarDeterminate) findViewById(R.id.progress);
pb.setVisibility(ProgressBarDeterminate.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
ProgressBarDeterminate pb = (ProgressBarDeterminate) findViewById(R.id.progress);
pb.setVisibility(ProgressBarDeterminate.GONE);
}
}
private class MyWebChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
ProgressBarDeterminate pb = (ProgressBarDeterminate) findViewById(R.id.progress);
pb.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#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) {
wv.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
}
When I made screen orientation change, my web view reach to home page automatically. I wanna make my web view activity to prevent restart activity when I made screen orientation change. Please, tell me how to do. Sorry for my poor English.
Use savedInstanceState. Load the url only when it is null.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
wv = (WebView) findViewById(R.id.myWeb);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wv.getSettings().setDomStorageEnabled(true);
wv.setWebViewClient(new MyWebViewClient());
wv.setWebChromeClient(new MyWebChromeClient());
if (savedInstanceState == null)
{
wv.loadUrl("http://www.mmcloudschooling.com");
} else {
wv.restoreState(savedInstanceState);
}
}
#Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
wv.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
wv.restoreState(savedInstanceState);
}
I am using fragments and have added a item on the action bar to refresh webview named airport_menuRefresh but when ever i click it nothing happens. I have 3 tabs. I have tried different methods but neither of them works.
Here is the code of my 1tab(tab1 only contains the not working logic to refresh).
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
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;
private WebView myWebView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String url = "http://google.com";
int delay = 6 * 1000;
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
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);
Handler handler = new Handler();
MyRunnable runnable = new MyRunnable(url, myWebView);
handler.postDelayed(runnable, delay);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView View, String url) {
GamesFragment.this.progress.setProgress(0);
View.loadUrl(url);
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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.airport_menuRefresh:
GamesFragment.this.myWebView.loadUrl("http://google.com");
return true;
}
return super.onOptionsItemSelected(item);
}
public class MyRunnable implements Runnable {
String url;
private WebView mywebview;
public MyRunnable(String url, WebView wv) {
this.url = url;
this.mywebview = wv;
}
public void run() {
mywebview.loadUrl(url);
}
}
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);
}
}
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.MenuInflater;
import android.view.MenuItem;
import com.tech.example.adaptor.SlidingTabLayout;
import com.tech.example.adaptor.TabsPagerAdapter;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends ActionBarActivity {
SlidingTabLayout tabs;
ViewPager viewPager;
TabsPagerAdapter adapter;
Toolbar toolbar;
private Menu optionsMenu;
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);
viewPager.setOffscreenPageLimit(2);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
setRefreshActionButtonState(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.
this.optionsMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
public void setRefreshActionButtonState(final boolean refreshing) {
if (optionsMenu != null) {
final MenuItem refreshItem = optionsMenu
.findItem(R.id.airport_menuRefresh);
if (refreshItem != null) {
if (refreshing) {
refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
} else {
refreshItem.setActionView(null);
}
}
}
}
}
EDIT
I edited my main activity like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.airport_menuRefresh:
GamesFragment.myWebView1.loadUrl("http://example.com");
MoviesFragment.myWebView.loadUrl("http://example2.com");
TopRatedFragment.myWebView2.loadUrl("http://example3.com");
return true;
}
return super.onOptionsItemSelected(item);
}
Now its reloading but suppose i click reload on tab1 all 3 tabs reloads! Any solution?
I guess error is in the name of the WebView of your methods. As long as you imported:
import android.view.View;
When in this methow
public boolean shouldOverrideUrlLoading(WebView View, String url) {
// ^-----> UPPERCASE
you make this call:
View.loadUrl(url);
you're trying to call static method of View and not the instance of View from method signature.
try this:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// ^---> lowercase
GamesFragment.this.progress.setProgress(0);
view.loadUrl(url);
// ^---> lowercase
return true;
}
Finally i found a solution.
Moved onCreateOptionsMenu and onOptionsItemSelected from main activity to fragment tabs like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
.............
.............
.............
setHasOptionsMenu(true);
.................
.................
}
#Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.airport_menuRefresh:
TopRatedFragment.this.myWebView2.loadUrl("http://myexample.com");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And this worked like a charm.
I have my WebViewFragment and of course i want to go back in the site im browsing but i have the error:
The method onBackPressed() is undefined for the type Fragment
Why? I don´t understand that?
What am i doing wrong?
Here´s my full webview code i´m using.
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewFragment extends Fragment {
WebView webView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Retrieving the currently selected item number
int position = getArguments().getInt("position");
String url = getArguments().getString("url");
// List of rivers
String[] menus = getResources().getStringArray(R.array.menus);
// Creating view corresponding to the fragment
View v = inflater.inflate(R.layout.fragment_layout, container, false);
// Updating the action bar title
getActivity().getActionBar().setTitle(menus[position]);
//Initializing and loading url in webview
webView = (WebView)v.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl(url);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
return v;
}
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}}
If i´m using:
public void onBackPressed() {
Fragment webview = getFragmentManager().findFragmentByTag("webview");
if (webview instanceof WebViewFragment) {
boolean goback = ((WebViewFragment)webview).canGoBack();
if (!goback)
super.onBackPressed();
}
}}
I have the errors:
The method canGoBack() is undefined for the type WebViewFragment
The method onBackPressed() is undefined for the type Fragment
Thank you dudes for your help!
Found the solution what is working for me in my fragment.
webView.setOnKeyListener(new 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 v;