0

I have a simple animation attached to dynamic textview that i am creating but what i want is to add delay while adding them. Please guide me how to do that.

    LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
    final HorizontalScrollView hsv = new HorizontalScrollView(TestViewActivity.this);
    LinearLayout lhsv = new LinearLayout(TestViewActivity.this);

    Animation a1 = new AlphaAnimation(0.00f, 1.00f);
    a1.setDuration(350);
    a1.setFillAfter(true);  

    for(int k =0; k < 5; k++){
        // may be some handler here but how ?
        TextView tv = new TextView(TestViewActivity.this);
        tv.setText("Text");
        tv.setTextSize(42);
        tv.setPadding(10, 0, 10, 0);
        tv.setVisibility(View.INVISIBLE);
        tv.clearAnimation();
        tv.startAnimation(a1);

        lhsv.addView(tv, k);
    }

    hsv.addView(lhsv);

    ll.addView(hsv);

Thanks

Based on suggestion i have tried this it works, but all view come all together, what i want is that one view enter then bit of delay then another view enter and so on...this is the code.

   final Handler handler = new Handler();
    LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
    final HorizontalScrollView hsv = new HorizontalScrollView(TestViewActivity.this);
    final LinearLayout lhsv = new LinearLayout(TestViewActivity.this);

    final Animation a1 = new AlphaAnimation(0.00f, 1.00f);
    a1.setDuration(350);
    a1.setFillAfter(true);  
    for(int k =0; k < 5; k++){
         new Handler().postDelayed(new Runnable() {
                public void run() {
                    //write your code here...
                    final TextView tv = new TextView(TestViewActivity.this);  
                    tv.setText("Text");
                    tv.setTextSize(42);
                    tv.setPadding(10, 0, 10, 0);
                    tv.setVisibility(View.INVISIBLE);
                    tv.clearAnimation();   
                    tv.startAnimation(a1);
                    lhsv.addView(tv, temp);
                    temp++;
                }
            }, 2000);


    }

    hsv.addView(lhsv);
    ll.addView(hsv);
Programmer
  • 5,360
  • 10
  • 37
  • 61

3 Answers3

6

use this

 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            //write your code here...
        }
    }, delay_time);
Venkata Krishna
  • 1,543
  • 1
  • 11
  • 19
  • Thanks but the problem is how can i create textview in an inner class and add to a view as i have to make k a final. – Programmer Feb 14 '12 at 07:12
  • create Textview outside the above(Runnable block) and use that id in inside the Runnable bloock.Declare your Textview outside the onCreate means create as global variable instead of local variable. – Venkata Krishna Feb 14 '12 at 07:14
  • ok thanks bro...but what about lhsv.addView(tv, k); if i put that thing inside loop it wont allow me to change k as it is made final. – Programmer Feb 14 '12 at 07:17
  • declare temporary local variable and assign 'k' as follows like final int temp=k; and use 'temp' variabel instead of k. – Venkata Krishna Feb 14 '12 at 07:19
  • Thanks bro i modified the code but all views come all of sudden and not one by one... – Programmer Feb 14 '12 at 07:28
0

Try AysncTask. It is meant for the same cause.

Mxyk
  • 10,678
  • 16
  • 57
  • 76
Nitin Bansal
  • 2,986
  • 3
  • 23
  • 30
0

try this...

 for(int k =0; k < 5; k++){
   //write your code here...
                        final TextView tv = new TextView(TestViewActivity.this);  
                        tv.setText("Text");
                        tv.setTextSize(42);
                        tv.setPadding(10, 0, 10, 0);
                        tv.setVisibility(View.INVISIBLE);
                        tv.clearAnimation();   
                        tv.startAnimation(a1);
                        lhsv.addView(tv, temp);
                        temp++;
             new Handler().postDelayed(new Runnable() {
                    public void run() {

                    }
                }, 2000);
}
Venkata Krishna
  • 1,543
  • 1
  • 11
  • 19