I have the HTML file in the assets folder and trying to load that in android webview. The css style is not getting applied also javascript isnt getting applied and images are not getting loaded.
Here is the activity code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView ctWebView = (WebView) findViewById(R.id.ctWebview);
ctWebView.getSettings().setDomStorageEnabled(true);
ctWebView.getSettings().setJavaScriptEnabled(true);
ctWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
ctWebView.setWebChromeClient(new WebChromeClient());
ctWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
Log.e("resource",url);
}
});
ctWebView.loadUrl("file:///android_asset/creditrackerhtml.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
Here is the layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/ctWebview"></WebView>
</RelativeLayout>
The HTML file is too big to paste here, but I have put the logs in public void onLoadResource(WebView view, String url) and have seen that proper resource urls are being hit at.
Is it that webview is being loaded before the resources are downloaded? Not sure how to fix this problem
Have you tried loadDataWithBaseURL()?
webView.loadDataWithBaseURL(url,
data,
"text/html",
"utf-8",
null);
Because according to official documentation,
loadDataWithBaseURL()
loads the given data into this WebView, using baseUrl as the base URL
for the content. The base URL is used both to resolve relative URLs
and when applying JavaScript's same origin policy. The historyUrl is
used for the history entry.
Put your files in assets folder.
Go to Project > app > src > main > right click > New > Folder > Assets Folder. Name it assets.
To access file use getAssets().open(filename.txt)
To make file to URL use file.toURI() or file.toURI().toURL().
Also, you do not need much to load url. Just use webView.loadUrl(url);
If you have links use :
//So links stay inside webview
webview.setWebViewClient(new WebViewClient());
webview.setOnKeyListener(new 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;
}
});
The CSS and images in the HTML should use URLs relative to the assets directory or use absolute paths on the web.
Related
I have a requirement to display webview in android application and if user click on any links on webview screen it should be open in a tabs(like same as chrome browser). So user can switch any of the tabs and view the results. Is it possible to navigate browser kind of tabs in android webview?
For Ex Im open some portal link in android webview enter link description here and I tried to open PNR status/ vacancy on that protal. For this i need to show PNR/vacancy are like a tabs not to add screens on top it.
Please suggest how to achieve this requirement
Thanks you
For displaying webview you need to define in the xml at first.
Second you need in java something like this.
The first is the XML. Second is how you need to define in java
<WebView
android:id="#+id/webView"
android:background="#color/downloadText"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
private WebView webView;
webView = findViewById(R.id.webView);
webView.loadUrl("https://www.google.com");
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
WebView newWebView = new WebView(WebViewActivity2.this);
view.addView(newWebView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
newWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse(url));
startActivity(browserIntent);
return true;
}
});
return true;
});
I am trying to change the visibility of my textview inside onCreate method, but I cant seem to get a handle for it. Or, perhaps something else is wrong. Also, the getUrl() from the WebView class is not behaving as it should. It seems that it detects the url i am looking for after i leave the page with the url i am trying to detect. After reading the documentation, it made it somewhat clear based on the definition of the method getUrl(). Here is what the documentation says: "Gets the URL for the current page. This is not always the same as the URL passed to WebViewClient.onPageStarted because although the load for that URL has begun, the current page may not have changed."
Here's what I mean:
I click on "link within the webpage that has url am trying to detect call it url5", then pressed back button, then click on "some other link within webpage call it url6" . Now, when I go back out of link with url6, the link for url5 is detected and I should be able to setVisibility of my textview as VISIBLE, which i tried but nothing happens. Well, here's my code. Please advise of anything that I might be doing wrong.
"xml format"
<RelativeLayout>
specifications for the relative layout...
<TextView>
android:visibility="invisible"
other specifications such as match_parent for both height and width
/>
<LinearLayout>
specifications of linear layout...
<LinearLayout/>
</RelativeLayout>
"inside onCreate"
relative_layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.content_main, null);
text_view = (TextView)relative_layout.findViewById(R.id.text);
WebView w = (WebView) findViewById(R.id.webview);
w.setWebViewClient(new MyWebViewClient());
//outside of onCreate inside private class MyWebViewClient
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
if(W.getUrl().equals("url I want to detect") ||
W.getUrl().equals("another url I want to detect")){
text_view.setVisibility(View.VISIBLE); //HOWEVER NOTHING SHOWS AT ALL
relative_layout.setVisibility(View.INVISIBLE);
}
return false;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && w.canGoBack()) {
if(text_view.isShown()){
w.goBack();
text_view.setVisibility(View.INVISIBLE);
relative_layout.setVisibility(View.VISIBLE);
}else w.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
How to prevent navigation to another page when click on a link or button inside a WebView page, (I do not want to disable touch listener).
Try to set new WebViewClient() into webview and catch link in shouldOverrideUrlLoading(WebView view, String url) method
Like this
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(youUrl)){
//do something
}
return true;
}
I am trying to display an external website in a fragment on a webview. Although when i open the fragment, i get a blank screen. Kindly help me with the same.
public class one extends android.support.v4.app.Fragment {
public one() {
// Required empty public constructor
}
private WebView mWebview ;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FrameLayout framelayout = (FrameLayout) inflater.inflate(R.layout.fragment_one, container, false);
mWebview = (WebView) framelayout.findViewById(R.id.web);
WebSettings setting =mWebview.getSettings();
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
//webView.loadUrl("http://www.cleankutz.appointy.com");
mWebview.loadUrl("https://www.cleankutz.appointy.com");
return framelayout;
}
}
You need the following in the manifest:
<manifest ... >
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
Here are some things you can try:
Make sure that internet is enabled in your device when testing
Enable java script:
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Also, I find this code quite useless:
mWebview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
That just overrides any link clicks. And if you want to do that, you should just not set the web view client. Also, just a bonus, if you want your webview to go back on back press, do this:
#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);
}
Please accept my answer by pressing the checkmark if this helped. If you have more questions, feel free to ask me.
i am making an android that runs a fullscreen webview but the problem is the index page appears after a 2 sec delay so i want to implement a splash screen to be displayed until the webview loads the website in the background .please if you could he me with complete code of this splash screen because i am completely new to it .
main activity is :
public class MainActivity extends Activity {
WebView website;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
website = (WebView) findViewById(R.id.website);
website.setWebViewClient(new WebViewClient());
WebSettings webSettings = website.getSettings();
webSettings.setJavaScriptEnabled(true);
website.loadUrl("http://www.fitanity.com/index2.jsp");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#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) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && website.canGoBack()) {
website.goBack();
return true;
}
else
{
finish();
}
return super.onKeyDown(keyCode, event);
}
}
you can initially show ImageView, once webview get loaded in public void onPageFinished() change their visibility.
public class MainActivity1 extends Activity {
WebView website;
ImageView imgLoading;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
website = (WebView) findViewById(R.id.website);
imgLoading = (ImageView)findViewById(R.id.imgloader);
WebSettings webSettings = website.getSettings();
webSettings.setJavaScriptEnabled(true);
website.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
imgLoading.setVisibility(View.GONE);
//show webview
website.setVisibility(View.VISIBLE);
}
});
website.loadUrl("http://www.fitanity.com/index2.jsp");
}
your xml will looks like :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgloader"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/loader"
android:visibility="visible" />
<WebView
android:id="#+id/website"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" />
</LinearLayout>
To do this, You have to make a class which extends WebViewClient class.
in this class, There are some method you can or you have to override,
1.onPageStarted -- This will call when webview will start loading Page. (SHOW YOU SPALSH)
2.shouldOverrideUrlLoading -- This method you have to override
3.onPageFinished -- This will call when webview will stop loading Page. (HIDE YOUR SPLASH)
Now into you webview, use you own class of Webview like this,
web.setWebViewClient(new mywebclient());
Hope It Helps you,
Cheers
-Aman
Dhwanik's solution worked for me too but I wanted to show come text with backgrount and loading gif as a splash screen so I replaced image with a new layout and placed all my contents in it.
Here is the link which which could be useful for everyone who need the same like me
http://www.codewithasp.net/2016/01/show-splash-screen-while-android.html