Skip to content

Instantly share code, notes, and snippets.

@ApolloZhu
Created October 8, 2017 19:53
Show Gist options
  • Save ApolloZhu/1cd0662f8173307a2942723756753fb7 to your computer and use it in GitHub Desktop.
Save ApolloZhu/1cd0662f8173307a2942723756753fb7 to your computer and use it in GitHub Desktop.
Lau Board Android
import android.content.Context
import android.media.MediaPlayer
import android.os.AsyncTask
/**
* Created by Apollonian on 9/16/17.
*/
object Lau {
val quotes = arrayOf(
R.raw.allright to "Alrigth",
R.raw.anyquestions to "Any Questions?",
R.raw.aye to "AYE",
R.raw.candobetter to "Can Do Better",
R.raw.doable to "It's Doable",
R.raw.java to "Cup of Java",
R.raw.lunch to "Lunchtime",
R.raw.moses to "I'm Moses",
R.raw.ohh to "OHH",
R.raw.ohno to "OH NO!",
R.raw.ok to "OK",
R.raw.putawayyourcellphone to "Put away Cell Phone",
R.raw.seemepls to "See Me",
R.raw.verydoable to "VERY Doable"
)
private var mediaPlayer: MediaPlayer? = null
private class PlayTask : AsyncTask<Pair<Int, Context>, Void, Void?>() {
override fun doInBackground(vararg p0: Pair<Int, Context>?): Void? {
mediaPlayer?.release()
val arg = p0.first() ?: return null
mediaPlayer = MediaPlayer.create(arg.second, quotes[arg.first].first)
mediaPlayer!!.start()
return null
}
}
fun speak(position: Int, context: Context) {
PlayTask().execute(position to context)
}
}
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.GridView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val gridView = findViewById<GridView>(R.id.grid_view)
gridView.adapter = TextViewCellAdapter(this)
gridView.setOnItemClickListener { _, _, position, _ ->
Lau.speak(position, this)
}
}
}
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
/**
* Created by Apollonian on 9/16/17.
*/
data class TextViewCellAdapter(val context: Context) : BaseAdapter() {
override fun getView(position: Int, recycledView: View?, parent: ViewGroup?): View {
val textView: TextView
if (recycledView is TextView) {
textView = recycledView
} else {
textView = TextView(context);
textView.textSize = 27f
textView.layoutParams = ViewGroup.LayoutParams(420, 250)
textView.setPadding(8, 8, 8, 8)
}
textView.text = Lau.quotes[position].second
return textView
}
override fun getItem(position: Int): Any = Lau.quotes[position]
override fun getItemId(position: Int): Long = position.toLong()
override fun getCount(): Int = Lau.quotes.count()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment