DroidGap: how get webpage? - android

My source:
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
#Override
public boolean dispatchTouchEvent(MotionEvent e) {
super.dispatchTouchEvent(e);
// Now what page ??
return true;
}
How to get the current web page?

Create one external javascript file, write following code in it:
&ltscript>
document.getElementByTagName("body").addEventListener('touchstart',touchPagePressed);// you can use 'onclick' also
function touchPagePressed()
{
alert(touchPagePressed');
var sPath=window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
alert(sPage);
MyAndroid.performClick(sPage);
}
&lt/script>
include that external js file in every html page.
after this, write following code in your onCreate method:
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new Object()
{
public void performClick(String currentPageName)
{
// Deal with a click on the body tag
System.out.println("PageName- "+currentPageName);
}
},"MyAndroid");
}
}

Related

Whatsapp Api not getting loaded in webview android

I am able to load the website properly in the app but it has a function which redirects users to whatsapp. The api works fine on mobile browser and on PC/laptop. But in the android app it loads for a second and then says webpage unavailable. What am I missing?
Image 1 stays only for a second.
After 1 second loading time :
Main Java Code:
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.loadUrl("https://zzzz/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
}
Override this shouldOverrideUrlLoading and do this in it
Code
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
});
Your code should be like this
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.loadUrl("https://naturesexpress.in/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
});
}

err_unknown_url_scheme WebView

I have a WebView app that contain a wordpress site, built with elementor pro. I use a button that will allow the user to call or to navigate to an adress via waze. The issue is that it keeps giving me the
err_unknown_url_scheme
Few fixes that did't work.
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
myWebView.loadUrl("https://perfectskin.co.il/app/");
myWebView.setWebViewClient(new WebViewClient());
webSettings.setJavaScriptEnabled(true);
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
}else {
super.onBackPressed();
}
}
}
Try to add call permission to manifest file.
"android.permission.CALL_PHONE"

The source code to refresh WebView periodically on Android Studio 3.1.1 doesn't work

I get this answer from StackOverflow old question-answer. But when I put it on Android Studio, it comes with an error. How to solve it? Please, take a look at this screenshot and this piece of code:
public class MainActivity extends AppCompatActivity {
public WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) findViewById(R.id.webView);
WebSettings webSettings= webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://obs6.com/");
webView.setWebViewClient(new WebViewClient());
Toast.makeText(LogThirdPager.this, "Hello", Toast.LENGTH_SHORT).show();
reload();
}
public void reload() {
final Handler handler = new Handler() {
#Override
public void publish(LogRecord record) {
}
#Override
public void flush() {
}
#Override
public void close() throws SecurityException {
}
};
webView.postDelayed(new Runnable() {
Toast.makeText(LogThirdPager.this, "Hello", Toast.LENGTH_SHORT).show();
reload();
},5000);
}
}
Change LogThirdPager.this in two lines to MainActivity.this. LogThirdPager.this cannot be referenced from MainActivity.

How to open a url other than defined url in webview?

I tried my best but it doesn't work for me. I want to open url other than http://google.com in default browser. What code should add inside, I seen android documentation and added the code but it doesn't work. Any suggestion is appreciated.
public class MainActivity extends Activity {
private WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
this.webview = (WebView) findViewById(R.id.activity_main_webview);
webview.loadUrl("http://google.com");
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
}
You can try to this hope this can help you..
public class MainActivity extends AppCompatActivity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse("http://gmail.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
try this snippet and make sure you have Internet permission in manifest file
public class MainActivity extends Activity {
private WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.activity_main_webview);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
webview.loadUrl("http://google.com/");
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String myAlternativeURL = "http://yahoo.com";
if (!url.equals(myAlternativeURL)) {
view.loadUrl(myAlternativeURL);
return true;
}
}
});
});

Method Not Working Inside Javascript Interface Android

I am loading an Webview url inside my Javascript interface In Android , but they are not working;
How to To handle IT
#SuppressLint({ "SetJavaScriptEnabled", "NewApi" }) public class MainActivity extends ActionBarActivity {
WebView Browser;
String website ="http://192.168.1.4/Soft/Bigcats/Ba/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Browser=(WebView) findViewById(R.id.webView1) ;
class MyJavaScriptInterface
{
#JavascriptInterface
#SuppressWarnings("unused")
public void Danyial(String Data)
{
Browser.loadUrl("http://enjoybaba.com");
}
}
Browser.addJavascriptInterface(new MyJavaScriptInterface(), "api");
Browser.loadUrl(website);
Browser.setWebViewClient(new WebViewClient());
}
}
JavaScript runs in another thread, not UI so you may need to run your code on the UI thread for it to work.
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
Browser.loadUrl("http://enjoybaba.com");
}
});
You have Browser.setWebViewClient(new WebViewClient()); after Browser.loadUrl(website);. Try to setWebViewClient before loadUrl.
I would do this a little bit differently. Take a look.
Declare JsHelper.
public interface JsHelper {
public void loadNewUrl();
}
Javascript Interface calls JsHelper
public class JsApi {
JsHelper mJsHelper;
public JsApi(JsHelper jsHelper) {
mJsHelper= mJsHelper;
}
#JavascriptInterface
public void Danyial() {
mJsHelper.loadNewUrl();
}
}
MainActivity implements JsInterface
public class MainActivity extends ActionBarActivity implements JsHelper {
WebView Browser;
String website ="http://192.168.1.4/Soft/Bigcats/Ba/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Browser=(WebView) findViewById(R.id.webView1) ;
Browser.addJavascriptInterface(new JsApi (this), "api");
Browser.setWebViewClient(new WebViewClient());
Browser.loadUrl(website);
}
#Override
public void loadNewUrl(){
Browser.loadUrl("http://enjoybaba.com");
}
}

Categories

Resources