Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Android: append a character to my textview after every second

I want to append a dot(.) to my textview after every second. When it reaches 3 dots, it should reset to a single dot and continue the loop like this:

mytext.
mytext..
mytext...
mytext.
mytext..
mytext...

and so on

My java code:

MaterialTextView txt = alertdialog.findViewById(R.id.dialogtext);

new Timer().scheduleAtFixedRate(new TimerTask(){
    @Override
    public void run(){
        for(int i=0; i<3; i++){
            for(int j=0; j<i; j++){
                   //I am stuck here                         
            }
        }
    }
},0,1000);

I have no idea, how to write the logic for getting my desired objective. Please guide me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want to apply this like googles speech listener starts "Listening…"

Then you can use this below example.

Shape: Create a Drawable shape, (name: dot_background)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="oval">
  <solid android:color="#808080" />
</shape>

xml part , (name: activity_main)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dots_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
    android:id="@+id/dot1"
    android:layout_width="12dp"
    android:layout_height="12dp"
    android:background="@drawable/dot_background"
    android:visibility="invisible" />

<ImageView
    android:id="@+id/dot2"
    android:layout_width="12dp"
    android:layout_height="12dp"
    android:background="@drawable/dot_background"
    android:visibility="invisible" />

<ImageView
    android:id="@+id/dot3"
    android:layout_width="12dp"
    android:layout_height="12dp"
    android:background="@drawable/dot_background"
    android:visibility="invisible" />

</LinearLayout>

Java File:

    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.view.animation.AlphaAnimation;
    import android.view.animation.Animation;
    import android.widget.ImageView;
    import android.widget.LinearLayout;

    public class MainActivity extends AppCompatActivity {

        private static final long DOTS_ANIMATION_DELAY = 500; // Delay between dot visibility changes

        private LinearLayout dotsContainer;
        private Handler handler;
        private Runnable dotsAnimationRunnable;
        private boolean isVisible = false;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            dotsContainer = findViewById(R.id.dots_container);
            handler = new Handler();

            dotsAnimationRunnable = new Runnable() {
                @Override
                public void run() {
                    toggleDotsVisibility();
                    handler.postDelayed(this, DOTS_ANIMATION_DELAY);
                }
            };
        }

        @Override
        protected void onResume() {
            super.onResume();
            startDotsAnimation();
        }

        @Override
        protected void onPause() {
            super.onPause();
            stopDotsAnimation();
        }

        private void startDotsAnimation() {
            handler.postDelayed(dotsAnimationRunnable, DOTS_ANIMATION_DELAY);
        }

        private void stopDotsAnimation() {
            handler.removeCallbacks(dotsAnimationRunnable);
        }

        private void toggleDotsVisibility() {
            for (int i = 0; i < dotsContainer.getChildCount(); i++) {
                ImageView dot = (ImageView) dotsContainer.getChildAt(i);
                if (isVisible) {
                    fadeOut(dot);
                } else {
                    fadeIn(dot);
                }
            }
            isVisible = !isVisible;
        }

        private void fadeIn(ImageView dot) {
            Animation fadeInAnimation = new AlphaAnimation(0, 1);
            fadeInAnimation.setDuration(300);
            dot.startAnimation(fadeInAnimation);
            dot.setVisibility(ImageView.VISIBLE);
        }

        private void fadeOut(ImageView dot) {
            Animation fadeOutAnimation = new AlphaAnimation(1, 0);
            fadeOutAnimation.setDuration(300);
            dot.startAnimation(fadeOutAnimation);
            dot.setVisibility(ImageView.INVISIBLE);
        }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading