Good evening! I have been looking for a countdown timer for a long time and, wandering around the site, I found:

On the phone In Android Studio

I only needed the display of days, so I corrected the code a little. The question is that I can’t move this TextView on screen with the contents of the days before the event. In other words, it is at the top of the screen, and I need it to be at the bottom.

Moving TextView (id: tv2) on the screen, the shift in the emulator does not occur, all data is still displayed in the upper left corner of the screen. What could be the problem? How can you solve it?

Markup :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".ImagePickerActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv2" android:layout_marginTop="200dp" /> </LinearLayout> 

code :

 public class MainActivity extends Activity { /** Called when the activity is first created. */ TextView tv2; long diff; long milliseconds; long endTime; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv2 = new TextView(this); this.setContentView(tv2); SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm"); formatter.setLenient(false); String oldTime = "01.08.2015, 12:00"; Date oldDate; try { oldDate = formatter.parse(oldTime); milliseconds = oldDate.getTime(); //long startTime = System.currentTimeMillis(); // do your work... long endTime=System.currentTimeMillis(); diff = endTime-milliseconds; Log.e("day", "miliday"+diff); long seconds = (long) (diff / 1000) % 60 ; Log.e("secnd", "miliday"+seconds); long minutes = (long) ((diff / (1000*60)) % 60); Log.e("minute", "miliday"+minutes); long hours = (long) ((diff / (1000*60*60)) % 24); Log.e("hour", "miliday"+hours); long days = (int)((diff / (1000*60*60*24)) % 365); Log.e("days", "miliday"+days); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } Long serverUptimeSeconds = (System.currentTimeMillis() - milliseconds) / 1000; String serverUptimeText = String.format("%d days %d hours %d minutes %d seconds", serverUptimeSeconds / 86400, ( serverUptimeSeconds % 86400) / 3600 , ((serverUptimeSeconds % 86400) % 3600 ) / 60, ((serverUptimeSeconds % 86400) % 3600 ) % 60 ); Log.v("jjj", "miliday"+serverUptimeText); MyCount counter = new MyCount(milliseconds,1000); counter.start(); } // countdowntimer is an abstract class, so extend it and fill in methods public class MyCount extends CountDownTimer { public MyCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { tv2.setText("done!"); } @Override public void onTick(long millisUntilFinished) { //tv.setText("Left: " + millisUntilFinished / 1000); long diff = endTime - millisUntilFinished; Log.e("left", "miliday"+diff); long seconds = (long) (diff / 1000) % 60 ; //Log.e("secnd", "miliday"+seconds); long minutes = (long) ((diff / (1000*60)) % 60); //Log.e("minute", "miliday"+minutes); long hours = (long) ((diff / (1000*60*60)) % 24); //Log.e("hour", "miliday"+hours); int days = (int)((diff / (1000*60*60*24)) % 365); Log.v("days", "miliday"+days); Long serverUptimeSeconds = (millisUntilFinished - System.currentTimeMillis()) / 2000; String serverUptimeText = String.format("%d", serverUptimeSeconds / 86400, ( serverUptimeSeconds % 86400) / 3600 , ((serverUptimeSeconds % 86400) % 3600 ) / 60, ((serverUptimeSeconds % 86400) % 3600 ) % 60 ); Log.v("new its", "miliday"+serverUptimeText); // tv.setText(days +":"+hours+":"+minutes + ":" + seconds); tv2.setText(serverUptimeText); } } 

}

  • one
    The location of the widgets is specified in the markup, not in the code. Where is she? - pavlofff
  • in the above code you do not move anything anywhere - Vladyslav Matviienko
  • As I understand it, the contents of the time reference should appear on TextView2, right? Why then does it appear only in the upper left corner? - Dmitry
  • How did you try to move this TextView ? To place the bottom, use the attribute android:layout_gravity = "bottom" for the widget - pavlofff
  • @pavlofff, and posted: added the attribute "bottom" to Gravity. The fact is that when you start on the emulator, nothing is shown at all (no elements, except days before the event). And in android studio I added TextView. Now I will attach 2 screenshots. - Dmitry

1 answer 1

In your onCreate method, onCreate is called

 tv2 = new TextView(this); this.setContentView(tv2); 

but you must

 tv2 = (TextView)findViewById(R.id.tv2); 

The setContentView() method in a normal situation should be called once in the onCreate() method, since It is designed to set the content (a layer with your view elements) that will be displayed on the screen, and preferably immediately after super.onCreate() , so as not to catch the NPE later when searching for views and setting values ​​for them.

  • Thank you very much! Very helpful! - Dmitry