Android edit stored .html page - java

I have a locally stored HTML file in my assets folder that I am accessing through this:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
setContentView(webView);
webView.loadUrl("file:///android_asset/test.html");
}
I was wondering if there is any way I would be able to edit the source of the HTML file loaded, as I may need to manipulate certain elements.
ex. I have a <h3>Johny</h3> in the test.html, How can I change it by using an edittext?
I know how to use and show and get results form an edittext, but then, how to replace what is from the edittext to the <h3> tags in replacement to Johny?

Related

Get the main content from RSS Feeds

I've created an application which lists all the feed items in ListFragment. My question is how to load the full text from the link. I've tried with WebView, but it is loading the complete URL. Since RSS XML structure does not have any tags related to full/actual content, can anyone give some reference so that I can use it?
This is how I've tried to load the URL in WebView:
URL = getIntent().getStringExtra("url");
webView = (WebView) findViewById(R.id.activity_display_news_webview);
webView.loadUrl(URL);
You can use boilerpipe to extract the full text from the url

Loading html data from page source into webview

I'm trying to load a page in my webview by copying the html source (right click page, select 'View page source') and pasting it into my HTML file that I have in my android's 'assets' folder.
The problem I encounter is that when I use webview.loadUrl(myUrl); it loads the page but it's mostly black and white, I dont see any colors.. usually only the links show (the links are clickable but don't work, they just give me a Webpage not available error) and thats about it.
How would I fix this behavior?
my code looks like this
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.myWebview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false;
}
});
String myUrl = "file:///android_asset/Assets.html";
webView.loadUrl(myUrl);
}
the Assets.html file contains all the HTML code obviously.
I found the problem.
It was the encoding.
Unfortunately I am not sure what encoding to use but this is how I solved it.
When I copied the source from a different web page, and tried to run my app, an error showed up, it said that certain characters contained in the HTML source needed a different encoding to run, as soon as I selected "Change encoding" within the message that popped up, and ran the app, the website loaded fine.

display html in popup

What we do is we define a webview and make the html files being displayed in that. Is it possible to display an html file in android's default pop up on the same screen? I have stored the html file in assets/www folder.
Please let me know regarding this
It is possible to display a WebView or any other UI in a default Android Dialog. But, it is better and easier for you to make your own custom Dialog class and display the WebView in that. You can also make it an Activity and use the Dialog theme to make the Activity look and behave like a Dialog.
You are correct to put your .html file in the "assets" folder. That is what I do.
Load the WebView like this:
private final static String url = "file:///android_asset/file.html";
webview.loadUrl(baseUrl);

Java Android WebView Displaying a string

i have an application that fetch html from a websites, exactly in a webpage permitted only tu logged in user, so i have setted the login cookie and i get back the html webpage to a string, i tryied to easily take this string off from emulator, but i can't do copy-paste trought emulator-pc, does someone know how can i load a webview to reder my string and make me seeing if i am logged in and all work or not?
If it's possible i'd like to directly render the string, without saving it to an html file and opening it... is this possible?
Thanks, matteo.
Setting webview content form String:
public static void loadHtmlToWebView(WebView view, String html) {
view.loadData(Base64.encodeToString(html.getBytes(), Base64.DEFAULT) , "text/html", "base64");
}

WebView not Loading Data?

I'm trying to load the parsed html data from an rss feed using a
WebView, but the webview claims that the page:
"data:text/html;utf-8,[The html I'm trying to display]"
is not available.
I find it strange that it seems to be putting the html data into the
url, when I just want it to display it.
Here's my code right now for the webview:
Bundle data = getIntent().getExtras();
WebView webview = new WebView(this);
setContentView(webview);
webview.loadData(data.getString("DEFAULTTEXT"), "text/html", "utf-8");
Where the HTML has been passed in a string in the Bundle with the
identifier: DEFAULTTEXT. I've tested the class and the HTML is passed
fine, it just isn't displayed correctly.
It works fine on some of the webpages I've tried, but not others. I'll try to post the code of one that works and one that doesn't.
Huuu.... so I turned my computer on this morning and it worked perfectly. I still don't know what the problem was. :/
Edit: Never mind. It works on some, but not all of the pages I try to display.
Edit2: swapping it out for loadDataWithBaseURL worked like a charm.
As stated, when you have characters like '%', '\', '#' in your HTML, it needs to be escaped which loadData doesn't seem to do automatically.
loadDataWithBaseURL instead of loadData does escape and seems to fix this. Just use null for baseUrl and historyUrl. So the example code in the question is changed to:
Bundle data = getIntent().getExtras();
WebView webview = new WebView(this);
setContentView(webview);
webview.loadDataWithBaseURL(null,data.getString("DEFAULTTEXT"), "text/html", "utf-8",null);
I believe this sporadic behavior of loadData* is because of what is the content of the page you are trying to load. If it is plain simple html it has no problem. But if it has components like css or other features requiring external info, it will bail out.
My experience!
Are you sure you have the correct permissions defined in the manifest.xml?

Categories