I am trying to make some math operation when some edittext has focus. I have 3 edittexts and depending on which one has focus the math operation changes and so do the other two edittext values. I have something similar in Java and it works fine, but on Kotlin it does not enter any of the desired functions.
override fun onClick(v: View?) {
if (edtProductSuggestedPrice.hasFocus() ) {
calculateWitIVA(1)
}
if(edtProductSuggestedPriceWithIva.hasFocus()){
calculateWitIVA(2)
}
}
How can I make this work?
I believe you need to use MutableLiveData<String>
and it is recommended to use two-way data binding
for example
my ViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.yazan.talabatclonedriver.Repository
import com.yazan.talabatclonedriver.db.UserEntity
class UserViewModel(private val repository: Repository) : ViewModel() {
val userName: MutableLiveData<String> = MutableLiveData("N/A")
val phoneNum: MutableLiveData<String> = MutableLiveData("404")
val driverPass: MutableLiveData<String> = MutableLiveData("")
}
and my Layout
<?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.yazan.talabatclonedriver.viewModel.UserViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/orange"
tools:context=".ui.frags.SignInFrag">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="30dp"
android:text="Welcome to \nTalabat Driver"
android:textColor="#FFF"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:src="#drawable/scooter_delivery_pic"
app:layout_constraintBottom_toTopOf="#+id/cv_login_container"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
tools:ignore="ContentDescription" />
<androidx.cardview.widget.CardView
android:id="#+id/cv_login_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="32dp"
app:cardElevation="20dp"
app:layout_constraintBottom_toTopOf="#+id/tv_navigate_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
android:text="Log into your driver account"
android:textColor="#000"
android:textSize="22sp"
android:textStyle="bold" />
<EditText
android:id="#+id/et_userName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:ems="5"
android:text="#={viewModel.userName}"
android:hint="Enter username"
android:textSize="20sp" />
<EditText
android:id="#+id/et_userPass"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:ems="5"
android:text="#={viewModel.driverPass}"
android:hint="Enter password"
android:textSize="20sp" />
<Button
android:id="#+id/bt_signIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/orange"
android:padding="10dp"
android:text="Continue"
android:textAllCaps="false"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/tv_navigate_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{viewModel.driverPass}"
android:textColor="#FFF"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
when you change the et_userName edit text the tv_navigate_text should change at the same moment
don't forget to add
BuildFeatures{
dataBinding = true
}
to your level app Gradle
Related
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 have a stateflow that is giving me the value of a mutable state flow from ViewModel, what I am trying to do is, I want to show hide a webview based on a button click. When the value is true I want to show the web view and when I want to hide it I change its value to false. The values are being updated correctly but not reflecting inside data binding.
this is my viewmodel
class ArticleDetailsViewModel : ViewModel() {
private val _isWebViewShowing = MutableStateFlow(false)
val isWebViewShowing: StateFlow<Boolean>
get() = _isWebViewShowing
fun onReadMoreClicked() {
_isWebViewShowing.value = true
}
fun changeWebViewState() {
_isWebViewShowing.value = false
}}
This my XML where I am doing the comparison
<?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="article"
type="io.infinity.newsapp.model.domain.ArticleDomainModel" />
<variable
name="viewModel"
type="io.infinity.newsapp.viewmodels.ArticleDetailsViewModel" />
<import type="android.view.View"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_generic"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="#{viewModel.isWebViewShowing().value ? View.INVISIBLE :View.VISIBLE }"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/toolbar"
app:layout_constraintStart_toStartOf="#+id/toolbar"
app:layout_constraintTop_toBottomOf="#+id/toolbar">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
app:cardCornerRadius="#dimen/_20sdp"
android:layout_margin="#dimen/_16sdp"
android:layout_height="250dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
loadImageFromUrl="#{article.urlToImage}"
android:scaleType="centerCrop"/>
</com.google.android.material.card.MaterialCardView>
<TextView
android:id="#+id/textView"
style="#style/eighteen_sp_muli_bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_8sdp"
android:text="#{article.title}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_margin="#dimen/_16sdp"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvAuthor"
style="#style/twelve_sp_muli_extra_bold"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="start"
android:layout_marginStart="8dp"
android:text="#{article.author}"
android:textAllCaps="true"
android:textColor="#color/semi_black"
/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="#dimen/_4sdp"
android:layout_marginEnd="#dimen/_2sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/tvTitle"
app:srcCompat="#drawable/circle_blue" />
<TextView
android:id="#+id/tvSource"
style="#style/twelve_sp_muli_extra_bold"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="#{article.source.name}"
android:textAllCaps="true"
android:textColor="#color/semi_black"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="#+id/imageView2" />
<TextView
android:id="#+id/publishedOn"
android:gravity="end"
style="#style/twelve_sp_muli_extra_bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginStart="8dp"
setDateAndTime="#{article.publishedAt}"
android:textAllCaps="true"
android:textColor="#color/semi_black"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
style="#style/fourteen_sp_muli_bold"
android:text="#{article.description}"
android:layout_marginStart="#dimen/_16sdp"
android:layout_marginEnd="#dimen/_16sdp"
android:layout_marginTop="#dimen/_16sdp"
android:layout_marginBottom="0dp"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tvReadMore"
android:onClick="#{() -> viewModel.onReadMoreClicked()}"
android:layout_width="match_parent"
style="#style/twelve_sp_muli_regular"
android:text="#string/read_more"
android:textColor="#color/light_blue"
android:layout_marginStart="#dimen/_16sdp"
android:layout_height="wrap_content"/>
</LinearLayout>
<WebView
android:id="#+id/webView"
loadArticleInWeb="#{article.url}"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="#{viewModel.isWebViewShowing().value ? View.VISIBLE :View.INVISIBLE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
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.
Iam working on an app using recyclerview in which if I click an item, it will change in layout height(view will be visible) and margins changes.
How to animate like google dialer app when I click recent contact will show elevation layout and shrink margins?
I use notifyitemchange(position) and work in animate shift of point (left and top of layout).
but I want to animate shrink layout margins not shift view like google app dialer recent contacts [google app dialer].
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/groups_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="10dp"
android:paddingBottom="180dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
viewholder layout
<?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="group"
type="com.example.gtt.database.Group" />
<variable
name="clickListener"
type="com.example.gtt.start.GroupListener" />
<variable
name="viewModel"
type="com.example.gtt.start.StartViewModel" />
</data>
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
layoutMarginGroup="#{group}"
layoutMarginGroupId="#{viewModel.hideShowId}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
app:cardCornerRadius="6dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/itemLayout"
layoutBackColor="#{group}"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:onClick="#{() -> viewModel.onGroupHideShowClicked(group.groupId)}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/groupView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="#dimen/layoutMargin"
android:layout_marginTop="#dimen/layoutMargin"
android:layout_marginBottom="#dimen/layoutMargin"
android:background="#drawable/colored_circle"
android:gravity="center_horizontal|center_vertical"
android:onClick="#{() -> clickListener.onClick(group)}"
android:text="#{group.groupName.length >=2 ? group.groupName.substring(0,2).toUpperCase() : group.groupName.toUpperCase() }"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/white"
app:layout_constraintEnd_toStartOf="#id/groupsTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- android:text="#string/g" -->
<TextView
android:id="#+id/groupsTextView"
groupNameText="#{group}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:textColor="#color/colorBlue"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="#id/materialTextView"
app:layout_constraintEnd_toStartOf="#+id/cml"
app:layout_constraintStart_toEndOf="#id/groupView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/materialTextView"
groupMaterialText="#{group}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/colorBlue"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="#id/appTextView"
app:layout_constraintEnd_toEndOf="#id/groupsTextView"
app:layout_constraintStart_toStartOf="#id/groupsTextView"
app:layout_constraintTop_toBottomOf="#+id/groupsTextView" />
<TextView
android:id="#+id/appTextView"
appText="#{group}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textColor="#color/colorBlue"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="#+id/groupsTextView"
app:layout_constraintStart_toStartOf="#id/groupsTextView"
app:layout_constraintTop_toBottomOf="#+id/materialTextView" />
<LinearLayout
android:id="#+id/cml"
cmlBackColor="#{group}"
android:layout_width="wrap_content"
android:minWidth="70dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="#+id/appTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/groupsTextView"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/numberOfClasses"
android:textAlignment="center"
android:textSize="12sp" />
<TextView
android:id="#+id/classTextView"
classText="#{group}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textAlignment="center"
android:textColor="#color/colorBlue"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/numberOfMonths"
android:textAlignment="center"
android:textSize="12sp" />
<TextView
android:id="#+id/monthTextView"
monthText="#{group}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textAlignment="center"
android:textColor="#color/colorBlue"
android:textSize="18sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout2"
layoutVisibilityGroup="#{group}"
layoutVisibilityGroupId="#{viewModel.hideShowId}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cLayout">
<!-- android:visibility="gone"
android:background="#drawable/shadow_border"
-->
<View
android:id="#+id/divider9"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/addClassButton"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_add_class"
android:onClick="#{() -> viewModel.addClass()}"
android:text="#string/c"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/delClassButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/delClassButton"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:drawableTop="#drawable/ic_del_class"
android:onClick="#{() -> viewModel.delClass()}"
android:text="#string/c"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/addClassButton"
app:layout_constraintTop_toTopOf="#+id/addClassButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</layout>
binding adapter to change data and layout
#BindingAdapter("layoutVisibilityGroup", "layoutVisibilityGroupId")
fun ConstraintLayout.setLayoutVisibility(item: Group, id: Long) {
visibility = if (item.groupId == id) {
View.VISIBLE
} else {
View.GONE
}
}
#BindingAdapter("layoutMarginGroup","layoutMarginGroupId")
fun setLayoutMargin(view: CardView, item: Group, id: Long) {
if (view.layoutParams is ViewGroup.MarginLayoutParams) {
val p = view.layoutParams as ViewGroup.MarginLayoutParams
if (item.groupId == id) {
val npx = dpToPixel(10f).toInt()
p.setMargins(npx, p.topMargin, npx, p.bottomMargin)
} else {
p.setMargins(0, p.topMargin,0, p.bottomMargin)
}
//view.layoutParams = p
}
view.cardElevation = if (item.groupId == id) {
dpToPixel(8f)
} else {
0f
}
}
and I use this code to animate layout changes
adapter.notifyItemChanged(oldPosition)
adapter.notifyItemChanged(newPosition)
This is my xml :
<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:fillViewport="true">
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/white"
android:paddingLeft="#dimen/cl_12"
android:paddingTop="#dimen/cl_48"
android:paddingRight="#dimen/cl_12"
android:paddingBottom="#dimen/cl_28">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fingerprint_id"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_title"
style="#style/Typeface.Body.Bold.TextDarkGrey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/padding_8"
android:lineSpacingExtra="4sp"
android:text="Enter your password"
app:layout_constraintTop_toBottomOf="#+id/imageView"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/tv_fingerprint_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/cl_16"
android:lineSpacingExtra="8sp"
android:text="Enter your Tesco password to activate fingerprint ID"
app:layout_constraintTop_toBottomOf="#+id/tv_title"
tools:ignore="MissingConstraints" />
<EditText
android:id="#+id/et_input_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/cl_20"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="#+id/tv_fingerprint_description" />
<TextView
android:id="#+id/tv_close"
style="#style/Typeface.Body.TescoBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/cl_24"
android:layout_marginRight="#dimen/padding_8"
android:lineSpacingExtra="8sp"
android:padding="#dimen/padding_8"
android:text="Cancel"
app:layout_constraintEnd_toStartOf="#+id/tv_submit"
app:layout_constraintTop_toBottomOf="#+id/et_input_text" />
<TextView
android:id="#+id/tv_submit"
style="#style/Typeface.Body.TescoBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/cl_24"
android:lineSpacingExtra="8sp"
android:padding="#dimen/padding_8"
android:text="Submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/et_input_text"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
This is my code written in class
et_input_text.setOnFocusChangeListener(object : View.OnFocusChangeListener{
override fun onFocusChange(v: View?, hasFocus: Boolean) {
if(hasFocus)
scroll_view.scrollTo(0,scroll_view.bottom)
}
})
in manifest file, I have added
<activity
android:name=".features.common.customview.FingerPrintWithPasswordDialogActivity"
android:theme="#style/PaddedScreenDialogTheme"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait" />
I am trying to set my submit and cancel button on top of the keyboard but it is hidden inside the keyboard please suggest to me how to achieve this.
please see in this screen Submit and the canal is hidden i want to scroll full bottom so that it should show cancel and submit button.
Try by removing
android:windowSoftInputMode="adjustPan"