0

I realize the title is confusing, but I'm not sure how to summarize my problem.

So in HTML, you can have a webpage

reallyLongWebpage.html

And when you open it in a browser it opens it at the top of the document. But if, instead, you navigate to

reallyLongWebpage.html#section46

then it will open the webpage, but scroll down to whereever <a name="section46"></a> is on the page.

I'm trying to do a similar thing with my android app.

I have an activity with a fair amount of content (not a huge amount, but still a little too much to scroll down every time to find what you want). I know how to use intents and send parameters, but on the activity itself, how do I take the value (e.g. "scroll to anchor 7") and scroll to the appropriate positon?

Entity
  • 7,972
  • 21
  • 79
  • 122
  • Does your activity contain a list view? Is it the ListView that you wish to scroll? – EJK Nov 10 '13 at 04:49
  • It does not contain a list view. It is mostly TextViews with a few images here and there. All the headings (which I want to be anchors) are separate TextViews. – Entity Nov 10 '13 at 04:55
  • Darn. I was just in the process of composing an answer for list views :-( – EJK Nov 10 '13 at 04:57
  • 1
    Is this what you are looking for: http://stackoverflow.com/questions/6831671/is-there-a-way-to-programmatically-scroll-a-scroll-view-to-a-specific-edit-text – EJK Nov 10 '13 at 05:04

2 Answers2

2

The answer really depends on what you are scrolling exactly, but if you are using ScrollView, then you can use .scrollTo(int, int) (see here).

To retrieve the scroll coordinates or search text from the Intent, it should be placed inside the data Bundle before it is sent.

If you are not using preordained coordinates and searching for some particular text, you will just need to check each TextView's contents and compare. It may be helpful to use setTag and getTag to specify which TextView's are considered searchable headers.

Dave
  • 4,282
  • 2
  • 19
  • 24
1

If it is a ListView in the Activity you wish to scroll, then

a) In the intent that launches the activity, send the index of the dataset item as an extra

int indexToSelect = 17;
intent.putExtra("IndexToSelect", indexToSelect);

b) In the activity itself, get the index from the intent

int indexToSelect = intent.getIntExtra("IndexToSelect");

c) Finally, scroll the ListView to that position:

listView.setSelectionFromTop(indexToSelect, 0);
EJK
  • 12,332
  • 3
  • 38
  • 55