0

I would like to know if it is possible to to create a button in a activity to jump down the scrollview to the corresponding information. Sort of like:

Button 1
Button 2
Button 3

paragraph 1
paragraph 2
paragraph 3

So by when selecting button 1 takes you to the start of paragraph 1 and so forth. By doing it this way the user don't have to scroll down to find relevant paragraph/info or by starting a new activity. (similar to some web pages with a lot of info on with headings ontop of the page to take you to the relevant info on the same page.)

Okay this is what i have tried sofar:

final View rootView = inflater.inflate(R.layout.fragment_snakes, container, false);

Button button = (Button) rootView.findViewById(R.id.Button1);
button.setOnClickListener(new OnClickListener() {
    public void onClick(final View v) {
        mScrollView = (ScrollView) rootView.findViewById(R.string.TextView1);
        mScrollView.smoothScrollTo(R.id.TextView1, 0);
    }
});

Then i get this error:

  11-05 09:28:43.722: E/AndroidRuntime(30741): java.lang.NullPointerException
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at     com.test.testpark.StestFragment$1.onClick(StestFragment.java:34)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at   android.view.View.performClick(View.java:4637)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at android.view.View$PerformClick.run(View.java:19422)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at android.os.Handler.handleCallback(Handler.java:733)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at android.os.Handler.dispatchMessage(Handler.java:95)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at android.os.Looper.loop(Looper.java:136)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at android.app.ActivityThread.main(ActivityThread.java:5586)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at java.lang.reflect.Method.invokeNative(Native Method)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at java.lang.reflect.Method.invoke(Method.java:515)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
  11-05 09:28:43.722: E/AndroidRuntime(30741):  at dalvik.system.NativeStart.main(Native Method)

Is there a tutorial I can follow?
Any suggestions?

EDIT

  11-05 10:41:36.770: E/AndroidRuntime(3630): java.lang.NullPointerException
  11-05 10:41:36.770: E/AndroidRuntime(3630):   at com.test.testpark.StestFragment$1.onClick(StestFragment.java:34)
  11-05 10:41:36.770: E/AndroidRuntime(3630):   at android.view.View.performClick(View.java:4637)
  11-05 10:41:36.770: E/AndroidRuntime(3630):   at android.view.View$PerformClick.run(View.java:19422)
  11-05 10:41:36.770: E/AndroidRuntime(3630):   at android.os.Handler.handleCallback(Handler.java:733)
  11-05 10:41:36.770: E/AndroidRuntime(3630):   at android.os.Handler.dispatchMessage(Handler.java:95)
  11-05 10:41:36.770: E/AndroidRuntime(3630):   at android.os.Looper.loop(Looper.java:136)
  11-05 10:41:36.770: E/AndroidRuntime(3630):   at android.app.ActivityThread.main(ActivityThread.java:5586)
Allrounder
  • 685
  • 2
  • 9
  • 20
  • 1
    u need smoothScrollToPosition concept. i dont know much about it, but here is the link. it mway help u http://stackoverflow.com/questions/14479078/smoothscrolltopositionfromtop-is-not-always-working-like-it-should/20997828#20997828 – Hirak Chhatbar Nov 04 '14 at 16:27
  • Thanks, i'll have a look at that and see if it would help. Thank you – Allrounder Nov 04 '14 at 16:32
  • @Keithk Can you also post your fragment_snakes layout ? – Gaëtan Maisse Nov 05 '14 at 08:49

2 Answers2

2

Android doc for smoothScrollTo(int, int) :

Like scrollTo(int, int), but scroll smoothly instead of immediately.

Parameters: x the position where to scroll on the X axis y the position where to scroll on the Y axis

So you should not use Textview id (R.id.TextView1) but textview position, something like this :

  final View rootView = inflater.inflate(R.layout.fragment_snakes, container, false);

  Button button = (Button) rootView.findViewById(R.id.Button1);
  TextView paragraph1 = (TextView) rootView.findViewById(R.id.TextView1);
  ScrollView scrollView = (ScrollView) rootView.findViewById(R.id.ScrollView);

  button.setOnClickListener(new OnClickListener() {
      public void onClick(final View v) {
          scrollView.smoothScrollTo(0,paragraph1.getTop());
      }
  });
Gaëtan Maisse
  • 12,208
  • 9
  • 44
  • 47
  • Thanks the only error i have now is :"getTop cannot be resolved or is not a field" – Allrounder Nov 05 '14 at 08:38
  • @Keithk my bad, I forgot parenthesis it's paragraph1.getTop() and not paragraph1.getTop – Gaëtan Maisse Nov 05 '14 at 08:39
  • Nevermind, Have resolved the issue now. The problem was that 'ScrollView scrollView = (ScrollView) rootView.findViewById(R.id.ScrollView);' Should be 'ScrollView mScrollView = (ScrollView) rootView.findViewById(R.id.ScrollView);' All working fine now, but not important but would be helpfull is that can i make it scroll a little bit slower? – Allrounder Nov 05 '14 at 08:50
  • +1 All is working fine now! Thank you so much for your help! – Allrounder Nov 05 '14 at 08:53
  • Updated my answer, I think there is not simple way to change scroll speed, if you really want to achieve this you will need to implement your own scroll method – Gaëtan Maisse Nov 05 '14 at 08:56
0
mButton1.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(final View v) {
    mScrollView.smoothScrollTo(0, mParagraph1.getTop());                
  }
}
pdegand59
  • 12,776
  • 8
  • 51
  • 58
  • thanks, but seem to get an error under mParagraph1 "Cannot invoke getTop() on the primitive type int" :-( – Allrounder Nov 04 '14 at 16:55
  • `mParagraph1` is supposed to be the TextView containing the actual text of the first paragraph. – pdegand59 Nov 04 '14 at 17:03
  • Thanks, however when i put in my text view (R.id.TextView1) still gives me the same error. How would i go about doing this? – Allrounder Nov 05 '14 at 06:53