A ScrollView is a special type of FrameLayout in that it allows users to
scroll through a list of views that occupy more space than the physical
display.I just add some attributes Some rule for scrollview
1. Scrollview cannot hold
more than one child. If you have more than one child then you must place it
inside another layout like -Linear Layout, Relative Layout, Table Layout.
2. You should never use a
ScrollView with a ListView or GridView, because they both takes care of their
own vertical scrolling.
5. It only supports vertical
scrolling not horizontal scrolling.
Syntax…
[<ScrollView>
<LinearLayout>
<TextView>
<TextView>
<TextView>
…
<\LinearLayout>
<\ScrollView> ]
Example -XML
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button1"
android:padding="30dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button2"
android:padding="30dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button3"
android:padding="30dp"
/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button4"
android:padding="30dp"
/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button5"
android:padding="30dp"
/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button6"
android:padding="30dp"
/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button7"
android:padding="30dp"
/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button8"
android:padding="30dp"
/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button9"
android:padding="30dp"
/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button10"
android:padding="30dp"
/>
</LinearLayout>
</ScrollView>
Programattically ScrollView
XML
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ll1"
>
</ScrollView>
Java
public class MainActivity extends AppCompatActivity {
ScrollView layout1;
ScrollView.LayoutParams params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout1=(ScrollView) findViewById(R.id.ll1);
ScrollView scrollView=new ScrollView(this);
params=new ScrollView.LayoutParams(ScrollView.LayoutParams.WRAP_CONTENT,ScrollView.LayoutParams.WRAP_CONTENT);
Button button=new Button(this);
// button.setId(button);
button.setWidth(200);
button.setHeight(200);
button.setText("Program");
layout1.addView(button);
}
}
ScrollTo and ScrollBy
1. Scroll To - Set the
scrolled position of your view.
2. Scroll By - scroll
smoothly instead of immediately.
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="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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="Android is a mobile operating system (OS) currently
developed by Google,
based on the Linux kernel and designed primarily for touchscreen mobile devices
such as smartphones and tablets. Android's user interface is mainly based on
direct manipulation, using touch gestures that loosely correspond to real-world
actions,
such as swiping, tapping and pinching, to manipulate on-screen objects, along
with a virtual keyboard for
text input. In addition to touchscreen devices, Google has
further developed Android TV for televisions,
Android Auto for cars, and Android Wear for wrist watches, each with a
specialized user interface.
Variants of Android are also used on notebooks, game consoles, digital cameras,
and other electronics.
Android has the largest installed base of all operating systems of any kind.
Android has been the best selling OS on tablets since 2013, and on smartphones
it is dominant by any metric.
Initially developed by
Android, Inc., which Google bought in 2005,[16] Android was unveiled in 2007,
along with the founding
of the Open Handset Alliance – a consortium of hardware, software,
and telecommunication
companies devoted to advancing open standards for mobile devices.
[17] As of July 2013,
the Google Play store has had over one million Android applications published,
and over 50 billion
applications downloaded.[18] An April–May 2013 survey of mobile application
developers found that
71% of developers create
applications for Android,[19] and a 2015 survey found that 40% of
full-time professional
developers see Android as their priority target platform, which is comparable
to
Apple's iOS on 37% with
both platforms far above others.[20] In September 2015,
Android had 1.4 billion
monthly active users.[21]"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
JAVA
public class MainActivity extends AppCompatActivity {
ScrollView layout1;
Button buttonScrollUp,buttonScrollDown,buttonScrollToTop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonScrollUp = (Button)findViewById(R.id.scrollup);
buttonScrollDown = (Button)findViewById(R.id.scrolldown);
buttonScrollToTop = (Button)findViewById(R.id.scrolltotop);
layout1 = (ScrollView)findViewById(R.id.myview);
buttonScrollUp.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
layout1.scrollBy(0, +20);
}});
buttonScrollDown.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
layout1.scrollBy(0, -20);
}});
buttonScrollToTop.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
layout1.scrollTo(0, 0);
}});
}
}
*************************************************************************
fillViewReport
Defines
whether the scrollview should stretch its content to fill the viewport.
code.
It's
value must be a boolean "true or false ".
android:fillViewport="true/false"
*******************************************************
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:paddingTop="6dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Welcome to My Application" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#ff106510"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="12dip" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:paddingBottom="6dip"
android:text="@string/hello" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/bottom_bar"
android:gravity="center_vertical">
<Button
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:text="Accept" />
<Button
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:text="Refuse" />
</LinearLayout>
</LinearLayout>
</ScrollView>
************************************************************************
Fling
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/hello"
android:textStyle="bold" />
<TextView
android:id="@+id/tvTap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip" >
</TextView>
<TextView
android:id="@+id/tvTapEvent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip" >
</TextView>
<ScrollView
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button5" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button6" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button7" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button8" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button9" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button10" />
</LinearLayout>
</ScrollView>
</LinearLayout>
java
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener
{
private GestureDetector gd;
private TextView tvTap;
String TAG = getClass().getSimpleName();
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTap = (TextView)findViewById(R.id.tvTap);
gd
= new GestureDetector(this);
gd.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener()
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e)
{
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return gd.onTouchEvent(event);//return the double tap events
}
@Override
public boolean onDown(MotionEvent e)
{
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
{
return false;
}
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
tvTap.setText("Flip Right to Left");
Log.v(TAG, "Right to Left");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
tvTap.setText("Flip Left to Right");
Log.v(TAG, "Left to Right");
}
} catch (Exception e)
{
}
return false;
}
@Override
public void onLongPress(MotionEvent e)
{
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
}
@Override
public void onShowPress(MotionEvent e)
@Override
public boolean onSingleTapUp(MotionEvent e)
{
return false;
}
}
Full Scroll ( focus - Up ,Down)
Handles
scrolling in response to a "home/end" shortcut press. This method
will scroll the view to the top or bottom and give the focus to the
topmost/bottommost component in the new visible area. If no component is a good
candidate for focus, this scrollview reclaims the focus.
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"
>
<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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="Android is a mobile operating system (OS) currently
developed by Google, based on the Linux kernel and designed primarily for touch
screen mobile devices such as smart phones and tablets. Android's user
interface is mainly based on
direct manipulation, using touch gestures that loosely correspond to real-world
actions, such as swiping, tapping and pinching, to manipulate on-screen
objects, along with a virtual keyboard for text input. In addition to touch screen
devices, Google has further developed Android TV for televisions, Android Auto
for cars, and Android Wear for wrist watches, each with a specialized user
interface. Variants of Android are also used on notebooks, game consoles,
digital cameras, and other electronics. Android has the largest installed base
of all operating systems of any kind.
Android has been the best selling OS on tablets since 2013, and on smartphones
it is dominant by any metric. Initially developed by Android, Inc., which
Google bought in 2005,[16] Android was unveiled in 2007, along with the
founding of the Open Handset Alliance – a consortium of hardware, software, and
telecommunication companies devoted to advancing open standards for mobile
devices. [17] As of July 2013, the Google Play store has had over one million
Android applications published, and over 50 billion applications
downloaded.[18] An April–May 2013 survey of mobile application developers found
that 71% of developers create applications for Android,[19] and a 2015 survey
found that 40% of full-time professional developers see Android as their
priority target platform, which is comparable toApple's iOS on 37% with both
platforms far above others.[20] In September 2015,Android had 1.4 billion
monthly active users.[21]"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Java
public class MainActivity extends AppCompatActivity {
ScrollView mScrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScrollView = (ScrollView) findViewById(R.id.myview);
mScrollView.post(new Runnable() {
@Override
public void run() {
mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}
No comments:
Post a Comment