There is no change to the second activity element when intending - android

I made a simple application using Kotlin. I want to change pages. I create a new Activity and call it from the MainActivity. When I run AVD, then switch pages, the page displayed is empty. There are no elements at all like new Activities.
And this is the intent code for the second Activity:
class MainActivity : AppCompatActivity() {
already_have_account_text_view.setOnClickListener {
Log.d("MainActivity", "Try to login activity")
//launch the login activity
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}
LoginActivity code:
package com.example.kotlinmessanger
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
class LoginActivity: AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
setContentView(R.layout.activity_login)
}
}
And this is the xml of the second Activity:
<?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"
android:padding="20dp"
android:background="#8BC34A"
tools:context=".LoginActivity">
<EditText
android:id="#+id/email_edittext_login"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:background="#FFFFFF"
android:ems="10"
android:hint="E-mail"
android:inputType="textEmailAddress"
android:paddingLeft="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/password_edittext_login"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:background="#FFFFFF"
android:ems="10"
android:paddingLeft="16dp"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="#+id/email_edittext_login"
app:layout_constraintStart_toStartOf="#+id/email_edittext_login"
app:layout_constraintTop_toBottomOf="#+id/email_edittext_login" />
<Button
android:id="#+id/login_button_login"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="#+id/password_edittext_login"
app:layout_constraintStart_toStartOf="#+id/password_edittext_login"
app:layout_constraintTop_toBottomOf="#+id/password_edittext_login" />
</androidx.constraintlayout.widget.ConstraintLayout>

Please change width of android:layout_width="0dp" 0dp to Any specific width of all the EditText and Button and add some proper width to them

Related

My button on clicked is not working in Kotlin Android Studio

