Databinding

This commit is contained in:
2020-03-06 01:37:57 -05:00
parent 698d7cd2ce
commit 42abd36a45
6 changed files with 109 additions and 33 deletions

View File

@@ -20,6 +20,10 @@ android {
enabled = true
}
dataBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled false

View File

@@ -1,15 +1,42 @@
package dev.andrewkemp.aboutme
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.databinding.DataBindingUtil
import dev.andrewkemp.aboutme.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var myName: MyName = MyName("Andrew Kemp")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(R.layout.activity_main)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.myName = myName
done_button.setOnClickListener {
addNickname(it)
}
}
private fun addNickname(view: View) {
binding.apply {
myName?.nickname = nicknameEdit.text.toString()
invalidateAll()
nicknameEdit.visibility = View.GONE
doneButton.visibility = View.GONE
nicknameText.visibility = View.VISIBLE
}
hideKeyboard(view)
}
private fun hideKeyboard(view: View) {
var imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}

View File

@@ -0,0 +1,3 @@
package dev.andrewkemp.aboutme
data class MyName(var name: String = "", var nickname: String = "")

View File

@@ -1,40 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingStart="@dimen/padding"
android:paddingEnd="@dimen/padding">
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/name_text"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/andrew_kemp"
android:textAlignment="center" />
<data>
<variable
name="myName"
type="dev.andrewkemp.aboutme.MyName" />
</data>
<ImageView
android:id="@+id/star_image"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_margin"
android:contentDescription="@string/yellow_star"
app:srcCompat="@android:drawable/btn_star_big_on" />
<ScrollView
android:id="@+id/bio_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingStart="@dimen/padding"
android:paddingEnd="@dimen/padding">
<TextView
android:id="@+id/bio_text"
android:id="@+id/name_text"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:text="@string/bio" />
</ScrollView>
android:text="@={myName.name}"
android:textAlignment="center" />
</LinearLayout>
<EditText
android:id="@+id/nickname_edit"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/what_is_your_nickname"
android:importantForAutofill="no"
android:inputType="textMultiLine|textPersonName"
android:textAlignment="center" />
<Button
android:id="@+id/done_button"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/layout_margin"
android:fontFamily="@font/roboto"
android:text="@string/done" />
<TextView
android:id="@+id/nickname_text"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={myName.nickname}"
android:textAlignment="center"
android:visibility="gone" />
<ImageView
android:id="@+id/star_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_margin"
android:contentDescription="@string/yellow_star"
app:srcCompat="@android:drawable/btn_star_big_on" />
<ScrollView
android:id="@+id/bio_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/bio_text"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:text="@string/bio" />
</ScrollView>
</LinearLayout>
</layout>

View File

@@ -1,6 +1,5 @@
<resources>
<string name="app_name">About Me</string>
<string name="andrew_kemp">Andrew Kemp</string>
<string name="yellow_star">Yellow Star</string>
<string name="bio">
Hi, my name is Andrew!
@@ -10,4 +9,6 @@
\n\u2022 Avid video gamer, and world traveler
\n\u2022 purveyor of marine life, and avid scuba diver
</string>
<string name="what_is_your_nickname">What is your nickname?</string>
<string name="done">Done</string>
</resources>