Android (Kotlin): text now showing after I set it - android

Playing around with Android and following this simple introduction but I can't get the line resultsTextView.text = rand.toString() to trigger. Not sure what I'm missing.
The main Kotlin code looks like this (MainActivity.kt):
package io.github.ovid.randomizer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton = findViewById<Button>(R.id.rollButton)
val resultsTextView = findViewById<TextView>(R.id.resultsTextView)
val seekBar = findViewById<SeekBar>(R.id.seekBar)
rollButton.setOnContextClickListener {
val rand = Random.nextInt(seekBar.progress) + 1
resultsTextView.text = rand.toString()
true
}
}
}
And my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity" >
<Button
android:id="#+id/rollButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginLeft="96dp"
android:layout_marginEnd="96dp"
android:layout_marginRight="96dp"
android:layout_marginBottom="24dp"
android:text="Roll"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<SeekBar
android:id="#+id/seekBar"
style="#style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="24dp"
android:max="10"
android:progress="3"
app:layout_constraintBottom_toTopOf="#+id/rollButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginBottom="16dp"
android:text="How Many"
app:layout_constraintBottom_toTopOf="#+id/seekBar"
app:layout_constraintStart_toStartOf="parent" />
<View
android:id="#+id/divider"
android:layout_width="409dp"
android:layout_height="1dp"
android:layout_marginBottom="16dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="#+id/textView"
tools:layout_editor_absoluteX="1dp" />
<TextView
android:id="#+id/resultsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textSize="144sp"
app:layout_constraintBottom_toTopOf="#+id/divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Everything on the layout in the emulator looks correct, but pressing the "Roll" button shows I clicked the button, but no text shows up in the resultsTextView.

D'oh! It was setOnClickListener, not setOnContextClickListener. Made the change and removed the true at the end and it worked fine. Sorry for the noise.

Just replace the event method from setOnContextClickListener to setOnClickListener, an it will work fine. Just like this:
rollButton.setOnClickListener {
val rand = Random.nextInt(seekBar.progress) + 1
resultsTextView.text = rand.toString()
}

Related

Unresolved reference to constraintLayout variable in Kotlin code in android app

