I am building my first app and i am trying to change activities using buttons.The first button works fine. But the second crashes the app giving this error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
I tried researching, but i didn't find a working solution. So please help me out here
login.kt
package com.example.login
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class login : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val loginbutton = findViewById<Button>(R.id.signinbtn)
loginbutton.setOnClickListener {
val intent = Intent(this,gamefeed::class.java)
startActivity(intent)
finish()
}
val signupbutton = findViewById<Button>(R.id.signupbtn)
signupbutton.setOnClickListener {
val intent = Intent(this,Registration::class.java)
startActivity(intent)
finish()
}
}
}
activity_login.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:id="#+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#drawable/ic_spalsh"
tools:ignore="InvalidId">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="392dp"
android:layout_height="217dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView2"
android:layout_width="399dp"
android:layout_height="228dp"
android:layout_marginBottom="32dp"
android:contentDescription="#string/todo"
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.285"
app:srcCompat="#drawable/login1" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="#string/Join_with_us"
android:textColor="#color/black"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout"
tools:ignore="TextContrastCheck" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:background="#drawable/background_edittext"
android:elevation="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:ems="10"
android:hint="#string/username"
android:importantForAutofill="no"
android:inputType="textPersonName|textEmailAddress"
android:padding="24dp"
tools:ignore="TextContrastCheck"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF"
/>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editTextTextPassword"
android:layout_width="match_parent"
android:layout_height="71dp"
android:ems="10"
android:hint="#string/password"
android:importantForAutofill="no"
android:inputType="textPassword"
android:padding="24dp" />
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/forgot_password"
android:textColor="#color/purple_700"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
tools:ignore="TextContrastCheck" />
<Button
android:id="#+id/signupbtn"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:backgroundTint="#color/white"
android:text="#string/Dont_have_account"
android:textColor="#color/black"
android:textSize="14sp"
android:visibility="visible"
app:cornerRadius="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/signinbtn"
tools:visibility="visible" />
<Button
android:id="#+id/signinbtn"
android:layout_width="176dp"
android:layout_height="68dp"
android:layout_marginTop="24dp"
android:background="#drawable/background_btn"
android:backgroundTint="#color/bottom_nav_background"
android:padding="15dp"
android:text="#string/login"
android:textColor="#color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4"
app:layout_constraintWidth_percent="0.6"
tools:ignore="DuplicateSpeakableTextCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
Registration.kt
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class Registration : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_registration)
val signinbutton2 = findViewById<Button>(R.id.loginbtn)
signinbutton2.setOnClickListener {
val intent = Intent(this,login::class.java)
startActivity(intent)
finish()
}
}
}
activity_registration.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:id="#+id/activity_registration"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Registration"
android:background="#drawable/ic_spalsh">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout3"
android:layout_width="393dp"
android:layout_height="213dp"
android:background="#drawable/registration1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.40"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/Register_Here"
android:textColor="#color/black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout3"
tools:ignore="TextContrastCheck" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="356dp"
android:layout_height="354dp"
android:layout_marginStart="30dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="30dp"
android:background="#drawable/background_edittext"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.263"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:ems="10"
android:hint="#string/email"
android:importantForAutofill="no"
android:inputType="textPersonName|textEmailAddress"
android:padding="10dp"
tools:ignore="TouchTargetSizeCheck,TextContrastCheck" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:ems="10"
android:hint="#string/username"
android:inputType="textPersonName"
android:padding="10dp"
tools:ignore="TouchTargetSizeCheck,TextContrastCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="#+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:ems="10"
android:hint="#string/age"
android:inputType="textPersonName"
android:padding="10dp"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<TextView
android:id="#+id/gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#null"
android:text="#string/gender"
android:textColor="#AEA8BC"
android:textColorHighlight="#color/purple_700"
android:textSize="20sp"
tools:ignore="TextContrastCheck" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="44dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/malerb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/male"
tools:ignore="TouchTargetSizeCheck" />
<RadioButton
android:id="#+id/femalerb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:hint="#string/female"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck" />
<RadioButton
android:id="#+id/otherrb"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_weight="1"
android:hint="#string/other"
tools:ignore="TouchTargetSizeCheck" />
</RadioGroup>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/password1"
android:inputType="textPassword"
android:padding="10dp"
tools:ignore="TouchTargetSizeCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="#+id/confirmpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/confirm_password"
android:inputType="textPassword"
android:padding="10dp"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<Button
android:id="#+id/regbtn"
android:layout_width="131dp"
android:layout_height="wrap_content"
android:layout_marginStart="105dp"
android:background="#drawable/background_btn"
android:textColor="#color/white"
android:text="#string/register"
tools:ignore="DuplicateSpeakableTextCheck" />
</LinearLayout>
<Button
android:id="#+id/signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="123dp"
android:background="#00FFFFFF"
android:text="#string/Allready_have_account"
android:textColor="#color/black"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout2"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login/com.example.login.Registration}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.login.Registration.onCreate(Registration.kt:15)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
You get an error because there is no loginbtn on the activity_registration.xml page
class Registration : AppCompatActivity() {
private lateinit var regbtn:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_registration)
regbtn=findViewById<Button>(R.id.regbtn)
signinbutton2.setOnClickListener {
val intent = Intent(this,login::class.java)
startActivity(intent)
finish()
}
}
I recommend you to look at the binding structure
build.gradle(module)
buildFeatures {
viewBinding true
dataBinding true
}
Registration.kt
class Registration : AppCompatActivity() {
private lateinit var binding: RegistrationBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAdminBinding.inflate(layoutInflater)
val view=binding.root
setContentView(view)
binding.signinbutton2.setOnClickListener {
val intent = Intent(this,login::class.java)
startActivity(intent)
finish()
}
}
}
Related
I am creating one learning application in which one activity suddenly crashed. everything I did for it, I install application again but not get succussed. Please suggest me any solutions
In this is activity I join variable activity (Variable is name of activity)
package com.example.myapplication.java
import android.annotation.SuppressLint
import android.app.ActivityOptions
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import com.example.myapplication.R
import com.example.myapplication.java.Variable
class Java_Basic : AppCompatActivity() {
#SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_java_basic)
val back: ImageView = findViewById(R.id.back)
val variable: Button = findViewById(R.id.variable_btn)
back.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
variable.setOnClickListener{
intent = Intent(this, Variable::class.java)
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle())
}
}
}
This is Variable activity
package com.example.myapplication.java
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import com.example.myapplication.R
class Variable : AppCompatActivity() {
#SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_variable)
val back: ImageView = findViewById(R.id.back2)
back.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
}
}
this is Variable.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"
android:background="#drawable/bg_main" >
<ImageView
android:id="#+id/back2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="16dp"
android:layout_marginTop="40dp"
android:src="#drawable/ic_baseline_arrow_back_242"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView"
android:layout_width="49dp"
android:layout_height="49dp"
android:layout_marginStart="84dp"
android:layout_marginTop="28dp"
android:src="#drawable/javalogo"
app:layout_constraintStart_toEndOf="#+id/back2"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/java"
android:layout_width="79dp"
android:layout_height="41dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="104dp"
android:fontFamily="#font/el_messiri_medium"
android:gravity="center"
android:text="Java"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="35sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="88dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:layout_margin="10sp"
app:cardBackgroundColor="#36393F"
app:cardCornerRadius="10dp"
app:cardElevation="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10sp"
android:layout_marginTop="10sp"
android:fontFamily="#font/el_messiri_medium"
android:text="#string/variable"
android:layout_gravity="center_horizontal"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/intro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_marginBottom="10sp"
android:autoLink="web"
android:fontFamily="#font/alata"
android:lineHeight="27dp"
android:justificationMode="inter_word"
tools:ignore="UnusedAttribute"
android:paddingHorizontal="10dp"
android:text="#string/variable_intro"
android:textColor="#color/white"
android:textColorLink="#9997CB"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:layout_margin="10sp"
app:cardBackgroundColor="#36393F"
app:cardCornerRadius="10dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:fontFamily="#font/el_messiri_medium"
android:text="#string/local_var"
android:layout_gravity="center_horizontal"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10sp"
android:autoLink="web"
android:fontFamily="#font/alata"
android:lineHeight="27dp"
android:justificationMode="inter_word"
tools:ignore="UnusedAttribute"
android:paddingHorizontal="10dp"
android:text="#string/localvar_explain"
android:textColor="#color/white"
android:textColorLink="#9997CB"
android:textSize="16sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:layout_margin="10sp"
app:cardBackgroundColor="#36393F"
app:cardCornerRadius="10dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:layout_marginBottom="10sp"
android:autoLink="web"
android:fontFamily="#font/alata"
android:lineHeight="27sp"
android:paddingHorizontal="10dp"
android:text="#string/java_sample"
android:textColor="#color/white"
android:textColorLink="#9997CB"
android:textSize="16sp"
tools:ignore="UnusedAttribute" />
<androidx.cardview.widget.CardView
android:layout_width="335sp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:layout_margin="15sp"
app:cardBackgroundColor="#FFF"
app:cardElevation="8dp">
<TextView
android:id="#+id/code1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:layout_marginBottom="10sp"
android:textSize="13sp"
android:fontFamily="serif-monospace"
android:lineHeight="27sp"
android:paddingHorizontal="10dp"
android:text="#string/localvar_code"
android:textStyle="bold"
android:textColor="#color/black"
android:textColorLink="#9997CB"
android:textIsSelectable="true"
tools:ignore="UnusedAttribute" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
I am trying to create a logout button on the Sliding Root Navigation which I found here
This is my main activity
class HomeScreenActivity : AppCompatActivity() {
private lateinit var binding: ActivityHomeScreenBinding
private lateinit var leftDrawerBinding: MenuLeftDrawerBinding
private lateinit var homeScreenViewModel: HomeScreenViewModel
private var slidingRootNav: SlidingRootNav? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHomeScreenBinding.inflate(layoutInflater)
leftDrawerBinding = MenuLeftDrawerBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.slToolbar)
title = ""
slidingRootSetup(savedInstanceState)
setUpBottomNavBar()
homeScreenViewModel = ViewModelProvider(this)[HomeScreenViewModel::class.java]
replaceFragment(InGymFragment())
// leftDrawerBinding.logoutFab.setOnClickListener(listener)
}
// Side Panel
private fun slidingRootSetup(savedInstanceState: Bundle?) {
slidingRootNav = SlidingRootNavBuilder(this)
.withToolbarMenuToggle(binding.slToolbar)
.withMenuOpened(false)
.withGravity(SlideGravity.LEFT)
.withContentClickableWhenMenuOpened(true)
.withSavedState(savedInstanceState)
.withMenuLayout(R.layout.menu_left_drawer)
.inject()
}
private val listener = View.OnClickListener {
when (it.id) {
leftDrawerBinding.logoutFab.id -> logout()
}
}
private fun logout() {
Toast.makeText(this, "logout clicked!", Toast.LENGTH_LONG).show()
// homeScreenViewModel.logoutUser
// startActivity(Intent(this,AuthenticationScreenActivity::class.java))
}
This is the menu left drawer
Screenshot of menu left drawer
<?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:background="#android:color/white"
android:backgroundTint="#color/colorPrimaryDark"
android:orientation="vertical">
<TextView
android:id="#+id/user_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="#string/name"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_name">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/logout_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:backgroundTint="#color/white"
android:focusableInTouchMode="true"
android:text="#string/logout"
tools:viewBindingType="com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton"
android:textColor="#color/black"
app:icon="#android:drawable/ic_lock_power_off"
app:iconTint="#color/black"
app:layout_constraintBottom_toTopOf="#id/app_version"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/app_version"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="#string/app_creator_with_version"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the main activity layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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=".view.ui.HomeScreenActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dark_blue">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_bottom_appbar"
app:elevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/sl_toolbar"
android:layout_width="match_parent"
android:layout_height="86dp"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/id_progress_bar"
android:layout_width="65dp"
android:layout_height="65dp"
android:indeterminateDrawable="#drawable/progress_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/profile"
android:layout_marginStart="8.5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/welcomeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="#string/app_name"
android:textColor="#ECE8E8"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="#+id/profile_image"
app:layout_constraintTop_toTopOf="#+id/profile_image" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/today_s_date"
android:textColor="#FFFFFF"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="#+id/welcomeMessage"
app:layout_constraintTop_toBottomOf="#+id/welcomeMessage" />
<ImageView
android:id="#+id/notificationsImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_notification"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/profile_image"
android:contentDescription="#string/notification" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="#+id/main_frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/bottom_nav_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/appBarLayout" />
<com.ismaeldivita.chipnavigation.ChipNavigationBar
android:id="#+id/bottom_nav_bar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:background="#drawable/rounded_top_bottomnav"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:cnb_menuResource="#menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
I don't know how to add an adapter to this. I tried searching for anyone already done that before but didn't find any clue.
Please See: Adding an android:onClick="logout" with fun logout(view: View) { Toast.makeText(this, "logout clicked via xml!", Toast.LENGTH_LONG).show() } works. But I wish to change the text too how am I suppose to get a hold to it?
You should include some relevant code of what you have tried so far, in order to better understand the question.
Anyway, if I get that correctly you need to add a Button in the menu.xml file, then in your fragment/activity.kt you can set a onClickListener on said button to implement the logic for logging out.
I follower this instruction:
https://stackoverflow.com/a/65903308/10642456
from the answer from "Geek Tanmoy"
but I still have unresolved Reference on most views also when adding binding. before them, it just doesn't do anything.
What could be the problem?
This is an example:
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.skipProfileImage.setOnClickListener {
finish()
}
}
Unresolved reference on skipProfileImage
EDIT:
This is not the ActivityMain but another one and I'll share it because it's the one where the error appears first when I rebuild project:
<?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:background="#303030"
tools:context=".AskProfileImage">
<ImageView
android:id="#+id/imageView97545"
android:layout_width="108dp"
android:layout_height="64dp"
android:layout_marginTop="8dp"
android:background="#00FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="32dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView97545">
<TextView
android:id="#+id/textView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/upload_a_profile_image"
android:textColor="#color/colorAlmostWhite"
android:textSize="22sp"
android:textStyle="bold" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/chooseProfileImage"
android:layout_width="104dp"
android:layout_height="104dp"
android:layout_marginTop="48dp">
<ImageView
android:id="#+id/imageView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_circle"
app:tint="#color/colorWhite" />
<ImageView
android:id="#+id/askProfileImageCam"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="28dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="28dp"
android:layout_marginBottom="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_camera"
app:tint="#color/colorText" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/chooseProfileImageAlternative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:padding="8dp"
android:text="#string/tap_to_choose_an_image"
android:textColor="#color/colorThemeSecond"
android:textSize="16sp" />
<TextView
android:id="#+id/skipProfileImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:padding="8dp"
android:text="#string/Skip"
android:textColor="#color/colorTextBitDarker"
android:textStyle="bold" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Bindings are generated based on the name of the XML layout file you've created for this activity.
I would suggest to double check if it exists & what it's called as your code indicates it should be called activity_main.xml.
Add
<layout>
at the most top place of your layout and
</layout>
at the most bottom place of your layout.
I volunteer with an organization that provides no-cost labor to build wheelchair ramps, wrapiowa.org
One of our volunteers did an app for Apple phones to assist in this activity. I am attempting to duplicate his work for Android using Android Studio but need help. My expertise is in wood designing and construction, not app programming.
Thanks, Ron
Here is what I have, two user inputs for length in inches and 16th’s and height in inches and 16th’s.
Input variables: edittext_number_length, edittext_number16_length, edittext_number_height, edittext_number16_height
The output I want is an angle in degrees and a run length:
Output variables: textView_cut_angle, textView_run
Based on this math:
Math:
length = edittext_number_length + (edittext_number16_length/16)
height = edittext_number_height + (edittext_number16_height/16)
textView_cut_angle = (asin(height/lenght)*180/3.1416)
textView_run = (sqrt(pow(length, 2) - pow(height,2)))
angle.kt
package com.example.wrap_iowarampdesignaid
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class angle : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_angle)
}
fun launchAngleActivity(view: View) {
startActivity(Intent(this#angle,illustration::class.java))
}
fun launchUpdateActivity(view: View) {
}
}
activity_angle.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"
android:background="#android:color/black"
tools:context=".angle">
<ImageView
android:id="#+id/logo"
android:layout_width="218dp"
android:layout_height="100dp"
android:layout_marginTop="16dp"
android:contentDescription="#string/logo"
android:scaleType="centerInside"
android:src="#drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/string2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="#string/angle2"
android:textColor="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logo" />
<Button
android:id="#+id/angle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="launchAngleActivity"
android:text="#string/s_illustration"
android:textAllCaps="false"
android:textColor="#android:color/white"
app:backgroundTint="#2196F3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/update_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:onClick="launchUpdateActivity"
android:text="#string/Update"
android:textAllCaps="false"
android:textColor="#android:color/white"
app:backgroundTint="#2196F3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView18" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="#string/s_length"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/string2" />
<TextView
android:id="#+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="#string/s_heigth"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView7" />
<TextView
android:id="#+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="#string/sc_angle"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/update_button" />
<TextView
android:id="#+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="#string/sc_length"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView19" />
<TextView
android:id="#+id/textView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="#string/degrees"
android:textColor="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView19" />
<TextView
android:id="#+id/textView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="#string/inches"
android:textColor="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView20" />
<EditText
android:id="#+id/edittext_number_length"
android:layout_width="40dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:background="#color/white"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/textView7"
app:layout_constraintEnd_toStartOf="#+id/edittext_number16_length"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<EditText
android:id="#+id/edittext_number16_length"
android:layout_width="25dp"
android:layout_height="0dp"
android:background="#color/white"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/textView7"
app:layout_constraintEnd_toStartOf="#+id/textView_number_length"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<EditText
android:id="#+id/edittext_number_height"
android:layout_width="40dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:background="#color/white"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/textView18"
app:layout_constraintEnd_toStartOf="#+id/edittext_number16_height"
app:layout_constraintTop_toTopOf="#+id/textView18" />
<EditText
android:id="#+id/edittext_number16_height"
android:layout_width="25dp"
android:layout_height="0dp"
android:background="#color/white"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/textView18"
app:layout_constraintEnd_toStartOf="#+id/textView_number16_length"
app:layout_constraintTop_toTopOf="#+id/textView18"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView_number_length"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:background="#color/white"
android:text="#string/_16"
app:layout_constraintBottom_toBottomOf="#+id/textView7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<TextView
android:id="#+id/textView_number16_length"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:background="#color/white"
android:text="#string/_16"
app:layout_constraintBottom_toBottomOf="#+id/textView18"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView18" />
<TextView
android:id="#+id/textView_cut_angle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:background="#color/white"
android:text="0"
app:layout_constraintBottom_toBottomOf="#+id/textView19"
app:layout_constraintEnd_toStartOf="#+id/textView21"
app:layout_constraintTop_toTopOf="#+id/textView19" />
<TextView
android:id="#+id/textView_run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:background="#color/white"
android:text="0"
app:layout_constraintBottom_toBottomOf="#+id/textView20"
app:layout_constraintEnd_toStartOf="#+id/textView22"
app:layout_constraintTop_toTopOf="#+id/textView20" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have a fragment that I need show and gone some XML layout into that with some steps (in this example I have 3 steps) I know simply we can include layout into parent fragment layout and just handle visible/gone layout. but I want to do it with ConstraintSet in ConstraintLayout. as I search for load another XML layout into parent XML layout both root elements in each XML layout must be constraint layout with the same component and same ids. but in my example, I have different XML layout with root element constraint layout but in each XML layout, I have different components and ids. now my question is how I can change part of my XML layout according to the step live data with another XML layout?
we care about be smooth this layout and change layout with some animation. these are my files:
PickupFragment:
class PickupFragment : BaseFragment<FragmentPickupBinding, PickupViewModel>
(R.layout.fragment_pickup, PickupViewModel()) {
private val primaryConstraintBody = ConstraintSet()
private val parcelConstraintBody = ConstraintSet()
private val senderConstraintBody = ConstraintSet()
private val receiverConstraintBody = ConstraintSet()
private val body by lazy { binding.body }
private val pickupRequestStepObserver = Observer<Int> { step ->
when (step) {
0 -> parcelConstraintBody.loading(body)
1 -> senderConstraintBody.loading(body)
2 -> receiverConstraintBody.loading(body)
}
}
override fun initVariables() {
binding.viewModel = vModel
}
override fun initObserves() {
vModel.pickupRequestStep.observe(this, pickupRequestStepObserver)
}
override fun initViews() {
vModel.pickupRequestStep.value = 0
primaryConstraintBody.clone(body)
parcelConstraintBody.clone(context, R.layout.fragment_pickup_parcel)
senderConstraintBody.clone(context, R.layout.fragment_pickup_sender)
receiverConstraintBody.clone(context, R.layout.fragment_pickup_receiver)
}
}
fragment_pickup:
<?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="viewModel"
type="com.chaparnet.chapar.views.pickup.PickupViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/body">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintBody"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
fragment_pickup_parcel:
<?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>
<variable
name="viewModel"
type="com.chaparnet.chapar.views.pickup.PickupViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".delivery_pickup.AddCargoFragment">
<TextView
android:id="#+id/txt_text_add_pickup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:fontFamily="#font/main_bold"
android:text=" "
android:textColor="#color/colorPrimary"
android:textSize="18sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txt_text_new_pickup_waybill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="16dp"
android:fontFamily="#font/main_medium"
android:text=""
android:textColor="#color/lightGray"
android:textSize="14sp"
android:visibility="gone"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/txt_text_add_pickup" />
<EditText
android:id="#+id/et_new_pickup_waybill"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginRight="8dp"
android:background="#android:color/transparent"
android:fontFamily="#font/main_medium"
android:gravity="right"
android:inputType="number"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="100dp"
android:paddingBottom="16dp"
android:textColor="#color/darkGray"
android:textSize="14sp"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/txt_text_add_pickup" />
<View
android:id="#+id/view_waybill"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#color/lighterGray"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/et_new_pickup_waybill" />
<TextView
android:id="#+id/txt_text_new_pickup_closed_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:fontFamily="#font/main_medium"
android:text=" "
android:textColor="#color/lightGray"
android:textSize="14sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_waybill" />
<EditText
android:id="#+id/et_new_pickup_closed_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:background="#android:color/transparent"
android:fontFamily="#font/main_medium"
android:gravity="right"
android:inputType="number"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="100dp"
android:paddingBottom="16dp"
android:text="#={viewModel.pickupClosedCount}"
android:textColor="#color/darkGray"
android:textSize="14sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_waybill" />
<View
android:id="#+id/view_closed_count"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#color/lighterGray"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/et_new_pickup_closed_count" />
<TextView
android:id="#+id/txt_text_new_pickup_kind_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:fontFamily="#font/main_medium"
android:text=" "
android:textColor="#color/lightGray"
android:textSize="14sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_closed_count" />
<Spinner
android:id="#+id/spinner_new_pickup_kind_service"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="14dp"
android:layout_marginRight="100dp"
android:paddingLeft="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_closed_count" />
<View
android:id="#+id/view_kind_service"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:layout_marginLeft="20dp"
android:layout_marginTop="16dp"
android:layout_marginRight="20dp"
android:background="#color/lighterGray"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/txt_text_new_pickup_kind_service" />
<TextView
android:id="#+id/txt_text_new_pickup_kind_rent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:fontFamily="#font/main_medium"
android:text=""
android:textColor="#color/lightGray"
android:textSize="14sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_kind_service" />
<!--<androidx.appcompat.widget.AppCompatSpinner
android:id="#+id/spinner_new_pickup_kind_rent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="100dp"
android:paddingLeft="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_kind_service" />-->
<RadioGroup
android:id="#+id/spinner_new_pickup_kind_rent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:layout_marginRight="100dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_kind_service">
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginRight="16dp"
android:button="#null"
android:checked="#={viewModel.radioPasPayment}"
android:drawableRight="?android:attr/listChoiceIndicatorSingle"
android:fontFamily="#font/main_light"
android:layoutDirection="rtl"
android:text=""
android:textAlignment="textStart"
android:textSize="12sp" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:button="#null"
android:checked="#={viewModel.radioPishPayment}"
android:drawableRight="?android:attr/listChoiceIndicatorSingle"
android:fontFamily="#font/main_light"
android:layoutDirection="rtl"
android:text=""
android:textAlignment="textStart"
android:textSize="12sp" />
</RadioGroup>
<View
android:id="#+id/view_kind_rent"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:layout_marginLeft="20dp"
android:layout_marginTop="16dp"
android:layout_marginRight="20dp"
android:background="#color/lighterGray"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/txt_text_new_pickup_kind_rent" />
<!--<TextView
android:id="#+id/txt_text_new_service_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:fontFamily="#font/main_medium"
android:tag="check_for_visibility"
android:text="Service percent"
android:textColor="#color/lightGray"
android:textSize="14sp"
android:visibility="gone"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_kind_rent" />
<EditText
android:id="#+id/et_new_pickup_service_percentage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:background="#android:color/transparent"
android:fontFamily="#font/main_medium"
android:gravity="right"
android:inputType="number"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="100dp"
android:paddingBottom="16dp"
android:tag="check_for_visibility"
android:textColor="#color/darkGray"
android:textSize="14sp"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_kind_rent" />
<View
android:id="#+id/view_kind_service_percentage"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:layout_marginLeft="20dp"
android:layout_marginTop="16dp"
android:layout_marginRight="20dp"
android:background="#color/lighterGray"
android:tag="check_for_visibility"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/txt_text_new_service_percentage" />-->
<TextView
android:id="#+id/txt_text_new_weight_of_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:fontFamily="#font/main_medium"
android:tag="check_for_visibility"
android:text="weight"
android:textColor="#color/lightGray"
android:textSize="14sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_kind_rent" />
<EditText
android:id="#+id/et_new_pickup_weight_of_goods"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:background="#android:color/transparent"
android:fontFamily="#font/main_medium"
android:gravity="right"
android:inputType="number"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="100dp"
android:paddingBottom="16dp"
android:tag="check_for_visibility"
android:text="#={viewModel.weightOfGoods}"
android:textColor="#color/darkGray"
android:textSize="14sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_kind_rent" />
<View
android:id="#+id/view_weight_of_goods"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:layout_marginLeft="20dp"
android:layout_marginTop="16dp"
android:layout_marginRight="20dp"
android:background="#color/lighterGray"
android:tag="check_for_visibility"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/txt_text_new_weight_of_goods" />
<TextView
android:id="#+id/txt_text_new_value_of_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:fontFamily="#font/main_medium"
android:tag="check_for_visibility"
android:text="Value"
android:textColor="#color/lightGray"
android:textSize="14sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_weight_of_goods" />
<EditText
android:id="#+id/et_new_pickup_value_of_goods"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:background="#android:color/transparent"
android:fontFamily="#font/main_medium"
android:gravity="right"
android:inputType="number"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="100dp"
android:paddingBottom="16dp"
android:tag="check_for_visibility"
android:text="#={viewModel.valueOfGoods}"
android:textColor="#color/darkGray"
android:textSize="14sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_weight_of_goods" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
and two other fragments are like fragment_pickup_sender and fragment_pickup_receiver but with different components and ids. in my fragment_pickup I'll have some button with next step and the previous step for update step live data to switch XML layout.
may please guide me on how I must handle this