Pages

Thursday, September 8, 2016

Android Example -Simple Scroll View with Button and TextView

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="in.kantapp.example8.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Clear"
            android:id="@+id/clear" />
        <TextView
            android:layout_width="match_parent"
            android:text="@string/scroll"
            android:textSize="20dp"
            android:textColor="#000000"
            android:layout_height="wrap_content"
            android:id="@+id/txt" />
    </LinearLayout>
</ScrollView>

MainActivity.java
package in.kantapp.example8;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button= (Button) findViewById(R.id.clear);
        final TextView textView= (TextView) findViewById(R.id.txt);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                textView.setText("");
            }
        });
    }
}

string.xml
<resources>
    <string name="app_name">"Example 8 "</string>
    <string name="scroll">Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container.

ScrollView only supports vertical scrolling. For horizontal scrolling, use HorizontalScrollView.</string>
</resources>

No comments:

Post a Comment