I am following this tutorial to create an android app with space animation stuff. In the step titled "Transition Manager" you're instructed to add the following code to your MainActivity.kt file:
`
constraintSet1.clone(constraintLayout) //1
constraintSet2.clone(this, R.layout.activity_main) //2
departButton.setOnClickListener { //3
//apply the transition
TransitionManager.beginDelayedTransition(constraintLayout) //4
val constraint = if (!isOffscreen) constraintSet1 else constraintSet2
isOffscreen = !isOffscreen
constraint.applyTo(constraintLayout) //5
}
`
When this code is added the variable "constraintLayout" throws up an unresolved reference error
I think constraintLayout is supposed to be pointing to something in the xml and something is failing to make that happen, I just don't know what.
The kotlin code after adding the above block is:
`
package com.raywenderlich.android.razegalactic
import android.os.Bundle
import android.support.constraint.*
import android.support.v7.app.AppCompatActivity
import android.transition.TransitionManager
import kotlinx.android.synthetic.main.keyframe1.*
/**
* Main Screen
*/
class MainActivity : AppCompatActivity() {
private val constraintSet1 = ConstraintSet()
private val constraintSet2 = ConstraintSet()
private var isOffscreen = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.keyframe1)
switch1.setOnCheckedChangeListener { _, isChecked ->
switch1.setText(if (isChecked) R.string.round_trip else R.string.one_way)
constraintSet1.clone(constraintLayout) //1
constraintSet2.clone(this, R.layout.activity_main) //2
departButton.setOnClickListener { //3
//apply the transition
TransitionManager.beginDelayedTransition(constraintLayout) //4
val constraint = if (!isOffscreen) constraintSet1 else constraintSet2
isOffscreen = !isOffscreen
constraint.applyTo(constraintLayout) //5
}
}
}
}
`
The main xml is as follows:
`
<android.support.constraint.ConstraintLayout 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:id="#+id/ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/flightsIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/rocket_icon"
app:layout_constraintBottom_toBottomOf="#+id/spaceStationIcon"
app:layout_constraintEnd_toStartOf="#+id/roverIcon"
app:layout_constraintStart_toEndOf="#+id/spaceStationIcon"
app:layout_constraintTop_toTopOf="#+id/spaceStationIcon" />
<ImageView
android:id="#+id/spaceStationIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="#drawable/space_station_icon"
app:layout_constraintEnd_toStartOf="#+id/flightsIcon"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/roverIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/rover_icon"
app:layout_constraintBottom_toBottomOf="#+id/flightsIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/flightsIcon"
app:layout_constraintTop_toTopOf="#+id/flightsIcon" />
<TextView
android:id="#+id/spaceStationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/space_stations"
app:layout_constraintEnd_toEndOf="#+id/spaceStationIcon"
app:layout_constraintStart_toStartOf="#+id/spaceStationIcon"
app:layout_constraintTop_toBottomOf="#+id/spaceStationIcon" />
<TextView
android:id="#+id/textView1"
android:layout_width="124dp"
android:layout_height="98dp"
android:layout_marginEnd="40dp"
android:background="#color/colorPrimary"
android:gravity="center"
android:paddingEnd="20dp"
android:text="#string/dca"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#+id/doubleArrowsIcon"
app:layout_constraintEnd_toEndOf="#+id/doubleArrowsIcon"
app:layout_constraintTop_toTopOf="#+id/doubleArrowsIcon" />
<TextView
android:id="#+id/roverLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/rovers"
app:layout_constraintEnd_toEndOf="#+id/roverIcon"
app:layout_constraintStart_toStartOf="#+id/roverIcon"
app:layout_constraintTop_toBottomOf="#+id/roverIcon" />
<TextView
android:id="#+id/textView2"
android:layout_width="124dp"
android:layout_height="98dp"
android:layout_marginStart="40dp"
android:background="#color/colorPrimary"
android:gravity="center"
android:paddingStart="20dp"
android:text="#string/mars"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#+id/doubleArrowsIcon"
app:layout_constraintStart_toStartOf="#+id/doubleArrowsIcon"
app:layout_constraintTop_toTopOf="#+id/doubleArrowsIcon" />
<TextView
android:id="#+id/flightsLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/flights"
app:layout_constraintEnd_toEndOf="#+id/flightsIcon"
app:layout_constraintStart_toStartOf="#+id/flightsIcon"
app:layout_constraintTop_toBottomOf="#+id/flightsIcon" />
<ImageView
android:id="#+id/doubleArrowsIcon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="40dp"
android:src="#drawable/double_arrows"
app:layout_constraintBottom_toTopOf="#+id/guideline1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Switch
android:id="#+id/switch1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="200dp"
android:background="#color/colorAccent"
android:checked="false"
android:padding="8dp"
android:switchPadding="24dp"
android:text="#string/one_way"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorAccent"
android:padding="8dp"
android:text="#string/traveller"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toBottomOf="#+id/switch1" />
<ImageView
android:id="#+id/rocketIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="8dp"
android:src="#drawable/rocket_icon"
app:layout_constraintCircle="#id/galaxyIcon"
app:layout_constraintCircleAngle="270"
app:layout_constraintCircleRadius="100dp" />
<ImageView
android:id="#+id/galaxyIcon"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/galaxy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.495" />
<Button
android:id="#+id/departButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/depart"
android:textColor="#android:color/white"
app:backgroundTint="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="158dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="0dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintGuide_percent="1" />
</android.support.constraint.ConstraintLayout>
`
can anyone tell me what I might be missing or how this can be fixed?
Thanks in advance.
You're suppose to reference a view from the xml before you can use it.
Do this, also if the name of the layout the MainActivity is suppose to use is main.xml, it should be the one your setContentView use not some other layout.
lateinit var cosntraintLayout: ConstraintLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main) // <- main.xml
cosntraintLayout = findViewById<ConstraintLayout>(R.id.ConstraintLayout) // <- add this line
...
...
}
switch1 and departButton will also throw the same error, but I'll leave it for you to figure it out based on the code I provided.

Android material TextInputLayout doesn't reconnect outline after setHintEnabled(false)

