Good evening! I have been looking for a countdown timer for a long time and, wandering around the site, I found:
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); } }
}
TextView
? To place the bottom, use the attributeandroid:layout_gravity = "bottom"
for the widget - pavlofff