My background is mainly web design using JS HTML and CSS writing simple apps - nothing professional. I have recent had the urge to learn a bit about Android development. I quickly came across the below line. I understand defining the variable as a webview etc, but I don't understand why '(WebView)', in brackets, is used in the statement.
WebView browser;
browser=(WebView)findViewById(R.id.webkit);
I can only imagine its something to do with android as I haven't come across it in JS before. Any help or info on where I can read up on this would be much appreciated!
It is type casting.
WebView browser;
In the above line, you are declaring browser as an object of WebView.
So it can hold only WebView
So, in the below line, you are setting the type to WebView in order to prevent error if the findViewById(R.id.webkit); return otherwise
browser=(WebView)findViewById(R.id.webkit);
This is actually a Java feature.
You can read more about it in Oracle Java Documentation
That is what we call as type casting!
Code:
WebView browser;
browser = (WebView) findViewById(R.id.webkit);
Explanation:
You are assigning value to the instance of WebView(i.e., browser) by typecasting the View from your design(.xml file) to WebView. Because, .java file should recognize what view is the webKit in the design.
Related
I insert the code of the page in WebView, but its code is cut off.
Both elements are at the bottom
String html = readFile("index.html"); webWiev.loadData(html, "text/plane; charset=utf-8", "utf-8"); T.setText(html); //EditText T = findViewById(R.id.editTextTextMultiLine);
Both elements get information from the same html variable
The page was broken so I rendered it as code, that's how I discovered the problem. ("text/plane; )
The code contains all the necessary script style pages, a total of 3496 strips, and 96,501 characters.
What could be the problem? I did not find it on the Internet. Maybe some webView limit.
The Android application must download the web application from the server or, if there is no connection, from a file. Accordingly, the page is displayed incorrectly when loaded from a file, I specify the entire web application with styles and scripts in one file. Everything I did was aimed at identifying the problem. And when I found it, I started looking for it on the Internet, but I couldn't find it.
In my app , I am having a webview . In webview ,I am showing HTML content which is having tag , .I have 2 tabs in that which should switch on clicking on same place. But in webview they are coming one after the other.
In HTML file, I am calling method of Jquery file that method is not getting called . In browser its working fine. In iOS its working.
I am loading webview with base Url method only.
webView.loadDataWithBaseURL(pathToHtml, strLoad, "text/html","UTF-8", "");
These files are in assets . I am not sure that I am able to provide correct path for jquery files. So , I copied files to sdcard. There I am giving path. So path can not be the issue.
Please help me out . I tried many things but nothing worked. I am not getting what is the issue.
You can call a javascript function from webView.loadUrl("javascript:testFunction();"); to change the tabs insted of loading the page again. at the first load, you can use webView.loadUrl("file:///android_asset/index.html"); Befor using the jquery code, make sure that javascript enabled, set the webviewclient and webchromeclient for the webView.
When I try to reload the titanium webview true webview.reload(), the view does not reload correctly. Instead if loading the page it gives me a page not found.
what i'm doing:
In Titanium i make use of webviews to display data. These webviews make use of HTML that is stored in the local filesystem that Titanium offers. The webview is called url is set by :
webview.setUrl( Ti.Filesystem.applicationDataDirectory.toString() + 'index.html');
This sets the proper url for the webview, it let's me see the correct html page. When I use webview.reload(), it seems lost... is there a way to reload the webview, or should i remove and then add the webview again?
Setting a URL for WebView the resource is usually loaded from the Resources folder.
So try to move all HTML files there (into Resources, same folder where app.js is located) and simply use.
webview.setUrl('index.html');
This has worked for me both on iOS and Android.
(There is an issue related to Android regarding WebView and setting its content by html property but this shouldn't matter here)
For PhoneGap Application, as the instruction said, I have replaced the setContentView() line with super.loadUrl("file:///android_asset/www/index.html"); and the next line is appView.addJavascriptInterface( new JavaScriptInterface(this), "Android");
My question is
if I wanna use appView in my JSInterface, how can I find it, or pass it into JSInterface, or reference it from JSInterface.
And is appView the only webview in PhoneGap?
First you should not take the approach of adding your own JS interface. It is better if you write a PhoneGap plugin:
http://docs.phonegap.com/en/2.0.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android
We've already suffered through all the pains of making sure the JS interfaces are setup correctly. From your plugin you can get access to appView by using:
this.webView
Yes, appView is the only webview in PhoneGap.
I take the response from an HTTP connection in the form of string and show that to webview like this:
WebView engine = (WebView)findViewById(R.id.webview);
engine.loadData(endResult, "text/html", "UTF-8"); /*endresult is string*/
I actually get a response that contains the google page (google search result direct from google.com).
The loadData method works well i.e it shows the web page but when I click on one of the links on that page it shows "page not available" and said that "xyz link might be temporarily down or it may have moved to permanently to a new web address".
this happens for all links accept the first present link on that page. i.e it shows correct page from first link on that page but fails for others..
I noticed that OSes prior to 2.3 failed to follow links if setHorizontalScrollBarEnabled and setVerticalScrollBarEnabled are set to false.
try to use loadDataWithBaseURL of the WebView class
I would avoid using engine.loadData - it seems to cause all sorts of crazy problems.
Use engine.loadDataWithBaseURL instead, and pass the base URL of where the content exists. I would think that the content you are loading is using relative paths in it's HTML so it's looking inside your app resources. By specifying the base URL you get around this problem.