Using com.google.android.material:material:1.6.1, when setting hint enabled to true and then to false, the outline gets broken. Here is the example code.
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val someHintButton = findViewById<MaterialButton>(R.id.someHintButton)
val noHintButton = findViewById<MaterialButton>(R.id.noHintButton)
val textInput = findViewById<TextInputEditText>(R.id.textInput)
val textLayout = findViewById<TextInputLayout>(R.id.textLayout)
someHintButton.setOnClickListener {
textInput.setText("Some text")
textLayout.setHintEnabled(true)
textLayout.setHint("Entered text:")
}
noHintButton.setOnClickListener {
textLayout.setHint(null)
textLayout.setHintEnabled(false)
textInput.setText("Some text will be entered here")
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:layout_marginTop="5dp"
android:layout_marginHorizontal="5dp"
android:id="#+id/textLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
>
<com.google.android.material.textfield.TextInputEditText
android:focusable="false"
android:editable="false"
android:id="#+id/textInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here some text"
android:textAlignment="center"
android:inputType="none"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/someHintButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="some hint"
android:textColor="#color/black"
android:textColorLink="#FFFFFF"
app:backgroundTint="#FFFFFF"
app:cornerRadius="8dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<com.google.android.material.button.MaterialButton
android:id="#+id/noHintButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No hint"
android:textColor="#color/black"
android:textColorLink="#FFFFFF"
app:backgroundTint="#FFFFFF"
app:cornerRadius="8dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Screen after launch:
Screen after 'Some hint' pressed:
Screen after 'No hint' pressed:
setHint(null) or setHint("") don't do any difference. Why doesn't outline get reconnected? Please advise.

Create multiple sets of plus and minus buttons with TextView on same page of App (Kotlin)

I have two controls that i can increase or decrease an integer. I click on either the first or second set of plus and minus buttons,despite being separate functions, it'll display the Integers on both outputs. For example if I click on the plus and minus button for the integer_number_1, it'll display the numbers on both the integer_number_1 and integer_number_2 outputs/TextView.
How do I fix this??? I've so many different ways and none of them worked.
Here's my xml section:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:layout_marginBottom="549dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/decrease_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="#+id/integer_number_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="16dp"
android:inputType="number"
android:text="0"
android:textSize="70sp"
android:textStyle="bold" />
<Button
android:id="#+id/increase_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
<Button
android:id="#+id/decrease_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/integer_number_2"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.317" />
<Button
android:id="#+id/increase_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.579"
app:layout_constraintStart_toEndOf="#+id/integer_number_2"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.313" />
<TextView
android:id="#+id/integer_number_2"
android:layout_width="46dp"
android:layout_height="99dp"
android:layout_marginStart="46dp"
android:layout_marginLeft="46dp"
android:inputType="number"
android:text="0"
android:textSize="70sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.426"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.297" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here's my kt file:
package com.example.plus_and_minus_input
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
increase_1.setOnClickListener { increaseInteger1() }
decrease_1.setOnClickListener { decreaseInteger1() }
increase_2.setOnClickListener { increaseInteger2() }
decrease_2.setOnClickListener { decreaseInteger2() }
}
fun increaseInteger1() {
display(integer_number_1.text.toString().toInt() + 1)
}
fun decreaseInteger1() {
display(integer_number_1.text.toString().toInt() - 1)
}
fun increaseInteger2() {
display(integer_number_2.text.toString().toInt() + 1)
}
fun decreaseInteger2() {
display(integer_number_2.text.toString().toInt() - 1)
}
private fun display(number: Int) {
integer_number_1.setText("$number")
integer_number_2.setText("$number")
}
}
I think you need to separate this following display methods.
private fun display(number: Int) {
integer_number_1.setText("$number")
integer_number_2.setText("$number")
}
to
private fun display_number_1(number: Int) {
integer_number_1.setText("$number")
}
and
private fun display_number_2(number: Int) {
integer_number_2.setText("$number")
}

Reset integers in a TextView to 0 (Kotlin)

