Why is not working my horizontal scrolling done programmatically? - android

I'm trying to make horizontal scroll in TextView bRes programmatically. Reading some answers and tutorials, I've included some settings.
bRes.setHorizontallyScrolling(true)
bRes.setMaxLines(1)
bRes.setMovementMethod(ScrollingMovementMethod())
bRes.setScroller(Scroller(this))
I put in the left and right side of TextView, small left and arrows black triangles with a click event to make right and left scroll if text width surpasses the view width. The buttons just appears if they has some action to do.
When I set the text in TextView, I call this code
fun changeRes(t:String){
bRes.text = t
bRes.post{ bRes.scrollTo(0,0) }
val largu = bRes.getPaint().measureText(t)
bRight.visibility = if (largu > 0.99 * bRes.width)
View.VISIBLE else View.INVISIBLE
bLeft.visibility = View.INVISIBLE
}
I've selected put the text excess in the right side, i,e, when the text is wider than TextView, the left black arrow is invisible and the right black triangle is visible.
Well, with this the text disappears completely. If I comment the line
bRes.post{ bRes.scrollTo(0,0) }
the text appears again, but there is no scroll. I don't get to see the left excess characters. The bRes.scrollX value should by 200/300 pixes but is over 1 million!
The first parameter, according documentation, is the number of pixels invisible to left of the TextView.
PS: I've used exactly the same procedure to program a vertical scroll in another TextView and it works very well. I don't use ScrollView, I've directly programmed a scrolling in my another TextView.
PS 2: I've created all my layout dinamically. No XML for layouts.
Somebody has some help for me?
I have no idea about it, except put the text in TextView manually in each change.

What kind of layout are you using? It should not be a LinearLayout, but rather a FrameLayout or RelativeLayout

I've made a workaround that is working very well for me. It's sad that I haven't found a more direct way to do this:
a) I've put 2 triangle arrows inside 2 small buttons to the left and rigth of TextView.
b) The buttons are visible or not depending if has or not scroll hidden text in its respective direction.
c) I've used the event OnTouchListener to accept click in the scroll buttons or 20% of TextView width nearest to each arrow for getting more hidden text in that direction (I've made this because the arrows are small and prone to touching in a wrong place.
d) I have to keep inside a tag (extra field of the widget) a complete text value of the widget.
e) For calculating which text I have to put in a field, I've programmed a heuristic using
widget.getPaint().measureText(string)
The visual is something like this:
PS: The ideal isn't using horizontal scroll, but if it is necessary, I think that the above appearance is nice and it is practical for users. I don't like the marquee solution where text moves alone: thinking as user, it's annoying.

Related

RelativeLayout android:clipChildren="false" does not work: Animation will get clipped?

I am trying to implement a scrolling "newsbar". There is a RelativeLayout which fills the screen horizontally. It should contain many different news titles, contained by TextView objects, which I download from the internet and they should be animated in order to slowly moving from right to left, which will generate a "scrolling newsbar" effect. I place the first TextView as RelativeLayout.ALIGN_PARENT_LEFT and all the consecutive TextViews aligned as RelativeLayout.RIGHT_OF according to the previous TextView. So, the text would enter from the right side of the screen and flow through the layout and finally exit from the left side.
I see that the RelativeLayout clips children which are outside of its bounds. I set android:clipChildren="false" for this purpose but it does not function and the TextViews continue to get clipped, which is very annoying and is currently blocking my progress.
Is this a bug or am I doing something wrong?
Please help me...

How to control the LinearLayout In the ScrollView In Android Eclipse?

Hi there I am in the middle of developing my app and am just curious to find out whether or not this is possible.
Ok so I have loads of text in my Layout and I want to pragmatically control where the linear layout in the scroll view has been pressed.
For example I want to program my app to go in the middle of my text how would I do that?
What about using the "scrollTo" function. Programatically you will be able to specify the x and y to scroll to.
http://developer.android.com/reference/android/widget/ScrollView.html#scrollTo(int, int)
Here is an example of how to use the scrollBy and scrollTo methods. Essentially you give it an offset in pixels for the amount you want to scroll. You can use getScrollX and getScrollY to get positions, though it sounds in your case you might want to use "scrollBy" to scroll an offset based on the top of the scroll view and where the user pressed.
http://android-er.blogspot.com/2011/09/scroll-view-scrollby-and-scrollto.html

Android - keeping a single element contained on screen

I'm experimenting to see if the layout scheme I want to use is possible. I want to have an XML layout that's scrollable. Within that scrollable layout, I want to have a single line going horizontally across the screen (I used just a View with a fixed height and different color). When that horizontal line reaches the top of the screen, and as I scroll down through the layout, I want it to stop and remain at the top of the screen while being able to scroll through everything below it.
I've been messing around in XML trying to get it to work, basically putting a bunch of junk before it and after it.
Any ideas as to how that might work? Would I have to do something fancy with the java code to fix the red line at the top when the scroll position reaches a certain point? Your help would be greatly appreciated.
I am assuming you want something like Gmail app where when you scroll a mail the header sticks on top... To do this, you need 2 views. You have your regular scroller below and overlay a fixed view on top (you can use a relative layout to do this). When your cell goes past a certain spot, you want to populate and set the visibility of the fixed view to VISIBLE. This would give the impression that the view you want to 'stick' to the top really just got stuck rather than scrolled out of view. You'll have to work out the opposite scrolling scenario too based on the location and height of the scrolled cell/view.
HTH

Android - Create Text Box object, like the one that microsoft word provides

This one bothers me for quite a while. I have an app where the user paints some objects with fingers. One is supposed to be a Text Box, a rectangular where the user can input text, like the one word provides (http://www.techrepublic.com/i/tr/cms/contentPics/r00319991202jod05_01.gif)
I do not need the text to be painted dynamically, I launch another activity where the user adds text and I catch the text on activity result. But I need the user to be able to reshape the object and it should handle all the cases like:
i. Box too narrow for the text -> get the last words to another line
ii. Box too narrow for some single words -> split the words
I want to handle this exactly like word text box handles it. I thought it could me by dynamically making a textView, but it seems impossible. I posted a question here Android - create TextView (or EditText) programmatically and set it in a specific place on the screen giving coordinates (mostly about textView) but I had no luck, drawTextOnPath can work for i but it is very hard to implement ii and show text in a smooth way.
I know we are not supposed to ask the same thing twice, but I do it to explain in a new way what I need, which leads to a different question. Thanks to everybody in advance.
Moving the Text around can be done easily by dragging the view in onTouchEvent.
Scaling the text can be done using two finger gesture. Just like the zoom feature in Webview. You should increase the textsize of the TextView on zoom-in and decrease on zoom-out.
Update:
Resize the TextView itself when two finger gesture is on. That will automatically re-flow the text appropriately. TextView can be resized by calling setHeight and setWidth.
Declare a LinearLayout with TextView as the only child.
Inflate this layout when needed and set the margins for the textview to position it appropriately.
When resizing just change the height and width of the textview.

How to set the size of spinner

I want to know how to set the size of spinner.When i added large values to spinner list,the spinner expands and as result it pushes the labelField to the further left.
I want to know the how to set the spinner size to be a constant one
Presumably, you presently have the Spinner set with android:layout_width="wrap_content". That will expand as the values expand. If that is not what you want, do something else. RelativeLayout is particularly useful here, as you can anchor both the left and right sides of the Spinners to things they should not expand past (e.g., left to another widget, right to the right edge of the screen).
Just in case anyone come to this page again... One may refer to here:
How to change spinner text size and text color?

Categories

Resources