-1

I have one text view and two buttons "Start" and "Stop"

When I click on start, I want to start the text view count from 0.0000000000, 0.0000000001, 0.0000000002,............

Please help me how to do so in android Studio.

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

    count = findViewById(R.id.count);
    start1 = findViewById(R.id.start1);
    stop1 = findViewById(R.id.stop1);

    start1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startcount();
        }
    });

    stop1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

}

private void startcount()  {


}

}

enter image description here

  • 1
    StackOverflow is a site for you to ask "where is the error inside my implementation logic" not "how to implement my logic from a blank". – Geno Chen Dec 16 '21 at 08:15
  • 1
    How about just start countdown from 0 and to show on UI just divide it by 1000000 ? – ADM Dec 16 '21 at 09:21
  • Person is asking for complete a task, instead of he trying something and asking the community to fix the problem. Person has done zero ground work. – SoftwareGuy Dec 22 '21 at 17:41

1 Answers1

0

The original answer is from this answer I need to increment a number every second using threads in android

I changed it a little for your need:

int counter = 0;

Thread counterThread=new Thread(()->{
        try{
            while(true){
                counter++;
                count.setText((counter / 1000000).ToString());
                Thread.sleep(100);
            }
        }
        catch (Exception error){
            Log.e(TAG, error);
        }
    });

counterThread.start();

So what's being done here?

Firstly, we are opening a new thread because we don't want to run such a long task on the main thread.

In this new thread we created (counterThread) we have a while loop that loops forever and inside it we have a few mroe lines of code:

we begin by increasing the counter variable above by one each time.

Then, we set the text of count to the counter variable devided by a 1000000 (we devide by this number to achive the result you wanted e.g. 0.000001).

Finally, we wait for a 100 milliseconds.

Ido Barnea
  • 142
  • 2
  • 13