So I got the increment and decrement buttons work, but I need to reset the second set to 0 using the clear button. If you push it, it'll show you a 0 as a string value, but it won't reset the output. For example if I we're to increment the integer values up to 6 and then push the clear button, it'll show 0 in the TextView but once I push the increment button again it'll go straight up to 7 and not 1.
What am I missing?
KT
package com.example.plus_and_minus_input
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
increase_1.setOnClickListener { increaseInteger1() }
decrease_1.setOnClickListener { decreaseInteger1()
}
increase_2.setOnClickListener { increaseInteger2() }
decrease_2.setOnClickListener { decreaseInteger2() }
clear_button.setOnClickListener {
integer_number_2.text ="0"
}
}
private var integer1 = 0
private var integer2 = 0
fun increaseInteger1() {
//display_number_1(integer_number_1.text.toString().toInt() + 1)
integer1 = (integer1 + 1).coerceAtMost(10)
display_number_1(integer1)
}
fun decreaseInteger1() {
//display_number_1(integer_number_1.text.toString().toInt() - 1)
integer1 = (integer1 - 1).coerceAtLeast(0)
display_number_1(integer1)
}
fun increaseInteger2() {
//display_number_2(integer_number_2.text.toString().toInt() + 1)
integer2 = (integer2 + 1).coerceAtMost(10)
display_number_2(integer2)
}
fun decreaseInteger2() {
//display_number_2(integer_number_2.text.toString().toInt() - 1)
integer2 = (integer2 - 1).coerceAtLeast(0)
display_number_2(integer2)
}
private fun display_number_1(number: Int) {
integer_number_1.setText("$number")
}
private fun display_number_2(number: Int) {
integer_number_2.setText("$number")
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="309dp"
android:layout_height="118dp"
android:layout_marginTop="56dp"
android:layout_marginBottom="549dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/decrease_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="#+id/integer_number_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="16dp"
android:inputType="number"
android:text="0"
android:textSize="70sp"
android:textStyle="bold" />
<Button
android:id="#+id/increase_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
<Button
android:id="#+id/decrease_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/integer_number_2"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.317" />
<Button
android:id="#+id/increase_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.579"
app:layout_constraintStart_toEndOf="#+id/integer_number_2"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.313" />
<TextView
android:id="#+id/integer_number_2"
android:layout_width="46dp"
android:layout_height="99dp"
android:layout_marginStart="46dp"
android:layout_marginLeft="46dp"
android:inputType="number"
android:text="0"
android:textSize="70sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.426"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.297" />
<Button
android:id="#+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/integer_number_2" />
</androidx.constraintlayout.widget.ConstraintLayout>
You need to reset the value of the global int var also:
clear_button.setOnClickListener {
integer_number_2.text ="0"
integer2 = 0
}
It's bad practise to name your variables with underscores in kotlin/java. Use camel case instead tvIntegerNumberTwo and probably assign it the initials of the xml element type to better recognise what it is.

How do I get a button click to change two separate TextView fields in my app in Android Studio?

Here is my code. I figured out how to make one text field change when the button is clicked, but the minute I try to add another val like this:
val tv = findViewById(R.id.questionfield) as TextView
tv.text = "What is your name?"
I get an error that says:
"Conflicting declarations: val tv: Textview, val tv: TextView"
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="Avatar Response Field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/welcome" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:text="Question Pop Up Field"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:id="#+id/questionfield"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.497"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"/>
<Button
android:text="Click to Ask Alyssa a Question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/textView2"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:onClick="buttonClick"/>
<ImageView
android:layout_width="291dp"
android:layout_height="261dp" tools:src="#tools:sample/avatars"
android:id="#+id/imageView2" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/welcome"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/textView2"/>
</android.support.constraint.ConstraintLayout>
MainActivity.kt
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.TextView
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun buttonClick(v: View) {
val tv = findViewById(R.id.welcome) as TextView
tv.text = "Hi! I'm Alyssa"
val tv = findViewById(R.id.questionfield) as TextView
tv.text = "What is your name?"
}
}
The variable name tv is already used in the same scope. You can use a different variable name and try again. For example:
val tv = findViewById(R.id.welcome) as TextView
tv.text = "Hi! I'm Alyssa"
val anotherName = findViewById(R.id.questionfield) as TextView
anotherName.text = "What is your name?"

Categories

Resources