Advertisement
uopspop

Untitled

Sep 27th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. package com.example.sam.scrollviewprac;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.LinearLayout;
  7. import android.widget.ScrollView;
  8. import android.widget.TextView;
  9.  
  10. public class MainActivity extends AppCompatActivity {
  11.     private TextView tvCount;
  12.     private ScrollView scrollView;
  13.     private LinearLayout linearLayout;
  14.     private int count = 0;
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_main);
  19.         findViews();
  20.         // Counter default value: 0
  21.         this.tvCount.setText(Integer.toString(this.count));
  22.     }
  23.  
  24.     private void findViews() {
  25.         tvCount = (TextView)findViewById(R.id.tvCount);
  26.         scrollView = (ScrollView)findViewById(R.id.scrollView);
  27.         linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
  28.     }
  29.  
  30.     public void onAddClick(View view){
  31.         this.count++;
  32.         this.tvCount.setText(Integer.toString(this.count));
  33.         TextView textView = new TextView(this);
  34.         textView.setText(Integer.toString(this.count) + ": This is a line.");
  35.         this.linearLayout.addView(textView);
  36.         // first updated and then scrolls to the "new" bottom
  37.         scrollView.postDelayed(new Runnable(){
  38.             @Override
  39.             public void run() {
  40.                 scrollView.fullScroll(View.FOCUS_DOWN);
  41.             }
  42.         }, 100);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement