divine_rythm
divine_rythm

Reputation: 159

Date is not working in TextView when switched between tabs and app gets down when navigation is used

When I switch between my tabs the date won't show. I have searched for other methods for showing date with the help of textview but most of them didn't work. Only the method of thread worked for me but it has an issue that it doesn't show date in all tabs and the app goes down when we navigate from the navigation drawer.

preview

My code:

    Thread c = new Thread(){
            @Override
            public void run()
            {
                try {
                    while (!isInterrupted())
                    {
                        Thread.sleep(1000);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                TextView tdate =(TextView)findViewById(R.id.date);
                                long date = System.currentTimeMillis();
                                SimpleDateFormat sdf =new SimpleDateFormat("dd MMM,yyyy");
                                String dateString =sdf.format(date);
                                tdate.setText(dateString);
                            }
                        });
                    }
                }catch (InterruptedException a)
                {

                }
            }
        };
        c.start();

`

Upvotes: 0

Views: 44

Answers (0)

Related Questions