A
A
Alexey Medvedev2015-05-21 15:16:56
Android
Alexey Medvedev, 2015-05-21 15:16:56

A few unrelated issues with WebView, html resources and ListView?

There are several problems. It is not necessary to read everything and answer everything at once, I made subheadings to the problems so that you can immediately see what we are dealing with. In general, to business. There are two activities. On the first one - a ListView, on the second WebView, which loads html files from the project's raw resources. The web element works under this simple code:

WebView webView;
    webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(true);
    String text = readRawTextFile(context, getResources().getIdentifier(resName, "raw", "mypackagename"));

        webView.loadDataWithBaseURL("file:///android_asset/", text, "html", "utf-8", null);

Issue 1: Handling the back button click in webView.
In each html resource, under the heading, there is a picture-link of a specified size, which refers to the full size of the picture itself. The code in html is as simple as a bath sheet:
<a href="file:///android_asset/pic1.jpg"><img src="file:///android_asset/pic1.jpg" height="300dp"></a>

When exiting the full-sized image, I get to my list, and not back to the html file. It was clear that the "BACK" button is perceived by the layout, and not by the webView. After reading the documentation from Google, I found this way:
@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);
    }

It seems that the simplest code should work. But when checking, it turned out that returning from the full picture, the html file is not loaded back, and I get to an empty white screen, from which I exit back to the list. Perhaps this is due to the fact that I have a lot of html files, which, depending on the conditions, loads the right one. But here I'm just guessing, and I'm at an impasse. What do you think about this?
There is also a small little whim here. What unit of measurement (or html tags) can make the picture look the same on all screens? On a smartphone it's fine, on a tablet it looks too small.
Problem 2. Old version of android and html background.
Checking project on old phone with android version 2.2, there was another problem. The ListView was being filled with the system color where there was text, overlaying my background. And also he messed up (more precisely, I messed up, for sure) in the webView. In the html file, I made the background in this way:
<body background="file:///android_res/drawable/background_html.jpg" bgproperties="fixed"></body>

I made the background image (for example) at a size of 25*25, and inserted it into the 50*50 image three times, reflecting the ends so that there would be no inconsistencies when duplicating the image.
On **android 2.2**, the background image was not duplicated, but was attached under the top of the file, and when scrolling the content, the background image went away along with the title, leaving only a white background on the back. I don't even have a guess. What can you say? Will setting the background to the webView through java code help?
Problem 3. WebView and a link from one html file to another.
The most annoying. I also have links to other files in some html files, if there is any mention (according to the Wikipedia principle). Open, for example, file2.html The link is also framed with a simple html tag:
<a href="file:///android_res/raw/file4.html">Ссылка на другой html-файл из ресурсов</a>

On my phone with version 4.2.2 - everything is loaded as it should, just as it would be loaded if I went through the list.
On a tablet with version 4.4.2 and on a friend's phone with version 5.1, when clicking on the link, it is not the html file that is loaded into the webView, but simply shows my content with my code of this very html file, that is, it opens it as if I opened html file with notepad. When you go to this file through the list - everything is fine and correct. He should take it as an html page and build it in the right order. For some reason, the script crashes when there is a repeated link. What do you think about this?
PS: Thank you in advance to everyone who will answer at least push the right thought or answer at least one question. Wash me with my inexperience, please. I will be grateful.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
one pavel, 2015-05-21
@onepavel

Issues 1 and 3 developer.android.com/reference/android/webkit/Web...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question