Skip to content

Commit

Permalink
Completed with DataBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
azzumw committed Jan 7, 2022
1 parent 9268848 commit 0147c5e
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 102 deletions.
3 changes: 2 additions & 1 deletion Unscramble/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
Expand Down Expand Up @@ -31,7 +32,7 @@ android {
}

buildFeatures{
viewBinding true
dataBinding = true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.fragment.app.viewModels
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.example.unscramble.R
import com.example.unscramble.databinding.FragmentGameBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand Down Expand Up @@ -37,7 +38,9 @@ class GameFragment : Fragment() {
savedInstanceState: Bundle?
): View? {
Log.e(TAG,"OnCreateView - fragment created/recreated")
binding = FragmentGameBinding.inflate(inflater,container,false)
// binding = FragmentGameBinding.inflate(inflater,container,false)
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_game,container,false)

Log.d(
TAG, "Word: ${viewModel.currentScrambledWord} " +
"Score: ${viewModel.score} WordCount: ${viewModel.currentWordCount}")
Expand All @@ -50,7 +53,10 @@ class GameFragment : Fragment() {
Log.e(TAG,"OnViewCreated")
// Update the UI

binding.gameViewModel = viewModel
binding.maxNoOfWords = MAX_NO_OF_WORDS

binding.lifecycleOwner = viewLifecycleOwner
//set up click listeners
binding.submit.setOnClickListener { onSubmitWord() }
binding.skip.setOnClickListener { onSkipWord() }
Expand All @@ -61,17 +67,17 @@ class GameFragment : Fragment() {
//This parameter helps the LiveData to be aware of the GameFragment
// lifecycle and notify the observer only when the GameFragment is
// in active states (STARTED or RESUMED).
viewModel.currentScrambledWord.observe(viewLifecycleOwner,
{it -> binding.textViewUnscrambledWord.text = it } )
// viewModel.currentScrambledWord.observe(viewLifecycleOwner,
// {it -> binding.textViewUnscrambledWord.text = it } )

viewModel.score.observe(viewLifecycleOwner,{
newScore -> binding.score.text = getString(R.string.score,newScore)
})
// viewModel.score.observe(viewLifecycleOwner,{
// newScore -> binding.score.text = getString(R.string.score,newScore)
// })

viewModel.currentWordCount.observe(viewLifecycleOwner,{
newWordCount -> binding.wordCount.text =
getString(R.string.word_count,newWordCount, MAX_NO_OF_WORDS)
})
// viewModel.currentWordCount.observe(viewLifecycleOwner,{
// newWordCount -> binding.wordCount.text =
// getString(R.string.word_count,newWordCount, MAX_NO_OF_WORDS)
// })

}

Expand Down
197 changes: 106 additions & 91 deletions Unscramble/app/src/main/res/layout/fragment_game.xml
Original file line number Diff line number Diff line change
@@ -1,105 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout
<data>
<variable
name="gameViewModel"
type="com.example.unscramble.ui.game.GameViewModel" />

<variable
name="maxNoOfWords"
type="int" />

</data>

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/default_padding"
tools:context=".ui.game.GameFragment">
android:layout_height="match_parent">

<Button
android:id="@+id/skip"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="0dp"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/default_padding"
android:layout_marginEnd="@dimen/default_padding"
android:text="@string/skip"
app:layout_constraintBaseline_toBaselineOf="@+id/submit"
app:layout_constraintEnd_toStartOf="@+id/submit"
app:layout_constraintStart_toStartOf="parent" />
android:padding="@dimen/default_padding"
tools:context=".ui.game.GameFragment">

<Button
android:id="@+id/submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_margin"
android:text="@string/submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/skip"
app:layout_constraintTop_toBottomOf="@+id/textField" />
<Button
android:id="@+id/skip"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/default_padding"
android:layout_marginEnd="@dimen/default_padding"
android:text="@string/skip"
app:layout_constraintBaseline_toBaselineOf="@+id/submit"
app:layout_constraintEnd_toStartOf="@+id/submit"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/textView_instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/instructions"
android:textSize="17sp"
app:layout_constraintBottom_toTopOf="@+id/textField"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_unscrambled_word" />
<Button
android:id="@+id/submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_margin"
android:text="@string/submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/skip"
app:layout_constraintTop_toBottomOf="@+id/textField" />

<TextView
android:id="@+id/textView_unscrambled_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline3"
app:layout_constraintBottom_toTopOf="@+id/textView_instructions"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/word_count"
tools:text="Scramble word" />
<TextView
android:id="@+id/textView_instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/instructions"
android:textSize="17sp"
app:layout_constraintBottom_toTopOf="@+id/textField"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_unscrambled_word" />

<TextView
android:id="@+id/word_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/word_count"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline6"
app:layout_constraintBottom_toTopOf="@+id/textView_unscrambled_word"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="3 of 10 words" />
<TextView
android:id="@+id/textView_unscrambled_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline3"
app:layout_constraintBottom_toTopOf="@+id/textView_instructions"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/word_count"
android:text="@{gameViewModel.currentScrambledWord}"
tools:text="Scramble word" />

<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/score"
android:textAllCaps="true"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Score: 20" />
<TextView
android:id="@+id/word_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{@string/word_count(gameViewModel.currentWordCount,maxNoOfWords)}"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline6"
app:layout_constraintBottom_toTopOf="@+id/textView_unscrambled_word"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="3 of 10 words" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
style="@style/Widget.Unscramble.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_margin"
android:hint="@string/enter_your_word"
app:errorIconDrawable="@drawable/ic_error"
app:helperTextTextAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
app:layout_constraintBottom_toTopOf="@+id/submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_instructions">
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{@string/score(gameViewModel.score)}"
android:textAllCaps="true"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Score: 20" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
style="@style/Widget.Unscramble.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_margin"
android:hint="@string/enter_your_word"
app:errorIconDrawable="@drawable/ic_error"
app:helperTextTextAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
app:layout_constraintBottom_toTopOf="@+id/submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_instructions">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/text_input_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName|textNoSuggestions"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/text_input_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName|textNoSuggestions"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>

0 comments on commit 0147c5e

Please sign in to comment.