Skip to content

Commit

Permalink
Add Submit button with data biding
Browse files Browse the repository at this point in the history
  • Loading branch information
nka-coder committed May 9, 2021
1 parent bd619ca commit ab20cdf
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 46 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

dataBinding {
enabled = true
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand Down
58 changes: 57 additions & 1 deletion app/src/main/java/com/example/aboutme/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
package com.example.aboutme

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.example.aboutme.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

private val myName: MyName = MyName()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//setContentView(R.layout.activity_main)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.myName = myName

//findViewById<Button>(R.id.done_button).setOnClickListener {
// addNickname(it)
//}
binding.doneButton.setOnClickListener {
addNickname(it)
}
}

private fun addNickname(view: View) {
//val editText = findViewById<EditText>(R.id.nickname_edit)
//val nicknameTextView = findViewById<TextView>(R.id.nickname_text)

//nicknameTextView.text = editText.text
//editText.visibility = View.GONE
//view.visibility = View.GONE
//nicknameTextView.visibility = View.VISIBLE
binding.apply {
//nicknameText.text = binding.nicknameEdit.text
myName?.fullname = fullnameEdit.text.toString()
myName?.firstname = fullnameEdit.text.split(" ")[0].toString()
myName?.bio = "Hi, my name is "+ myName?.firstname +".\n\nI love fish." +
"\n\nThe kind that is alive and swims around in an aquarium or river, or a lake, and definitely the ocean." +
"\nFun fact is that I have several aquariums and also a river." +
"\nI like eating fish too. Raw fish. Grilled fish. Smoked fish. Poached fish - not so mush." +
"\nAnd sometimes I even go fishing.\n" +
"\nAnd even less sometimes, I actually catch something." +
"\n\nOnce, when I was camping in Canada, and very hungry, I even caught a large salmon with my hand." +
"\n\nI\'ll be happy to teach you how to make your own aquarium." +
"\nYou should ask someone else\n"
invalidateAll()
fullnameEdit.visibility = View.GONE
doneButton.visibility = View.GONE
firstnameText.visibility = View.VISIBLE
starImage.visibility = View.VISIBLE
bioScroll.visibility = View.VISIBLE
}

// Hide the keyboard
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/example/aboutme/MyName.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.aboutme


data class MyName(
var fullname: String ="",
var firstname: String ="",
var bio: String =""
)
99 changes: 69 additions & 30 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,42 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">

<TextView
android:id="@+id/name_text"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/name"
android:textAlignment="center" />
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
<variable
name="myName"
type="com.example.aboutme.MyName" />
</data>

<ImageView
android:id="@+id/star_image"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:contentDescription="@string/yellow_star"
app:srcCompat="@android:drawable/btn_star_big_on" />
android:orientation="vertical"
android:paddingStart="@dimen/padding"
android:paddingEnd="@dimen/padding">

<ScrollView
android:id="@+id/bio_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fullname_text"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={myName.fullname}"
android:textAlignment="center" />

<EditText
android:id="@+id/fullname_edit"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/what_is_your_fullname"
android:inputType="textPersonName"
android:textAlignment="center" />

<Button
android:id="@+id/done_button"
android:layout_width="match_parent"
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/bio_text"
android:id="@+id/firstname_text"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:text="@string/bio" />
</ScrollView>
android:textAlignment="center"
android:text="@={myName.firstname}"
android:visibility="gone" />

<ImageView
android:id="@+id/star_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:contentDescription="@string/yellow_star"
android:visibility="invisible"
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:visibility="invisible">

<TextView
android:id="@+id/bio_text"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.4"
android:paddingStart="@dimen/padding"
android:paddingEnd="@dimen/padding"
android:text="@={myName.bio}" />
</ScrollView>

</LinearLayout>
</LinearLayout>
</layout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="colorAccent">#76bf5e</color>
</resources>
18 changes: 3 additions & 15 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
<resources>
<string name="app_name">AboutMe</string>
<string name="name">Adamou Nchange Kouotou</string>
<string name="name">Fullname</string>
<string name="yellow_star">Yellow star</string>

<string name="bio">
Le lorem ipsum est, en imprimerie, une suite de mots sans
signification utilisée à titre provisoire pour calibrer une mise en page.

\n\n Le texte définitif venant remplacer le faux-texte dès qu\'il
est prêt ou que la mise en page est achevée. Généralement,
on utilise un texte en faux latin, le Lorem ipsum ou Lipsum.

\n\n Le lorem ipsum est, en imprimerie, une suite de mots sans signification
utilisée à titre provisoire pour calibrer une mise en page, le
texte définitif venant remplacer le faux-texte dès qu\'il est prêt.
\n\n Ou que la mise en page est achevée. Généralement, on utilise
un texte en faux latin, le Lorem ipsum ou Lipsum.\n\n
</string>
<string name="what_is_your_fullname">What is your Fullname?</string>
<string name="done">Submit</string>
</resources>

0 comments on commit ab20cdf

Please sign in to comment.