Created
March 22, 2017 11:38
-
-
Save AkshayChordiya/501534b698b713d8316b9ddc16ea0c6d to your computer and use it in GitHub Desktop.
Kotlin Article: MainActivity.kt with timer and views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
var mTimer: CountDownTimer? = null | |
var toolbar: Toolbar? = null | |
var time_in: TextView? = null | |
var time_out: TextView? = null | |
var remaining_time: TextView? = null | |
var go_home_progress: CircularProgressBar? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
bindViews() | |
setSupportActionBar(toolbar) | |
// Manually setting the start time | |
setStartTime(baseContext, System.currentTimeMillis()) | |
startTimer() | |
} | |
private fun bindViews() { | |
toolbar = findViewById(R.id.toolbar) as Toolbar | |
time_in = findViewById(R.id.time_in) as TextView | |
time_out = findViewById(R.id.time_out) as TextView | |
remaining_time = findViewById(R.id.remaining_time) as TextView | |
go_home_progress = findViewById(R.id.go_home_progress) as CircularProgressBar | |
} | |
private fun startTimer() { | |
mTimer = object : CountDownTimer(getRemainingTime(baseContext), 1000) { | |
override fun onFinish() { | |
time_in?.text = getString(R.string.default_time) | |
time_out?.text = getString(R.string.default_time) | |
remaining_time?.text = getString(R.string.default_remaining_time) | |
go_home_progress?.progress = 0F | |
removeStartTime(baseContext) | |
} | |
override fun onTick(millisUntilFinished: Long) { | |
remaining_time?.text = toReadableTime(millisUntilFinished) | |
go_home_progress?.setProgressWithAnimation(getPercentLeft(millisUntilFinished)) | |
} | |
} | |
if (didTimeStart(baseContext)) { | |
val remainingTime = getRemainingTime(baseContext) | |
if (remainingTime > 0) { | |
time_in?.text = toHourMinFormat(getStartTime(baseContext)) | |
time_out?.text = toHourMinFormat(getEndTime(baseContext)) | |
remaining_time?.text = toReadableTime(remainingTime) | |
go_home_progress?.progress = 100F | |
mTimer?.start() | |
} else { | |
time_in?.text = getString(R.string.default_time) | |
time_out?.text = getString(R.string.default_time) | |
remaining_time?.text = getString(R.string.default_remaining_time) | |
go_home_progress?.progress = 0F | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment