I'm looking for a way to scroll a webview that a specific part of the contained text is shown.
Scrolling programmatically needs always an integer value of how far to scroll. Is there any possibility of retrieving the y-position of a specific text in that webview?
If you are the editor of the html content, you could simply use anchors inside the html :
The text I want to scroll to
and then simply use webview.loadurl("mywebpage.html#firstpart")
WebView has the findAll(String) and findNext() methods. They're not well documented though, and they seem to have some issues pre-froyo.
This thread in particular may help you give it a try, though. These find methods call native code, so it's not something you could easily override, I don't think. There might be something you could do with Javascript, but that's certainly not something I'm familiar with.
Related
I am developing an Android App for a community forum and am struggling with how to display the conversations. One page consists of 30 posts, to which I am bound per API. The posts contain text with the usual formatting (strong, underline, etc.), but may also contain tables, blockquotes and some other blocklike elements. Most elements can be nested, e.g., the <table> block may be surrounded by <strong> tags. Each post consists of a header with a static layout and non-formatted text.
The approach I used so far is rendering the whole conversation as HTML in a WebView. This is very easy, since I can use CSS and Javascript to customize it and it is straightforward to implement. However, this method has some drawbacks:
WebViews are very heavy. Especially when animated gifs or large images are contained, scrolling and interaction becomes less smooth even on fast phones.
Interaction with the App is somewhat limited; It is much more difficult to scroll to a specific position when the View is rendered; mostly, because this must be handled via Javascript and one never knows about render times etc.
Buttons, Long touches, link clicks etc. must all be handled via Javascript, which, especially for the different touch events, is kind of cumbersome.
Now I was thinking of different ways to handle this. Options are the following:
Build a dynamical View hirarchy. All the inline tags could be Spans in TextViews, the rest would be some specific layouts. Disadvantages: I would have a large number of views on the screen, it is hard to generate the hierarchy properly and it is hard to, for example, have some block views be surrounded with <strong> tags.
A hybrid view, as the Gmail uses it. This also would need a lot of Javascript do calculate distances etc. and it is less flexible when the layout changes during display.
A ListView of 30 WebViews. WebViews are very heavy, so this may be unwise as well.
What is the best way to solve this? How are other, similar Apps solving this problem? Is there any way to make the Javascript stuff in WebViews more reliable and stable?
I think you hit the nail on the head with building the view hierarchy. I would probably create a custom list item layout for the rows, although a multiline text view would probably do the trick. Then to process the HTML you could use the Html Spanned builder. You would need to create a Html.TagHandler to deal with any html tags that the builder doesn't support though.
I want to show an internal log inside an activity. This log consists of text strings that inform the user about the status of an operation.
For example:
Process started
Doing task A
Doing task B
...
Process stopped
The only way that comes to my mind is to use a ScrollView with a TextView that will be updated whenever there is a new string in the log, but this solution seems inelegant.
Is there any UI widget that serves as a container for lines of text?
EDIT: I prefer not to use a ListView, I just want to show lines of plain text in a container, something simpler
EDIT 2: I used to develop Win32 programs in Delphi. There we have a TMemo component that serves as a container for lines of text. I'm looking for something similar in Android
What's inelegant about updating a TextView? This is the most straightforward and simplest way to implement what you're looking for.
A TextView updated by a background polling event, probably by way of an Thread/AsyncTask is what I would try first. Don't confuse necessary complexity with inelegance. Sometimes the obvious first idea you have is the correct one.
Why not use a vertical linear layout instead a ScrollView. Then just add TextViews to the LinearLayout as they are received. This would be more elegant that just appending the text items.
I am trying to display 2 columns of text in a GridView in android. I create a TextView for each cell of the grid. Everything seems to work well until the TextView tries to wrap the text to a second (or more) line. When this happens, sometimes the second line of text overwrites the next cell, rather than causing the cell to expand to fit the text. All I have to do to get the text to display properly is click one of the cells in the grid. This seems to cause the grid to repaint and then everything gets displayed properly. I have tried any number of things to fix this problem but nothing seems to help.
Is there some property that I have to set on the TextView or the GridView to allow the cells to expand to fit the text? Or is this just a glitch in GridView that I have to live with?
BTW, setting android:singleLine="true" prevents the word wrap and solves the problem but it isn't an ideal solution because I really don't want the text to be truncated.
At a quick glance, I'm afraid you might have to do a work-around. I know it seems like a glitch, but it may be more ideal to continue to have your views start a consistent location instead of constantly having them shift based on content. I know this is a pain for what you're trying to do, but keep in mind that this was designed for hopefully the best solution for any application. Anyway..
One workaround I would propose might be manually updating the beginning of the views that get written over. I know it's not exactly ideal, but you can figure out where to place it based on the properties of your view. This doesn't feel like a particularly elegant solution though, so I'll try to cook something better up and/or post sample code. Good Luck!
I am sure there is a better way to do what I am doing in my apps. The current one I am trying to improve is a list of military cadences. The way I am doing it now is by loading html files in a web view.
What I would like to be able to do is have one view set up and just be able to add the text portion of what I would be displaying with the html file.
What would be the best method. I know this is probably a pretty simple thing to do with a sting or array but I am at the very beginner level and would need to be pointed in the right direction to do it.
Sounds like you are searching for ListView. You can make a List of what you like tho show to the user and stick that with the an Adapter to the ListView.
I have a web view that is loaded with an HTML that contains links.
when I switch to another activity (say to another tab in a tab
activity) and then switching back to it, the link is surrounded with
an orange rectangle. also happens in the GoogleAdView which really
makes it impossible to view.
Try this to prevent webview from drawing a focus rectangle when it is first focused
webView.getSettings().setNeedInitialFocus(false);
Try webView.setFocusableInTouchMode(false) - it worked for me. Also, read this link if you want to understand what drove me to this solution.
You should take into account though that this solution will make all text input boxes in your webpage unavailable...
Found another solution, but it requires access to the html itself. You need to set the following css property: -webkit-tap-highlight-color:rgba(0,0,0,0); This will not cause the problem with the input boxes.
It seems that the link in the WebView has the focus. Maybe you could avoid it by letting another view request the focus (anotherView.requestFocus();) in onResume() or onStart().