Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Main Activity of the Android Binary Converter App for the SSaurel's Channel
package com.ssaurel.binaryconverter
import android.app.Activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.inputmethod.InputMethodManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
encodeBtn.setOnClickListener {
encode()
}
decodeBtn.setOnClickListener {
decode()
}
}
private fun encode() {
val txt = textToConvert.text.toString()
val binary = BinaryConverter.strToBinary(txt)
resultTv.text = binary
hideKeyboard()
}
private fun decode() {
val binary = textToConvert.text.toString()
val txt = BinaryConverter.binaryToString(binary)
resultTv.text = txt
hideKeyboard()
}
private fun hideKeyboard() {
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
var view = currentFocus
if (view == null)
view = View(this)
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment