Loading

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>



SHARE TWEET

Thank you for reading this article Scroll View - scrollBy() and scrollTo() With URL http://x-tutorials.blogspot.com/2011/09/scroll-view-scrollby-and-scrollto.html. Also a time to read the other articles.

0 comments:

Write your comment for this article Scroll View - scrollBy() and scrollTo() above!