0

I have a view and a textview in a linearlayout. That LinearLayout is present in a ScrollView. I want that scrollView to move down onclicking a button in that screen and again move up onclicking that again

2 Answers2

1

To scroll down on button click you should use this:

scrollView.post(new Runnable()
            {
                @Override
                public void run()
                {
                    //X,Y are scroll positions untill where you want scroll down
                    scrollView.scrollTo(X, Y);
                }
            });

To scroll up on button click you should use this:

scrollView.post(new Runnable()
            {
                @Override
                public void run()
                {
                    //X,Y are scroll positions untill where you want scroll up
                    int X=0,Y=0;
                    scrollView.scrollTo(X, Y);
                }
            });
Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80
  • Its not working. I have a ImageButton at the parentbottom and ScrollView in a LinearLayout below that. I want to move the layout in scrollview up on click of Image Button – Srujana Dakoju Mar 06 '15 at 06:13
1

Try this -

private ImageView list_scroll_iv;
private int focus_pos=1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list_scroll_iv = (ImageView)findViewById(R.id.list_scroll_iv);
    list_scroll_iv.setOnClickListener(this);
}



@Override
public void onClick(View v) {
    list_sv.post(new Runnable() {
        @Override
        public void run() {
            //1 for up and 2 for down
          if(focus_pos==1) {
              list_sv.fullScroll(ScrollView.FOCUS_DOWN);
              focus_pos=2;
              list_scroll_iv.setImageDrawable(getResources().getDrawable(R.drawable.blue_arrow_ic_up));
          }
          else {
              list_sv.fullScroll(ScrollView.FOCUS_UP);
              focus_pos=1;
              list_scroll_iv.setImageDrawable(getResources().getDrawable(R.drawable.blue_arrow_ic_down));
          }
        }
    });

}
Chirag Jain
  • 628
  • 11
  • 24