Tuesday, September 13, 2011

Scroll View - scrollBy() and scrollTo()

The method scrollBy() and scrollTo() can be called t scroll a view, by the amount of pixels to scroll and specify the position to scroll.

Scroll View - scrollBy() and scrollTo()


package com.exercise.AndroidScrollBy;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;

public class AndroidScrollByActivity extends Activity {

Button buttonScrollUp, buttonScrollDown, buttonScrollToTop;
ScrollView myView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonScrollUp = (Button)findViewById(R.id.scrollup);
buttonScrollDown = (Button)findViewById(R.id.scrolldown);
buttonScrollToTop = (Button)findViewById(R.id.scrolltotop);
myView = (ScrollView)findViewById(R.id.myview);

buttonScrollUp.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myView.scrollBy(0, +20);
}});

buttonScrollDown.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myView.scrollBy(0, -20);
}});

buttonScrollToTop.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myView.scrollTo(0, 0);
}});
}
}




main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/scrollup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Scroll Up"
/>
<Button
android:id="@+id/scrolldown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Scroll Down"
/>
<Button
android:id="@+id/scrolltotop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Scroll To Top"
/>
<ScrollView
android:id="@+id/myview"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#A0A0A0"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="This\nView\nCan\nBe\nScroled\nBy Buttons!"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dummy Button"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dummy Button"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dummy Button"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>



4 comments:

oliviu vais said...

What if i want the scroll action to be continous? Any help?

Unknown said...

what about when i do not know height of view?

I have one scenario that

1) I have on text box and button on top.
2)There is gridview which contain 15 rows and 2 columns.It is shown below button.
3) Now if I write 13 in text box and press button, gridview automatically scroll to 13th row.

Is it posible?

Konkhmer said...

Hi sir, if i want it automatically scroll up and start from bottom again automatically without using button, so how to do.

Ex: i have 6 of table rows, and i want my table row running to up and start from bottom again in some period of time.

please give me some more example please

Anonymous said...

Hi sir, if i want it automatically scroll up and start from bottom again automatically without using button, so how to do.

What if i want the scroll action to be continuous? Any help?.