I added scrolling activity(textscoll) to my Project.So I wanted to add huge text to that activity and several images in between that huge text.So I added string file in string.xml and called it in scrolling activity(content_textscoll.xml)...
My string file
<string name="string_text_file">This is huge text...</string>
My content_textscoll.xml
<TextView
android:layout_width="wrap_content"
android:textColor="#000000"
android:textSize="20dp"
android:lineSpacingMultiplier="1.3"
android:layout_height="wrap_content"
android:layout_margin="#dimen/text_margin"
android:text="#string/string_text_file" />
My problem is,
*I want to add several images in between above text.How to resolve that problem....?
I can add images to drawble folder.How to call them?
*How to add html links or other activity links
make web view :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="abhiandroid.com.htmlexample.MainActivity">
<WebView
android:id="#+id/simpleWebView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp" />
</RelativeLayout>
make assest folder if not exist and create new html file there with any name you want, then put your text and images there like this :
<p>in this tag (p tag) put your text </p>
<img>in this tag (img tag) put your images </img>
in your activity make something like this :
WebView webView;
public String fileName = "yourAssestFileName.html";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// init webView
webView = (WebView) findViewById(R.id.simpleWebView);
// displaying content in WebView from html file that stored in assets folder
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/" + fileName);
}
Related
I added every thing to my app but it keeps opening the page that said page not found even if i add a URL for google or any other link.
I look at most of the solutions for the same question but none of them worked (allowing internet access, allowing local access, changing the link to android asset, ......)
Any way hers my code hope you can help:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url="http://www.google.com";
WebView home = (WebView) this.findViewById(R.id.homeweb);
home.loadUrl(url);
home.setWebChromeClient(new WebChromeClient());
home.clearCache(true);
home.getSettings().setAppCacheEnabled(false);
}
}
And here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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="com.example.alex.myapplication.MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/homeweb" />
</RelativeLayout>
I know it's very simple in code but I'm still new to programming HTML in Android.
Just follow below code for showing html code in browser,
WebView view = new WebView(getActivity());
view.setVerticalScrollBarEnabled(false);
((LinearLayout)rootView.findViewById(R.id.text_about)).addView(view);
view.loadData(Html.fromHtml(put your url here), "text/html; charset=utf-8", "utf-8");
put html file in assets folder then write following code in activity
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/your_file_name.html");
remove these line from xml code
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
also remove code from activity
home.setWebChromeClient(new WebChromeClient());
home.clearCache(true);
home.getSettings().setAppCacheEnabled(false);
Programs run from top to bottom.So your problem is just the arrangement of your code.
In the onCreate method, use this code:
setContentView(R.layout.activity_main);
webView = (webView) findViewById(R.id.webView_id);
webSettings websettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//After configuring all the required settings then you load your url
webView.loadurl("https://www.google.com");
webView.setWebViewClient(new webViewClient());
I have a textview in a layout (called - t_c) with the code:-
<TextView
android:id="#+id/GoToTCContacting"
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="2"
android:background="#drawable/border2"
android:clickable="true"
android:onClick="GoToTCContacting"
android:padding="10dp"
android:text="Contacting"
android:textColor="#FFF"
android:textSize="18sp" />
I want the textview to open another layout (called - t_c_contacting) on click which has a webview in and that webview to open a html file I have in my layout folder called con.html.
This is how the t_c_contacting layout is coded:-
<WebView
android:id="#+id/contactingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
This is how my class is set up to open t_c_contacting which works fine but I can't work out how to populate the webview with my file.
public void GoToTCContacting(View view)
{
setContentView(R.layout.t_c_contacting);
}
If you already have the HTML stored localy, simply call:
webView.loadDataWithBaseURL(mUrl, mHtmlData, MIME_TYPE, CHARSET, "");
Where:
mUrl can be a dummy Url if you want, or the actual URL of the page, so the WebView can figure out how to place some UI elements such as Images.
mHtmlData is a String that contains the actual HTML content.
MIME_TYPE is a String that represents the mimeType of the data, say: "text/html"
CHARSET is a String that represents the encoding of the data, say: "utf-8"
I hope that helps.
I have an Android webView which loads a remote web page. As the code of this page is controlled by a colleague I can ask him to change it but I would prefer to find a solotion totally on client side.
For reasons regarding traffic and performance we want to store our css and javascript files locally on the client and not load them from the server.
What I came up with are two ideas but none of them worked out so far.
Store all the files in the asset folder and have the html refer to them
Problem: The webview seems to be not allowed to access "file:///..." urls
Question: Is there a way to work around this issue?
Just ignore all those references in the html and just inject all those files after loading them in the webview
Question: Just how do I add those files (.css / .js) to my already loaded html?
You can build a local WebView in the following way
The Activity (LocalWebviewActivity.java)
The Layout (activity_localwebview.xml)
The Assets folder (in the root of "assets" folder, create the folder "css" and place "style.css" in here)
You refer to JS files the same way you refer to CSS StyleSheets
LocalWebviewActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class LocalWebviewActivity extends Activity {
WebView myWebView;
StringBuilder mySBcontent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_localwebview);
myWebView = (WebView) findViewById(R.id.webkit);
mySBcontent = new StringBuilder();
mySBcontent.append("<html>");
mySBcontent.append("<head>");
mySBcontent.append("<link type='text/css' rel='stylesheet' href='css/style.css'>");
mySBcontent.append("</head>");
mySBcontent.append("<body>");
mySBcontent.append("<h1>My Heading</h1>");
mySBcontent.append("<p>My HTML content</p>");
mySBcontent.append("<p><img style='width:150px;' src='myImg.png' /></p>");
mySBcontent.append("</body>");
mySBcontent.append("</html>");
myWebView.loadDataWithBaseURL("file:///android_asset/", mySBcontent.toString(), "text/html", "UTF-8", "");
}
}
activity_localwebview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webkit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
I am using webview control for displaying a flash .swf file in my application.
when i run the app screen displaying whole white scrren.
Here it is my code,
public class flash extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
String url ="file:///android_asset/beautiful.swf";
WebView webview = (WebView) findViewById(R.id.flash_webview);
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setAllowFileAccess(true);
webview.loadUrl(url);
}
}
and the xml file contains webview control
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="#+id/flash_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
and also i added the Android:hardwareAccelerated:"true"
in the manifest file.
i am still stuck with the white screen.
Please help.
Thanks in advance.
Have you tried embeding the .swf file in an html file and load this html ?
I want to display the content of an html page and an image to the right of that in my view.
So i have defined my layout as
<ScrollView android:id="#+id/scrllvwNo1"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="fill_parent" android:background="#drawable/home_bg">
<ImageView android:id="#+id/aboutcmkimage"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:src="#drawable/about"
android:padding="5dip" />
<WebView android:id="#+id/aboutcmk" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textColor="#000000"
android:layout_toLeftOf="#+id/aboutcmkimage"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true" />
</RelativeLayout>
</ScrollView>
and
I try to load the html page as below
WebView web = (WebView)findViewById(R.id.aboutcmk);
web.loadData(getString(R.layout.about),"texl/html","utf-8");
in this case i am getting the error
"this page contains error at line1 ... "
if I try
web.loadDataWithBaseURL(null,getString(R.layout.about),"texl/html","utf-8",null);
no html output and no error
In both case the image is coming
Can anyone help me in debugging
My html has bullets , so i cannot use textview instead of webview
here is the definition of my htmlstring in xml file
<string name="About"><html><body><b>What is CMK?</b> ......</body></html>
Thanks a lot for the help
are you put this line What is CMK? ...... in strings.xml if yes then the correction is in this line
web.loadDataWithBaseURL(null,getString(R.layout.about),"texl/html","utf-8",null);
Correct one:
web.loadDataWithBaseURL(null, getString(R.string.About), "text/html", "UTF-8", null);
First of all, if you have saved your HTML specification as a string resource, your should access it as R.string.about and not R.layout.about. Change that, if it still doesn't work, try escaping the less than characters in your string, like this :
<string name="about"><html><body><b>What is CMK?</b> ......</body></html> </string>
I think you will have to escape the less than characters. Before loading the text, Log it. You'll see the problem.
Instead of storing the HTML in strings.xml, you can store it in its own file in the res/raw/ directory. For example, let's say you save the file in res/raw/mypage.html. I haven't tested this, but you should be able to open up raw resources and load them up in a WebView like:
try {
Resources resources = getResources();
InputStream inputStream = resources.openRawResource(R.raw.mypage);
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String htmlStr = new String(bytes);
webView.loadData(htmlStr, "text/html", "UTF-8");
} catch(Exception e) {
//blah
}