Android Kotlin - viewBindinding not working - android

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.

Related

Button Click and Change Text with Sliding Root Navigation Drawer

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.

How to calculate values on edittext when other edittext is focused?

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

how to keep view on top of key board in android

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"

Android how to animate alpha and then translation?

I want to make 2 animations, I have one view that will disappear once its touched(the one with emojis and "How do you feel today?"), with an alpha animation(from 1 to 0 alpha, at the end its visibility will be View.GONE), and then all the other views in the layout should slide up instead of them going up instantly. I have already done the alpha animation, the transition one is the one I can't achieve.
I am using Constraint Layout so what happens is as soon as the first view disappears changing its visibility to GONE, the others move instantly up. I have tried many ways, but I can´t find how to achieve this. I don't know what type of transition or animation should I use.
This is the code I am using fot the alpha animation:
fun hideEmojiView(view: View) {
val fadeOut: Animation = AlphaAnimation(1f, 0f)
fadeOut.interpolator = AccelerateInterpolator()
fadeOut.duration = 500
fadeOut.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationEnd(animation: Animation) {
view.visibility = View.GONE
}
override fun onAnimationRepeat(animation: Animation) {}
override fun onAnimationStart(animation: Animation) {}
})
view.startAnimation(fadeOut)
}
UPDATE: SOLUTION
I ended up making the suggestion #Tenfour04 made. The problem was that I was adding android:animateLayoutChanges="true" to a ScrollView and it needs to be added to a ConstraintLayout, and it needs to be the direct parent of the view you are dissapearing. So here is my Layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
tools:context=".fragments.maincontent.TodayFragment">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="#font/subtitle_font"
android:text="Upcoming"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rv_daily_picks" />
<FrameLayout
android:id="#+id/frameLayout7"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/textView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView6"
app:layout_constraintTop_toTopOf="#+id/textView6">
</FrameLayout>
<FrameLayout
android:id="#+id/frameLayout6"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/textView5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView5"
app:layout_constraintTop_toTopOf="#+id/textView5">
</FrameLayout>
<TextView
android:id="#+id/tv_welcome"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:fontFamily="#font/title_font"
android:text="Welcome, Sebastián"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/subtitle_font"
android:text="Stories"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/emoji_view" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_stories"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5">
</androidx.recyclerview.widget.RecyclerView>
<FrameLayout
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/textView7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView7"
app:layout_constraintTop_toTopOf="#+id/textView7">
</FrameLayout>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="#font/subtitle_font"
android:text="Daily picks"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rv_stories" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_daily_picks"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6">
</androidx.recyclerview.widget.RecyclerView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_upcoming"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView7" />
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/rv_upcoming" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/emoji_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/round_white_30alpha_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_welcome">
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="#font/subtitle_font"
android:text="How do you feel today?"
android:textColor="#color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView13">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/emoji1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/circle_white"
android:text="😡"
android:textAllCaps="false"
android:textSize="26sp" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/emoji2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/circle_white"
android:text="🙁"
android:textAllCaps="false"
android:textSize="26sp" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/emoji3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/circle_white"
android:text="🤢"
android:textAllCaps="false"
android:textSize="26sp" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/emoji4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/circle_white"
android:text="😃"
android:textAllCaps="false"
android:textSize="26sp" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/emoji5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/circle_white"
android:text="🤩"
android:textAllCaps="false"
android:textSize="26sp" />
</FrameLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
and my code:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val binding = FragmentTodayBinding.inflate(layoutInflater)
val user = AppController.get<UserModel>(UserModel.key)
binding.tvWelcome.text = "Welcome, " + user!!.name
binding.emoji1.setOnClickListener{ binding.emojiView.visibility = View.GONE }
binding.emoji2.setOnClickListener{ binding.emojiView.visibility = View.GONE }
binding.emoji3.setOnClickListener{ binding.emojiView.visibility = View.GONE }
binding.emoji4.setOnClickListener{ binding.emojiView.visibility = View.GONE }
binding.emoji5.setOnClickListener{ binding.emojiView.visibility = View.GONE }
return binding.root
}

How to handle the following case for ConstraintLayout, when one of the targets of layout_constraint visibility changed to View.GONE

I want to avoid nested layout, to improve my app performance.
Hence, I tried the following
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="30dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/title_text_view"
android:background="#ffd600"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Title\nTitle"
app:layout_constraintEnd_toStartOf="#+id/pin_text_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/pin_text_view"
android:background="#ff0000"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Pin" />
<TextView
android:id="#+id/body_text_view"
android:background="#304ffe"
android:textColor="#ffffff"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/title_text_view"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Body" />
</android.support.constraint.ConstraintLayout>
The output is as follow.
During runtime, I might change the visibility of title_text_view (yellow) to View.GONE.
But, it will looks like the follow. I don't wish pin_text_view (red) to be covered up by body_text_view (blue)
What I wish to have is
One of the way to overcome, is after changing the visibility of title_text_view (yellow) to View.GONE, I need use Java code to manual update app:layout_constraintTop_toBottomOf of body_text_view (blue) - Android : How to programatically set layout_constraintRight_toRightOf "parent"
From
<TextView
android:id="#+id/body_text_view"
...
app:layout_constraintTop_toBottomOf="#+id/title_text_view"
to
<TextView
android:id="#+id/body_text_view"
...
app:layout_constraintTop_toBottomOf="#+id/pin_text_view"
But, this will make my code more difficult to be maintained.
Is there any easier way, without involving Java code, and without nested layout?
You can use a barrier, which is available in ConstraintLayout since version 1.1
So if you are using an older version of ConstraintLayout, change the dependency for ConstraintLayout in your build.gradle to
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
or a later version.
Then add the barrier to your layout like following:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="30dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/title_text_view"
android:background="#ffd600"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Title\nTitle"
app:layout_constraintEnd_toStartOf="#+id/pin_text_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/pin_text_view"
android:background="#ff0000"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Pin" />
<android.support.constraint.Barrier
android:id="#+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="title_text_view,pin_text_view" />
<TextView
android:id="#+id/body_text_view"
android:background="#304ffe"
android:textColor="#ffffff"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/barrier"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Body" />
</android.support.constraint.ConstraintLayout>
For further information see this link
you can get closest to your requirement using below code without adding any extra code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="30dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/title_text_view"
android:background="#ffd600"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Title\nTitle"
android:visibility="visible"
app:layout_constraintEnd_toStartOf="#+id/pin_text_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/pin_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:background="#ff0000"
android:text="Pin"
android:textColor="#ffffff"
app:layout_constraintEnd_toEndOf="#+id/body_text_view" />
<TextView
android:id="#+id/body_text_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:background="#304ffe"
android:text="Body"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/pin_text_view" />
OR if you want to do some extra xml code you can do something like this using guidelines feature in constraintLayout:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="30dp">
<TextView
android:id="#+id/title_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="#ffd600"
android:padding="5dp"
android:text="Title\nTitle"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/guideline"
app:layout_constraintEnd_toStartOf="#+id/pin_text_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.09" />
<TextView
android:id="#+id/pin_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:background="#ff0000"
android:text="Pin"
android:textColor="#ffffff"
app:layout_constraintEnd_toEndOf="#+id/body_text_view" />
<TextView
android:id="#+id/body_text_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:background="#304ffe"
android:text="Body"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline" />
</android.support.constraint.ConstraintLayout>

Categories

Resources