I have a progressbar in my layout in Android and i want to change its visibility inside the viewmodel. But application only gets the first state of progressbar. Whenever i try to change it nothing happen. Here is my layout file ;
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<data>
<import type="android.view.View"></import>
<variable
name="viewmodel"
type="com.tolgahantutar.bexworkfloww.ui.auth.AuthViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical"
android:padding="24dp"
android:paddingTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="154dp"
android:layout_height="154dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="48dp"
android:src="#drawable/bexfalogo"
android:contentDescription="#string/bexlogo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="132dp"
android:textAllCaps="true"
android:textSize="16dp" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="#string/hint_username">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edit_text_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#={viewmodel.userName}"
>
</com.google.android.material.textfield.TextInputEditText>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/password_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="#string/hint_password"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edit_text_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:text="#={viewmodel.password}"
/>
</com.google.android.material.textfield.TextInputLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.button.MaterialButton
android:id="#+id/button_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="#string/button_login"
android:onClick="#{viewmodel::onClickUserLogin}"
/>
<com.google.android.material.button.MaterialButton
android:id="#+id/button_cancel"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:layout_toStartOf="#id/button_login"
android:layout_toLeftOf="#id/button_login"
android:text="#string/button_cancel" />
</RelativeLayout>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="#{viewmodel.isLoading ? View.VISIBLE : View.GONE}" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
and my viewmodel looks like this
package com.tolgahantutar.bexworkfloww.ui.auth
import android.content.Intent
import android.view.View
import android.widget.Toast
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.ObservableBoolean
import androidx.databinding.library.baseAdapters.BR
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tolgahantutar.bexworkfloww.data.network.repositories.AuthorizeSessionRepository
import com.tolgahantutar.bexworkfloww.ui.home.HomeActivity
import com.tolgahantutar.bexworkfloww.util.ApiException
import com.tolgahantutar.bexworkfloww.util.NoInternetException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.internal.notify
class AuthViewModel (
private val repository: AuthorizeSessionRepository
):ViewModel() {
var userName: String?=null
var password: String?=null
val isLoading = MutableLiveData<Boolean>(true)
fun onClickUserLogin(view: View){
isLoading.value = false
val sessionID = 0
val authorityID = 0
val loginType = "System"
viewModelScope.launch {
if(!(userName==null||password==null)){
try{
val authResponse = userLogin(sessionID,authorityID,userName!!,password!!,loginType)
if(authResponse.Result){
isLoading.value=false
Toast.makeText(view.context, "Login Successfull", Toast.LENGTH_LONG).show()
val intent = Intent(view.context,HomeActivity::class.java)
view.context.startActivity(intent)
}else{
//isLoading.value=false
Toast.makeText(view.context, "Login Failed!!", Toast.LENGTH_LONG).show()
}
}catch (e: ApiException){
e.printStackTrace()
}catch (e: NoInternetException){
e.printStackTrace()
}}
else{
Toast.makeText(view.context, "Kullanıcı adı ve şifre boş bırakılamaz!!", Toast.LENGTH_SHORT).show()
}
}
}
suspend fun userLogin(
SessionID : Int,
AuthorityID: Int,
UserName: String,
Password : String,
LoginType: String
)= withContext(Dispatchers.IO){repository.userLogin(SessionID, AuthorityID, UserName, Password, LoginType)}
}
As i mentioned it is just get the first state but when i click the button nothing changes..
Related
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()
}
}
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!
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()
}
}
}
}
}
}
}
I am new to programming and want to ask for help because I am stuck on this task.
Clicking on the RecyclerView gets this error.
Below I will attach my code and hope for your help and solutions to the problem. Thank you
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.skreep.worko, PID: 24044
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skreep.worko/com.skreep.worko.DetailActivity}: java.lang.NullPointerException: workout_title must not be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7560)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.NullPointerException: workout_title must not be null
at com.skreep.worko.DetailActivity.onCreate(DetailActivity.kt:32)
at android.app.Activity.performCreate(Activity.java:7894)
at android.app.Activity.performCreate(Activity.java:7881)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3283)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7560)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Workout Adapter
package com.skreep.worko.adapter
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.skreep.worko.DetailActivity
import com.skreep.worko.R
import com.skreep.worko.databinding.ItemWorkoutBinding
import com.skreep.worko.model.WorkoutData
class WorkoutAdapter(var c:Context,var workoutList:ArrayList<WorkoutData>
):RecyclerView.Adapter<WorkoutAdapter.WorkoutViewHolder>()
{
inner class WorkoutViewHolder(var v:ItemWorkoutBinding): RecyclerView.ViewHolder(v.root){}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WorkoutViewHolder {
val inflter = LayoutInflater.from(parent.context)
val v = DataBindingUtil.inflate<ItemWorkoutBinding>(
inflter, R.layout.item_workout,parent,
false)
return WorkoutViewHolder(v)
}
override fun onBindViewHolder(holder: WorkoutViewHolder, position: Int) {
val newList = workoutList[position]
holder.v.isWorkouts = workoutList[position]
holder.v.root.setOnClickListener {
val name = newList.name
val description = newList.description
val fullTime = newList.fullTime
val workoutDesc = newList.workoutDesc
val workoutName = newList.workoutName
val workoutTime = newList.workoutTime
val mIntent = Intent(c,DetailActivity::class.java)
mIntent.putExtra("description",description)
mIntent.putExtra("name",name)
mIntent.putExtra("fullTime",fullTime)
mIntent.putExtra("workoutDesc",workoutDesc)
mIntent.putExtra("workoutName",workoutName)
mIntent.putExtra("workoutTime",workoutTime)
c.startActivity(mIntent)
}
}
override fun getItemCount(): Int {
return workoutList.size
}
}
Detail Activity
package com.skreep.worko
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.database.DatabaseReference
import com.skreep.worko.adapter.WorkoutAdapter
import com.skreep.worko.model.WorkoutData
import kotlinx.android.synthetic.main.activity_detail.*
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.item_workout.*
import kotlinx.android.synthetic.main.item_workout_detail.*
class DetailActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val workoutIntent = intent
val name = workoutIntent.getStringExtra("name")
val description = workoutIntent.getStringExtra("description")
val workoutName = workoutIntent.getStringExtra("workoutName")
detail_name.text = name
detail_desc.text = description
workout_title.text = workoutName
}
}
Home Activity
package com.skreep.worko
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.database.*
import com.skreep.worko.adapter.WorkoutAdapter
import com.skreep.worko.model.WorkoutData
import kotlinx.android.synthetic.main.activity_detail.*
import kotlinx.android.synthetic.main.activity_home.*
class HomeActivity : BaseActivity() {
lateinit var mDataBase:DatabaseReference
private lateinit var workoutList:ArrayList<WorkoutData>
private lateinit var mAdapter:WorkoutAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
workoutList = ArrayList()
mAdapter = WorkoutAdapter(this, workoutList)
recyclerworkoutList.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerworkoutList.setHasFixedSize(true)
recyclerworkoutList.adapter = mAdapter
getWorkoutData()
}
private fun getWorkoutData() {
mDataBase = FirebaseDatabase.getInstance().getReference("Workout")
mDataBase.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (userSnapshot in snapshot.children) {
val workout = userSnapshot.getValue(WorkoutData::class.java)
workoutList.add(workout!!)
}
recyclerworkoutList.adapter = mAdapter
}
}
override fun onCancelled(error: DatabaseError) {
Toast.makeText(this#HomeActivity,
error.message, Toast.LENGTH_SHORT).show()
}
})
}
}
XML Activity Detail
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isData"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
tools:context=".DetailActivity">
<ImageView
android:id="#+id/back_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="20dp"
android:padding="20dp"
android:src="#drawable/ic_back">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:fontFamily="#font/poppins"
android:padding="20dp"
android:text="#string/app_name"
android:textSize="20sp"></TextView>
<TextView
android:id="#+id/detail_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="120dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isData.name}"
android:textColor="#color/darkblue"
android:textSize="26sp"></TextView>
<TextView
android:id="#+id/detail_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/detail_name"
android:layout_alignLeft="#+id/detail_name"
android:fontFamily="#font/poppins"
android:text="#{isData.workoutDesc}"
android:textColor="#color/lightgray">
</TextView>
<TextView
android:id="#+id/tv_workout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="310dp"
android:fontFamily="#font/poppins_bold"
android:gravity="center"
android:text="Упражнения"
android:textColor="#color/darkblue"
android:textSize="20sp"></TextView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/workoutListDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_workout"
android:orientation="vertical"
android:padding="10dp"
tools:itemCount="1"
/>
</RelativeLayout>
</layout>
Xml activity Home
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isWorkouts"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/darkblue"
android:orientation="vertical"
tools:context=".HomeActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:fontFamily="#font/poppins"
android:gravity="center"
android:padding="20dp"
android:text="#string/app_name"
android:textColor="#color/white"
android:textSize="20sp" />
<TextView
android:id="#+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="#string/welcome"
android:textColor="#color/white"
android:textSize="30sp"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/welcome_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="#string/welcome_description"
android:textColor="#color/desc_color"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="#id/welcome" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerworkoutList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
android:paddingTop="40dp"
tools:itemCount="1"
tools:listitem="#layout/item_workout" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</layout>
item_workout xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isWorkouts"
type="com.skreep.worko.model.WorkoutData" />
</data>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".SplashActivity">
<androidx.cardview.widget.CardView
android:layout_width="250dp"
android:layout_height="350dp"
android:layout_margin="10dp"
android:backgroundTint="#color/gray"
app:cardCornerRadius="10dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/watch_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="#drawable/ic_watch"
app:tint="#color/darkblue"></ImageView>
<TextView
android:id="#+id/tvfulltime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="26dp"
android:fontFamily="#font/poppins"
android:text="#{isWorkouts.fullTime}"
android:textSize="10sp"></TextView>
<TextView
android:id="#+id/tvtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="27dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isWorkouts.name}"
android:textColor="#color/darkblue"
android:textSize="24sp">
</TextView>
<TextView
android:id="#+id/tvdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvtitle"
android:layout_marginStart="10dp"
android:padding="20dp"
android:text="#{isWorkouts.description}"
android:textColor="#color/lightgray"
android:textSize="12sp"></TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:src="#drawable/ic_next"
>
</ImageView>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</layout>
item workout detail xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="isWorkout"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".SplashActivity">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:backgroundTint="#color/gray">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/workout_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isWorkout.workoutName}"
android:textColor="#color/darkblue"
android:textSize="24sp"></TextView>
<TextView
android:id="#+id/workout_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/workout_title"
android:layout_alignLeft="#+id/workout_title"
android:fontFamily="#font/poppins"
android:text="#{isWorkout.workoutName}"
android:textColor="#color/lightgray"
android:textSize="12sp"></TextView>
<TextView
android:id="#+id/workout_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/workout_title"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:text="#{isWorkout.workoutTime}"
android:textColor="#color/darkblue"
android:textSize="12sp">
</TextView>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
</layout>
Check your activity_detail XML file is missing a Textview called workout_title
You have it in a different XML layout file workout_detail xml
hope you guys are all well.
Newbie to Android development. I am trying out RecyclerViews but it is not getting rendered at all. Is there a certain step I am missing?
Here is my mainActivity kotlin file :
package com.example.alphablog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.alphablog.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rv = findViewById<RecyclerView>(R.id.recycler_view_main)
rv.layoutManager = LinearLayoutManager(this)
rv.adapter = PostAdapter()
}
}
Here is my main activity 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">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:i`enter code here`temCount="30"
tools:listitem="#layout/recycler_view_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my recycler view 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="wrap_content">
<ImageView
android:id="#+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_baseline_add_business_24"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title of Blog Post"
android:textSize="20sp"
android:textColor="#android:color/black"
app:layout_constraintTop_toTopOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/textView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="285dp"
android:layout_height="20dp"
android:text="Body of first blog post"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.050" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit and Delete action"
app:layout_constraintTop_toTopOf="#+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
And finally, here is my adapter class boiler plate:
package com.example.alphablog
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
class PostAdapter : RecyclerView.Adapter<PostAdapter.PostViewHolder>() {
class PostViewHolder(postView: View) : RecyclerView.ViewHolder(postView){
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder { // creates view to display
val postView = LayoutInflater.from(parent.context).inflate(R.layout.post, parent,false)
return PostViewHolder(postView)
}
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
}
override fun getItemCount(): Int { // amount of items you will be working with in your recycler view
return 30
}
}