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.
Related
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.
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
I'm using Fragments and one of my Fragment I have a webview. I want to add a image button that will let user to go back.
I have 4 Fragments and their names are Fragment1,Fragment2,Fragment3,Fragment4.
Also I have 4 layouts tab1.xml tab2.xml tab3.xml tab4.xml and there is 4 webview objects in all layouts.
When I click Fragment1, it opens tab1.xml and tab1.xml's webview1 opens www.site.com and user starts surfing inside webview1.
If user visits 2 or more pages in webview1 I want this user let can go back with a button.
How can I do this?
In your decalarations:
WebView web = null;
In your onCreateView;
// Prepare WebView.
web = (WebView) v.findViewById(R.id.htmlDisplay);
// Set WebView.
final WebSettings webSettings = web.getSettings();
// Enable JavaScript.
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
In your back button (not the physical one):
web.goBack()
If you want to use your physical back button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(web.canGoBack() == true)
{
web.goBack();
}
else
{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
i have created a webview
<WebView
android:id="#+id/webview_compontent"
android:layout_marginTop = "10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="#FFFFFF"
/>
and in my class.java file
webView = (WebView) findViewById(R.id.webview_compontent);
//Need to enable scrollbars when in zoom
webView.setHorizontalScrollBarEnabled(true);
webView.setVerticalScrollBarEnabled(true);
webView.setPadding(0, 0, 0, 0);
//Page is bigger than view so fit it inside the view
webView.setInitialScale(40);
//Enable zooming
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("www.xyz.com");
webView.setWebViewClient(new MyWebViewClient());
//To disable the link clicks on the page
webView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return true;
}
});
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
My problem is that i want to diable all the link clicks inside webview but allow zooming with vertical and horizontal scrolls so that the user can zoom and read the text.If i set "webView.setOnTouchListener" to "ture" my webview does not respond to click events which i want but zoom control also stops which i want to be enabled. How can i filter zoom and link clicks event and enable/disable the view accordingly.
Please help me how to go about this.
Thanks
Thanks for your answers.But i got it going by adding
webView.setClickable(false); //To disable link clicks
and
webView.getSettings().setBuiltInZoomControls(true); //Enable zooming
I would like to make an app, that shows a part of the site.
Lets say that i have this site:
I only want to show this:
https://mail.google.com/mail/?ui=2&ik=d5146163e8&view=att&th=132496ec47863fea&attid=0.1&disp=inline&realattid=f_gsbtyog80&zw
Is there anyway to do this?
I hope some of you could help me.
Gaauwe
You could load the url to a WebView and then set the WebView's scroll position and zoom level to display only a specific part of the web page. If you don't want to change zoom level you could initially scroll the WebView to the required location and then override the WebView's onTouch method so that it will only scroll upto the required cordinates.
public class MyWebViewActivity extends Activity implements PictureListener{
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setPictureListener(this);
// disable scroll on touch
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
webView.loadUrl(url);
}
#Override
public void onNewPicture(WebView view, Picture picture) {
// TODO Auto-generated method stub
webView.scrollTo(x, y);
}
}
One of the way is to use PHP on your own server and CURL the webpage into it and strip out all the unnecessary things .
After that you only load it into your webview.
*This is one of the method, there are others method better then this. *
Another way is coldfusion cfhttp .. not sure,about this http://www.quackit.com/coldfusion/tutorial/coldfusion_http.cfm