I am facing a problem which is my button ID is well-configured and in my main activity, I am calling the button using the correct ID too, but after I run the project on the emulator, the button is not working after I clicked on it for multiple times.
Can anyone help me to solve the problem?
This is my xml file:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bgColor"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="20dp"
android:text="Mobile Quiz Application"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:cardCornerRadius="10dp"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Welcome To The Quiz App!"
android:textColor="#color/txtTitle"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Please enter your name before starting the quiz."
android:textColor="#color/txtDesc"
android:textSize="15sp" />
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your name......"
android:inputType="textCapWords"
android:textColor="#color/txtTitle"
android:textColorHint="#color/txtDesc" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/btn_Start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Continue"
android:textStyle="bold">
</Button>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
This is my main file:
package com.example.quizapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnStart = findViewById<Button>(R.id.btn_Start)
val nameInput = findViewById<EditText>(R.id.etName)
btnStart.setOnClickListener {
Toast.makeText(this#MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}
}
}
findViewById is an old method.
i would recommend you to use with ViewBinding.
click here to documentation
You can use viewBinding
add line to build.gradle
buildFeatures{
viewBinding true
}
and update your activity like this
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnStart.setOnClickListener{
Toast.makeText(this#MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}
}

signInWithEmailAndPassword Firebase, Android Studio, kotlin not working properly

I am attempting to use Firebase to store my emails and passwords. On my Sign-Up page, maybe 5% of the time, the code will run smoothly with no problems. However, when it does run, I have to wait a decent amount of time before it will register the new user within Firebase (about 3-5 minutes). All of my Toast messages go through except for the "Sign up success" and "it.exception.toString()" because it rarely completes the process. I assume the problem lies within the "firebaseAuth.createUserWithEmailAndPassword..." block, but I am not able to fix the problem.
xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:scrollbars="none"
android:background="#color/grey"
tools:context=".LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="22dp"
android:layout_marginEnd="22dp">
<ImageView
android:id="#+id/blueCFSLogo"
android:layout_marginTop="32dp"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01"
app:srcCompat="#drawable/blue_cfs_logo" />
<TextView
android:id="#+id/signUpTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:fontFamily="#font/lobster_regular"
android:letterSpacing=".05"
android:text="Sign Up"
android:textColor="#color/orange"
android:textSize="34sp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/blueCFSLogo"
app:layout_constraintVertical_bias="0.514" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/emailLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColorHint="#color/grey"
app:boxBackgroundColor="#color/white"
app:boxCornerRadiusBottomEnd="10dp"
app:boxCornerRadiusBottomStart="10dp"
app:boxCornerRadiusTopEnd="10dp"
app:boxCornerRadiusTopStart="10dp"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" >
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/nunito_semibold"
android:inputType="textEmailAddress"
android:singleLine="true"
android:hint="#string/email"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/edtPasswordPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColorHint="#color/grey"
app:boxBackgroundColor="#color/white"
app:boxCornerRadiusBottomEnd="10dp"
app:boxCornerRadiusBottomStart="10dp"
app:boxCornerRadiusTopEnd="10dp"
app:boxCornerRadiusTopStart="10dp"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/passwordEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/nunito_semibold"
android:hint="#string/password"
android:inputType="textPassword"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/edtConfirmPasswordTil"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColorHint="#color/grey"
app:boxBackgroundColor="#color/white"
app:boxCornerRadiusBottomEnd="10dp"
app:boxCornerRadiusBottomStart="10dp"
app:boxCornerRadiusTopEnd="10dp"
app:boxCornerRadiusTopStart="10dp"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:endIconMode="password_toggle">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/confirmPasswordEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/nunito_semibold"
android:hint="Confirm Password"
android:inputType="textPassword"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/signUpBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
app:cornerRadius="20dp"
app:backgroundTint="#color/blue"
android:fontFamily="#font/nunito_bold"
android:textSize="16sp"
android:text="Sign Up"
android:textColor="#color/grey" >
</androidx.appcompat.widget.AppCompatButton>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center">
<TextView
android:id="#+id/alreadyHaveAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/nunito_semibold"
android:letterSpacing=".05"
android:text="Already have an account?"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="13sp" />
<TextView
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:fontFamily="#font/nunito_bolditalic"
android:letterSpacing=".05"
android:text="Login"
android:textColor="#color/orange"
android:textSize="13sp"
android:textAlignment="center"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
.kt file:
package com.countdownfantasysports.app
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import com.countdownfantasysports.app.databinding.ActivitySignUpBinding
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import kotlinx.android.synthetic.main.activity_sign_up.*
class SignUpActivity : AppCompatActivity() {
private lateinit var binding: ActivitySignUpBinding
private lateinit var firebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySignUpBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.login.setOnClickListener {
val loginIntent = Intent(this, LoginActivity::class.java)
startActivity(loginIntent)
}
firebaseAuth = FirebaseAuth.getInstance()
binding.signUpBtn.setOnClickListener{
val email = binding.emailEditText.text.toString()
val password = binding.passwordEt.text.toString()
val confirmPassword = binding.confirmPasswordEt.text.toString()
if (email.isNotEmpty() && password.isNotEmpty() && confirmPassword.isNotEmpty()) {
if (password == confirmPassword){
firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener{
if (it.isSuccessful) {
// If successful, start MainActivity
val mainIntent = Intent(this, MainActivity::class.java)
startActivity(mainIntent)
// If successful, display message for user
Toast.makeText(
this,
"Sign up success",
Toast.LENGTH_SHORT).show()
// If successful, log success
Log.d("Isaac", "createUserWithEmail:success")
} else {
// If failure, display message for user
Toast.makeText(
this,
it.exception.toString(),
Toast.LENGTH_SHORT).show()
// If failure, log failure
Log.w("Isaac", "createUserWithEmail:failure")
}
}
} else {
// If passwords do not match, display message to user
Toast.makeText(
this,
"Password does not match",
Toast.LENGTH_SHORT).show()
}
} else {
// If empty fields, display message for user
Toast.makeText(
this,
"Empty fields are not allowed",
Toast.LENGTH_SHORT).show()
}
}
// Start LoginActivity
binding.login.setOnClickListener {
val loginIntent = Intent(this, LoginActivity::class.java)
startActivity(loginIntent)
}
}
}```
I have tried mulitple builds, but this seems to be the only on that completes 5% of the time. Help would be appreciated. Thanks!

XML shows a white screen

I'm trying to do a login and create account pages for my app in Kotlin. The problem is that I added the create account page on AndroidManifest to be the first page when you open the app and it only shows a white screen. I noticed that in Android Studio there's a Design tab and the page shows exactly like I want it. Can you help me?
This is my 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_background">
<TextView
android:id="#+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create_account"
android:textSize="30sp"
android:textColor="#color/white"
android:gravity="center"
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="parent"
app:layout_constraintVertical_bias="0.132" />
<androidx.cardview.widget.CardView
android:id="#+id/myCardView"
android:layout_width="393dp"
android:layout_height="451dp"
android:layout_gravity="center"
android:layout_margin="4dp"
app:cardBackgroundColor="#color/cardsColor"
app:cardCornerRadius="12dp"
app:cardElevation="0dp"
app:cardUseCompatPadding="true"
app:contentPadding="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="361dp"
android:layout_height="426dp"
android:orientation="vertical"
android:padding="16dp"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:weightSum="5.5">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="10dp"
android:background="#drawable/edit_text_stroke"
android:gravity="center"
android:hint="#string/email"
android:minLines="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.213"
android:importantForAutofill="no" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="10dp"
android:background="#drawable/edit_text_stroke"
android:gravity="center"
android:hint="#string/password"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3"
android:importantForAutofill="no" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<Button
android:id="#+id/register_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/button_round_corners"
android:gravity="center"
android:padding="12dp"
android:text="#string/create_account"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.164"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/myCardView"
app:layout_constraintVertical_bias="0.535" />
<Button
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_round_corners"
android:padding="12dp"
android:text="#string/login"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.81"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/myCardView"
app:layout_constraintVertical_bias="0.535" />
</androidx.constraintlayout.widget.ConstraintLayout>
and my kotlin class
package projeto.a43796.dashboard
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.text.TextUtils
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import kotlinx.android.synthetic.main.activity_create_account.*
class CreateAccount : AppCompatActivity() {
lateinit var registerButton: Button
lateinit var email: EditText
lateinit var password: EditText
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
setContentView(R.layout.activity_create_account)
registerButton = findViewById(R.id.register_button)
email = findViewById(R.id.email)
password = findViewById(R.id.password)
registerButton.setOnClickListener(){
when{
TextUtils.isEmpty(email.text.toString().trim{it <= ' '}) -> {
Toast.makeText(
this#CreateAccount,
"Please enter your e-mail",
Toast.LENGTH_SHORT
).show()
}
TextUtils.isEmpty(password.text.toString().trim{it <= ' '}) -> {
Toast.makeText(
this#CreateAccount,
"Please enter your password",
Toast.LENGTH_SHORT
).show()
}
else -> {
val email: String = email.text.toString().trim{it <= ' '}
val passord: String = password.text.toString().trim{it <= ' '}
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, passord)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val firebaseUser: FirebaseUser = task.result!!.user!!
Toast.makeText(
this#CreateAccount,
"Account Created",
Toast.LENGTH_SHORT
).show()
val intent =
Intent(this#CreateAccount, Dashboard::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
intent.putExtra("user_id", firebaseUser.uid)
intent.putExtra("email_user", email)
startActivity(intent)
finish()
}
else{
Toast.makeText(
this#CreateAccount,
task.exception!!.message.toString(),
Toast.LENGTH_SHORT
).show()
}
}
}
}
}
}
}

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")
}

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

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()
}

Categories

Resources