In the following code, main xml file activity_main.xml is not used. What should be modified if I want to use it. The main intention is to load the webview in background while showing splashscreen on the front. Is my approach correct? If I want to use AsyncTask to load webview while showing splashscreen, what should I do?
My app is loading everytime while changing the orientation. what should I do in order to fix it?
public class MainActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
webview = new WebView(MainActivity.this);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setAppCacheEnabled(false);
webview.loadUrl("http://www.nricabs.com");
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
public void onPageFinished(WebView view,String url){
super.onPageFinished(view, url);
setContentView(webview);
}
}
);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack()){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
About your webview question, im pretty sure that it already loads on a separate thread so you dont need to use AsyncTask.
About the reload on orientation change, you can restore the state via SavedInstanceState as shown here.
Or, you can disable it as shown here
About using a xml layout just call
setContentView(R.layout.activity_main);
Hope this helps.
After the page has finished loading you can open a new intent to a new activity and there you can use a different layout.
Another approach could be to have one layout for both (main and progress) and then hide/show the layouts when the page has finished loading.
Related
I'm trying to program for android and I'm having a little problem. I made an app, but I can not make the back of the phone button, return to the previous page without completely closing the app. I'm working with WebView.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
// CODIGO DO WEB VIEW
final WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://www.idestudos.com.br");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
myWebView.setWebViewClient(new MyBrowser());
myWebView.setWebViewClient(new WebViewClient() { //CODE WEBVIEW}
Easy implemented by calling canGoBack.
if(webView.canGoBack()) {
webView.goBack();
}
And calling it from inside onBackPressed method in activity like this:
#Override public void onBackPressed() {
if(webView != null && webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
Here's a solution:
When your WebView overrides URL loading, it automatically accumulates a history of visited web pages. You can navigate backward and forward through the history with goBack() and goForward().
For example, here's how your Activity can use the device Back button to navigate backward:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
Check out the Building Web Apps in WebView Android Developers Guide for more info
Hope it helps you!
so I have a working webview that is simple, but most of the work is done in the main activity. I am trying to break it out to its own little class so I can load other classes into the main activity on the fly. I am stuck on how to do this. I have tried this a few times and been debugging it but I just dont know if I am using the right findByValue(R.id.webView); thing here. it keeps giving me a
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.webkit.WebView.findViewById(int)' on a null object reference
issue. I have tried passing the reference in as a parameter and also hard coded it. I am missing something but dont know what. There is a WebView in my activity_main that is called wevView that is what I am trying to find out where i'm did mistake.
Thanks for any help with this.
So this is my webview class:
public class NewWebView {
//variables need for webview
private final String myPage = "http://www.google.com";
WebView myWebView = null;
//consturctor
public NewWebView(){}
//This method creates the WebView
public void createWebView(WebView webView)
{
//WebView code
//Find the Webview in the Activity.xml file
//myWebView = webView;
myWebView = (WebView)myWebView.findViewById(R.id.webview);
// myWebView = (android.webkit.WebView)findViewById(R.id.webview);
//Use the custom WebViewClient for internal app browsing
myWebView.setWebViewClient(new MyWebViewClient());
//now for enabling the settings
WebSettings webSettings = myWebView.getSettings();
//Enable JavaScript
webSettings.setJavaScriptEnabled(true);
//Enable andriod zoom features
webSettings.setBuiltInZoomControls(true);
//Loading the Url
myWebView.loadUrl(myPage);
}
//Methods Used for WebViewer
//custom WebViewClent needed for internal app navigatiion
private class MyWebViewClient extends WebViewClient
{
public boolean shouldOverRideUrlLoading(android.webkit.WebView view, String url)
{
if(Uri.parse(url).getHost().equals(myPage))
{
//this is Apivita remain in the APP
return false;
}
/*
//if it is not in my site redirect it to mobile browers
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
*/
return true;
}
}
//Enable the use of the system back button for navigation
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//if the back button was pushed
if((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
{
myWebView.goBack();
return true;
}
//if not
return false;
}
}
and here is the main activity
public class MainActivity extends AppCompatActivity {
WebView myWebView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create the webview
myWebView = (android.webkit.WebView)findViewById(R.id.webview);
NewWebView apWebView = new NewWebView();
apWebView.createWebView(myWebView);
}
}
It looks like you are finding your webview in the main activity and then passing it into your class. In your custom class, you're then trying to find another view, which you technically already have. So its trying to find a webview, inside of the webview. It obviously can't find this, so it's null.
In theory, you should be able to just use myWebView = webView;, however, you need to make sure that it is finding it in the first place.
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
String uid = extras.getString("uid");
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://xxx.xxx.xxx.xxx/android/login.php?uid="+uid);
}
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) {
view.loadUrl(url);
return true;
}
}
This is the code of my webview activity.
I'd like to implement a page loading function which is It will count in background when URL called.If URL doesnt load completely in 5000ms, the code below must run automatically.
I cant figure out how to figure It.
Intent i = new Intent(MainActivity.this, InternetError.class);
startActivity(i);
I've tried lot of methods but never get correct result.
Thank you.
First, please change you implementation of shouldOverrideUrlLoading so that it does not call loadUrl on the WebView, it's wrong to do that. The URL load is in progress when you get that callback, you must not start it again.
Second, to solve your issue can you post a delayed message to a Handler that will execute your error code after the desired timeout, and remove that message if WebViewClient.onPageFinished comes first?
What are the settings to enable or disable in WebView to do this?
If you want to use a webview with exactly the same features as the native android browser, you have to check MANY options and settings. A WebView is made to NOT use all of the browsers options and settings for better performance and for having more controll on what the users can and can not do while browsing. to figure out all the opportunities you should read through the api documentation here:
http://developer.android.com/reference/android/webkit/WebView.html
For some further dealing and handling with the WebView class also here are some usefull sample codelines:
http://pankajchunchun.wordpress.com/2013/01/09/example-of-webview-with-result-handler-and-connection-timeout-handler/
I hope this will help you.
this is the webview example source..
what kind of setting do you wanna know??
public class MainActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
webview.setWebViewClient(new WebClient());
WebSettings set = webview.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
webview.loadUrl("http://www.google.com");
findViewById(R.id.btnStart).setOnClickListener(onclick);
}
OnClickListener onclick =new OnClickListener() {
#Override
public void onClick(View v) {
String url= null;
EditText add = (EditText)findViewById(R.id.add);
url = add.getText().toString();
webview.loadUrl(url);
}
};
class WebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
In addition to #Ssam's answer, you might want to manage the back button press so your app/Activity doesn't get closed on back press.
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
}else
super.onBackPressed();
}
where webview is your webview
I have been trying to put a back button progress bar in a webview and keep the url loading within my app instead of using the android default web browser.
If I manage to keep to browsing within the app and keep the back button the progress bar never shows up if I manage to get the progress bar to show up the code at the bottom for shouldoverideurl come up never read and the default browser launches, I tried all the google tutorials and solution but none of them work. I am currently using google.. Can anyone help??
public class livebrad extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Adds Progrss bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.brows);
// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
// Get Web view
mWebView = (WebView) findViewById( R.id.webView ); //This is the id you gave
//to the WebView in the main.xml
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true); //Zoom Control on web (You don't need this
//if ROM supports Multi-Touch
mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
// Load URL
mWebView.loadUrl("http://www.bbc.co.uk");
// Sets the Chrome Client, and defines the onProgressChanged
// This makes the Progress bar be updated.
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
MyActivity.setTitle(R.string.app_name);
}class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
});
}//End of Method onCreate
}
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SandbarinFacebook extends Activity {
WebView mWebView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fb);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",
true);
mWebView = (WebView) findViewById(R.id.webkitWebView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd.isShowing() && pd!=null)
{
pd.dismiss();
}
}
});
mWebView.loadUrl("http://m.facebook.com/sandbarathens");
}
}
If you want show progress bar each time user clicks links, add code to show progressbar in your shouldOverrideUrlLoading() method.
You just missed this statement.
mWebView.setWebViewClient(..)