0

My projec is a e-book reader application. There is only a VebView in screen and getting data from html file. It has only text content. I need to do get text in first line of current screen. In next step, I need to find the text in document and getting the text's position of screen. Finally, I need to scroll the found position. All of things necessary to solve that problem: (WebView Text Zoom Issue in Android)

html content:

<!DOCTYPE html>

    <head>
        <style type="text/css"> 
            p{} p.x1{} p.x2{} p.x3{} p.x4{} 
            h2.x1{} h2.x2{} h2.x3{} h2.x4{}
        </style>
    </head>

    <body>

        //paragraph-1
        <p class="x1">Title-1</p>
        <p class="x2">Title-2</p>
        <p class="x3">Title-3</p>
        <p class="x4">Title-4</p>
        <p>Text content.</p>


        //paragraph-2
        <h class="x1">Title-1</p>
        <h class="x2">Title-2</p>
        <p>Text content.</p>


        //paragraph-3
        <h class="x3">Title-1</p>
        <h class="x4">Title-2</p>
        <p class="x3">Title-3</p>
        <p>Text content.</p>


        //paragraph-4
        <h class="x2">Title-1</p>
        <p class="x3">Title-2</p>
        <p>Text content.</p>


        //...

    </body>

</html>
Community
  • 1
  • 1
ATES
  • 281
  • 1
  • 11
  • 29

2 Answers2

2

I think you are searching for this?

function findTopObject (elements) {
    var top = Number.POSITIVE_INFINITY;
    var obj = null;

    Array.from(elements).forEach(function (o) {
        var t = o.getBoundingClientRect(o).top;
        if (t > 0.0 && top > t) {
            obj = o;
            top = t;
        }
    });

    return obj;
}

for (var i = 0; i < 1000; i++) {
  var p = document.createElement("p");
  p.innerText = "Line " + i;
  document.body.appendChild(p);
}

var elements = document.querySelectorAll("p");
var obj = null;

window.onscroll = function() {
  if (obj != null) {
    obj.style.backgroundColor = "";
  }

  obj = findTopObject(elements);

  if (obj != null) {
    obj.style.backgroundColor = "red";
  }
};

function findTopObject(elements) {
  var top = Number.POSITIVE_INFINITY;
  var obj = null;
  Array.from(elements).forEach(function(o) {
    var t = o.getBoundingClientRect(o).top;
    if (t > 0.0 && top > t) {
      obj = o;
      top = t;
    }
  });
  return obj;
}
<body></body>
co3moz
  • 189
  • 7
1

There is no direct way to do what you want, First get html content by doing

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);

String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
    str.append(line);
}
in.close();
html = str.toString();

Then use sub string conccept to get what you want, have look Get Html content

Or you can use Jsoap parser to get value from specified tag in HTML

Document doc=Jsoup.connect("http://www.yahoo.com").get();
Elements elements=doc.select("img");

for(Element e:elements)
 {
  System.out.println(e.attr("src"));
 }
Community
  • 1
  • 1
Kathi
  • 1,061
  • 1
  • 16
  • 31