uopspop

Untitled

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