I have basic android webview.it is a signup page.i want to close this webview after signup and start new intent.im new to this please help.this is my code.
public class signupweb extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signupweb);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://aaa.freeasphost.net/signup.aspx");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.equals("http://aaa.freeasphost.net/MainPanel.aspx")) {
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
return false;
} else {
return true;
}
}
// }
I tried this but not working.it stuck in the signup page
You need to pass context to start new activity. Try this:
private class MyWebViewClient extends WebViewClient {
private Context context;
public MyWebViewClient(Context context) {
this.context = context;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.equals("http://aaa.freeasphost.net/MainPanel.aspx")) {
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
context.startActivity(intent);
return false;
} else {
return true;
}
}
Related
I was facing problem for calling android Admob interstitial from webview. I could not get any proper solution. Finally I figured out the solution to call the interstitial for javascript. Look into the below answer.
Solution for the above problem
Import all the required packages.
public class MainActivity extends AppCompatActivity {
WebView myWevView;
public InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
myWevView =(WebView)findViewById(R.id.myWevView);
WebSettings ws= myWevView.getSettings();
ws.setJavaScriptEnabled(true);
ws.setDomStorageEnabled(true);
myWevView.getSettings().setUseWideViewPort(true);
myWevView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWevView.setWebViewClient(new WebViewClient());
myWevView.setWebChromeClient(new WebChromeClient());
myWevView.getSettings().setBuiltInZoomControls(true);
myWevView.loadUrl("file:///android_asset/index.html");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener(){
#Override
public void onAdLoaded(){
}
#Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
}
public void displayLoadedAd(){
runOnUiThread(new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
else
Toast.makeText(getApplicationContext(), "Ad not loded", Toast.LENGTH_SHORT).show();
}
});
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
#JavascriptInterface
public void showAdFromJs(){
Toast.makeText(mContext, "Loading Ad", Toast.LENGTH_SHORT).show();
displayLoadedAd();
}
}
//Controlling navigation
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWevView.canGoBack()) {
myWevView.goBack();
}
else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
calling from javascript
$("#btn1").click(function(){
Android.showAdFromJs();
});
I used this without the javascript call like this:
#Override
public void onAdLoaded(){
displayLoadedAd();
}
You would need to lower the number of interstitial ads to your pref. in your admob console.
I am trying to make a Webview for my website. But i have one problem with jquery code.
This is my DEMO site. In this demo site you can see when you click the image then detail div will open with animation.
But that detail is not open when i click the image.
Note: I am a new in Android
Here is my application code for Webview:
package ecowebtr.ecowebtr;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView webView;
private CustomWebViewClient webViewClient;
private String Url = "http://www.ecowebtr.com/";
ProgressDialog mProgressDialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading...");
webViewClient = new CustomWebViewClient();
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(webViewClient);
webView.loadUrl(Url);
}
private class CustomWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!mProgressDialog.isShowing())
{
mProgressDialog.show();
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
}
}
public void onBackPressed()
{
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
Try this code to solve:
webView = (WebView) findViewById(R.id.web);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
settings.setDatabasePath("/data/data/" + this.getPackageName() + "/databases/");
}
I'm trying to call a "displayAd()" function from my javascript. Here's a sample of my code:
MyActivity.java:
public class MyActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//Inject WebAppInterface methods into Web page by having Interface name 'Android'
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
mWebView.loadUrl("http://myWebSite.com");
}
public class WebAppInterface{
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
#JavascriptInterface
public void doSomething1(){
…
}
#JavascriptInterface
public void doSomething2(){
…
}
}
How can i use this class or another way to call my function for display the ad like i'm doing now with the others functions. It seems to be easy but like i'm a beginer, i don't know how to do it. Here's another example of how i'm calling them:
function example1(){
Android.doSomething1();
}
function example2(){
Android.doSomething2();
}
and finaly here's the example of the documentation:
import com.google.android.gms.ads.*;
public class BannerExample extends Activity {
private InterstitialAd interstitial;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(MY_AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
}
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
I used below JSTest class and JSInterface to get javascript result
But "processJsReturnValue" function didn't be called.
What is my mistake?
public class JSTest {
private String jsReturnValue =null;
private WebView webView;
public Context mContext;
private static class JSInterface {
private JSTest myContext;
public JSInterface(JSTest context) {
this.myContext = context;
}
#JavascriptInterface
public void processReturnValue(String value) {
myContext.processJsReturnValue(value);
}
}
public JSTest(Context context) {
super();
mContext = context;
webView = new WebView(context);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JSInterface(this), "androidInterface");
String jsString = "(function(){"+
"window.androidInterface.processReturnValue('abc');"+
"})();";
webView.loadUrl("javascript:"+jsString);
}
public void processJsReturnValue(String value) {
Log.e("got it", value);
jsReturnValue = value;
}
}
I solved it from #Jite's help
solution:
public JSTest(Context context) {
super();
mContext = context;
webView = new WebView(context);
// chromeClient = new CustomWebChromeClient();
//webView.setWebChromeClient(chromeClient);
webView.getSettings().setJavaScriptEnabled(true);
jsinterface = new JSInterface(this);
webView.addJavascriptInterface(jsinterface, "androidInterface");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
Log.e("", "do loading");
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.e("", "do loading finished");
String jsString = "window.androidInterface.processReturnValue('abc');";
webView.loadUrl("javascript:"+jsString);
}
});
webView.loadData("<html></html>", "text/html", "utf8");
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview1);
web.setWebChromeClient(new MyWebChromeClient());
web.addJavascriptInterface(new DemoJavaScriptInterface(), "temp_1");
web.loadUrl("file:///android_asset/temp_1.html");
}
}
final class DemoJavaScriptInterface {
private Handler mHandler = new Handler();
WebView web;
DemoJavaScriptInterface() {
}
public void clickOnAndroid() {
mHandler.post(new Runnable() {
public void run() {
web.loadUrl("javascript:init();");
}
});
}
}
final class MyWebChromeClient extends WebChromeClient {
private static final String LOG_TAG = "WebViewDemo";
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.e(LOG_TAG, message);
result.confirm();
return true;
}
}
Try to write this line
web = (WebView) findViewById(R.id.webview1);
web.setWebChromeClient(new MyWebChromeClient());
web.getSettings().setJavaScriptEnabled(true);
web.addJavascriptInterface(new DemoJavaScriptInterface(), "temp_1");
web.loadUrl("file:///android_asset/temp_1.html");
[2]: Both Elements are coming from html file ,when going to click on "Click It" nothing happens instead of displaying xml data that i want