-
Notifications
You must be signed in to change notification settings - Fork 200
/
MainActivity.kt
104 lines (79 loc) · 3.72 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.klinker.android.link_builder_example
import android.content.Intent
import android.graphics.Color
import android.graphics.Typeface
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.klinker.android.link_builder.Link
import com.klinker.android.link_builder.applyLinks
import java.util.regex.Pattern
class MainActivity : AppCompatActivity() {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById<View>(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
toolbar.setTitle(R.string.app_name)
val demoText = findViewById<View>(R.id.test_text) as TextView
demoText.applyLinks(getLinks())
}
private fun getLinks(): List<Link> {
val github = Link("TextView-LinkBuilder")
.setTypeface(Typeface.DEFAULT_BOLD)
.setOnClickListener { openLink(GITHUB_LINK) }
val mentions = Link(Pattern.compile("@\\w{1,15}"))
.setTextColor(Color.parseColor("#00BCD4"))
.setHighlightAlpha(.4f)
.setOnClickListener { clickedText ->
openLink(TWITTER_PROFILE + clickedText.replace("@", ""))
}
val numbers = Link(Pattern.compile("[0-9]+"))
.setTextColor(Color.parseColor("#FF9800"))
.setOnClickListener { showToast("Clicked: $it") }
val longClickHere = Link("here")
.setTextColor(Color.parseColor("#259B24"))
.setOnLongClickListener { showToast("You long clicked. Nice job.") }
val yes = Link("Yes").setUnderlined(true)
.setTextColor(Color.parseColor("#FFEB3B"))
val no = Link("No").setUnderlined(false)
.setTextColor(Color.parseColor("#FFEB3B"))
val bold = Link("bold").setBold(true)
.setTextColor(Color.parseColor("#FF0000"))
val prepend = Link("prepended").setPrependedText("(!)")
val appended = Link("appended").setAppendedText("(!)")
val playStore = Link("Play Store")
.setTextColor(Color.parseColor("#FF9800"))
.setTextColorOfHighlightedLink(Color.parseColor("#FF6600"))
.setHighlightAlpha(0f)
.setOnClickListener { openLink(PLAY_STORE) }
return listOf(github, mentions, numbers, longClickHere, yes, no, bold, prepend, appended, playStore)
}
private fun openLink(link: String) {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(link))
startActivity(browserIntent)
}
private fun showToast(text: String) {
Toast.makeText(this@MainActivity, text, Toast.LENGTH_SHORT).show()
}
companion object {
private const val GITHUB_LINK = "https://github.com/klinker24"
private const val TWITTER_PROFILE = "https://twitter.com/"
private const val PLAY_STORE = "https://play.google.com/store/apps/developer?id=Klinker+Apps&hl=en"
}
}