How to make an activity scroll to a certain point? - android

Is it possible to have an activity scroll down to a certain point when a button is clicked?
I want my activity to behave just like an anchor in a web page.
I have two buttons at the top of the activity, followed by lengthy text aboout each of the buttons. When I click on the button at the top I want the page to scroll down beginning of that button's description.
Is this possible?

Assuming you are using a TextView to display your text, you use the built in scrolling function myTextView.scrollTo(x,y). Put it inside your buttons' onClicks and you should be good to go!
http://developer.android.com/reference/android/widget/TextView.html

So your outer layout is a scroll view?
You can call
View.requestFocus()
Obviously, the view has to be focusable.
The problem with scrollTo(x,y) mentioned above is most likely that you don't know x, y.

Related

Start animation if view is in focus

I have a android app and with several Textviews inside linear layyout which is inside scrollview. On a button click I can bring focus and animate any textView (like move left to right). But What I want is, On Scrolling, if a TextView has Focus, animation for this textView should start. Just like this website.
I have an idea, which is: If scroll changes, check textview.hasFocus(), if true then startAnimation. But I don't know which method to use for checking 'if page scrolled'? I don't even know if this is right approach? Please help.

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

Nested text view in android,with dynamic content

I want to display a recursive or nested text view at depth of 3 in Android.The text to be displayed is, dynamic coming from web service.
for example:
If I Click on Help(Level 1)
it'll show topics under Help
If I Click on Help_Topic_1(Level 2)
it'll show questions under Help_Topic_1
If I click on this question, say HT_Question_1(Level 3)
it'll show Answer of that question(Level 3)
how to accomplish with this? please guide me.
You should use ExpandableListView. Reference http://developer.android.com/reference/android/widget/ExpandableListView.html
You can call expandGroup and collapseGroup methods for expanding and collapsing on clicks.
the simplest way to do this is to have a nested layout structure. Your root view will contain the button to show level 1 and a child layout and initially be visible. The children's layout visibility will initially be set to "GONE". In the onclick listener for each button you change the visibility of the layout below it to view to "VISIBLE".
This of course is a very simple way of doing it. If you require to have open and close animations you'll need to use a more complex method.

Change layout when soft keyboard is shown

I would like to change the layout when an EditText is clicked and the softkeyboard is shown, so all EditText views are still visible.
I know that you can use the following two, but this is not what i'm looking for.
android:windowSoftInputMode="stateUnchanged|adjustPan">
android:windowSoftInputMode="stateUnchanged|adjustResize">
I've got 8 EditText views spread over the whole screen. When one is clicked to change a value I would like to still see all the ET views but nicely arrange and not pushed in a weird view.
Is there an easy way to do this?
I think you are going to have to do this yourself, but one technique might be ...
Create a layout with the views arranged for the keyboard shown (for instance a new RelativeLayout) that overlays your standard layout. Set it's visibility to GONE. Then when you detect the event that shows the keyboard hide the current view and show the alternate one
I suppose you could also use a ViewSwitcher
If you are using both at the same time then it doesnt work.Set the attribute android:windowSoftInputMode="adjustPan" only.That should work.

Vertical smooth scrolling of values in a text view

I have a text view where in i have to keep ages between 1-99. I also have two buttons ^ and v(i mean up and down arrows) on clicking them the values of the age should smoothly scroll to next or previous value.
I have been trying different ways but couldnt achieve smooth scrolling. Can anyone please give me any idea of how to achieve the task.
I think the easiest way is to simply put the TextView within a ScrollView, and let the arrows interact with the ScrollView, by using things like the fling method and playing around with the velocity parameter to suit your needs.
Use the animation framework.
When pressing down, start the 'down'-animation.
When pressing up, start the 'up'-animation.
Read more about animation here: http://developerlife.com/tutorials/?p=343
View animation is not much matured and hence i am noy sure if that can be used for moving the views.
Please find the description below:
Another disadvantage of the view
animation system is that it only
modified where the View was drawn, and
not the actual View itself. For
instance, if you animated a button to
move across the screen, the button
draws correctly, but the actual
location where you can click the
button does not change, so you have to
implement your own logic to handle
this.
Source
To scroll smoothly you can try using the scroller component.
Reference
What you would need to do is pass the duration of the scroll in the constructor and then use the property
setFinalY(int newY)
to increment the counter position by 1 unit (equal to the height of the item).
Please let me know if that helps!

Categories

Resources