I have a scrollView and ConstraintLayout as a child. Inside the constraintLayout, I have at least 2 editTexts.
My layout :
<?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="#color/colorWhite"
android:descendantFocusability="afterDescendants"
android:fitsSystemWindows="true"
android:focusable="false">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_with_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/status_bar_height"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="#+id/sendScrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
android:isScrollContainer="false"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="#id/nextButton"
app:layout_constraintEnd_toEndOf="#id/endGuideline"
app:layout_constraintStart_toStartOf="#id/startGuideline"
app:layout_constraintTop_toBottomOf="#id/toolbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/exchangeCardView"
android:layout_width="#dimen/currency_switch_width"
android:layout_height="#dimen/currency_switch_height"
android:layout_marginTop="#dimen/default_margin"
android:padding="2dp"
app:cardCornerRadius="20dp"
app:cardElevation="#dimen/elevation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ExchangeCurrencyView
android:id="#+id/exchangeSwitchView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.cardview.widget.CardView>
<EditText
android:id="#+id/amountEditText"
style="#style/EditTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/default_margin_x2"
android:autofillHints=""
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:inputType="number|numberDecimal"
android:maxLines="1"
android:minWidth="40dp"
android:singleLine="true"
android:textSize="50sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/exchangeCardView" />
<TextView
android:id="#+id/amountTextView"
style="#style/AmountTextViewStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/amountEditText" />
<TextView
android:id="#+id/balanceTextView"
style="#style/AddressTextViewStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/amountTextView" />
<SidedEditTextView
android:id="#+id/walletAddressEditTextView"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginTop="#dimen/default_margin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/balanceTextView" />
<TextView
android:id="#+id/miningFeesTextView"
style="#style/AddressTextViewStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/default_margin"
android:gravity="center"
android:textColor="#color/feesColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/walletAddressEditTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<Button
android:id="#+id/nextButton"
style="#style/RoundButtonStyle"
android:layout_width="0dp"
android:layout_height="#dimen/button_height"
android:layout_marginBottom="#dimen/default_margin_x1"
android:enabled="false"
android:gravity="center"
android:stateListAnimator="#null"
android:text="#string/send"
android:translationZ="#dimen/elevation"
app:elevation="#dimen/elevation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#id/endGuideline"
app:layout_constraintStart_toStartOf="#id/startGuideline"
app:layout_constraintTop_toBottomOf="#id/sendScrollView" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/startGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.06" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/endGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.94" />
I would like to scroll to the bottom of my scrollView in order to see all inputs above the button.
I used a KeyboardHeightProvider to to do this and it works :
override fun onHeightChanged(height: Int) {
getScrollView()?.let {
when (height) {
0 -> {
it.post {
it.scrollTo(0, 0)
onHide()
}
}
else -> {
it.post {
it.fullScroll(View.FOCUS_DOWN)
}
}
}
}
}
But with this snippet, the keyboard is blinking on screen and I get IInputConnectionWrapper warning.
I tried many other solutions like adding adjustPan or adjustResize but none worked.
android:windowSoftInputMode="adjustResize"
android:fitsSystemWindows="true"
Also, I used this :
val rootView = view?.findViewById<ViewGroup>(android.R.id.content)?.getChildAt(0)
rootView?.let {
it.viewTreeObserver.addOnGlobalLayoutListener {
val diff = it.rootView.height - it.height
if (diff > dpToPx(context!!, 200F)) {
sendScrollView.fullScroll(View.FOCUS_DOWN)
} else {
sendScrollView.fullScroll(View.FOCUS_UP)
}
}
}
Any idea?
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 am currently trying to expand a view when it is focused and contract it when unfocused. But, I am unable to get the events for onFocusChangeListener. I tried adding that for all of the views inside the parent view but that didn't work either. Is there anything like reyclerview takes all the focus changes to itself not allowing its children? This is my item code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginBottom="20dp"
app:cardCornerRadius="6dp"
app:cardElevation="3dp">
<LinearLayout
android:id="#+id/selected"
android:layout_width="7dp"
android:layout_height="match_parent"
android:background="#167ED1"
android:orientation="horizontal" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/largeItemView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible"
android:focusable="true"
android:focusableInTouchMode="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/primary"
android:alpha="0.5"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/questionTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Question"
android:maxLines="1"
android:padding="20dp"
android:theme="#style/SelectedColorBlue"
app:layout_constraintEnd_toStartOf="#+id/settings"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:focusable="true"
android:focusableInTouchMode="true"/>
<ImageView
android:id="#+id/settings"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_marginHorizontal="10dp"
android:background="?selectableItemBackgroundBorderless"
android:src="#drawable/more_vert"
app:layout_constraintBottom_toBottomOf="#id/questionTitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#id/questionTitle"
app:tint="#color/white" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/optionsRV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="vertical"
android:padding="10dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="#id/addOption"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/questionTitle"
tools:itemCount="2"
tools:listitem="#layout/add_form_option_item" />
<TextView
android:id="#+id/points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="1 Point"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="#id/addOption"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/addOption" />
<TextView
android:id="#+id/editPoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="#string/editHtmlUnderline"
android:textColor="#color/primary"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="#+id/points"
app:layout_constraintStart_toEndOf="#+id/points"
app:layout_constraintTop_toTopOf="#+id/points" />
<Button
android:id="#+id/addOption"
android:layout_width="wrap_content"
android:layout_height="57dp"
android:layout_margin="20dp"
android:drawableEnd="#drawable/plus"
android:text="Add Option"
app:cornerRadius="18dp"
app:layout_constraintBottom_toTopOf="#id/required"
app:layout_constraintEnd_toEndOf="parent" />
<ImageView
android:id="#+id/delete"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="10dp"
android:padding="3dp"
android:src="#drawable/delete"
app:layout_constraintBottom_toBottomOf="#+id/required"
app:layout_constraintEnd_toStartOf="#+id/imageView"
app:layout_constraintTop_toTopOf="#+id/required"
app:tint="#color/gray" />
<ImageView
android:id="#+id/imageView"
android:layout_width="1dp"
android:layout_height="35dp"
android:layout_marginHorizontal="10dp"
android:padding="3dp"
android:src="#color/gray"
app:layout_constraintBottom_toBottomOf="#+id/required"
app:layout_constraintEnd_toStartOf="#id/required"
app:layout_constraintTop_toTopOf="#+id/required"
app:tint="#color/gray" />
<CheckBox
android:id="#+id/required"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:buttonTint="#color/primary"
android:checked="true"
android:text="Required"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<androidx.constraintlayout.widget.Group
android:id="#+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="addOption,points,editPoints,delete,imageView,required" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
and this is my code for adding the focus listsner inside the onBIndViewHolder:
binding.largeItemView.setOnFocusChangeListener { _: View, hasFocus: Boolean ->
Toast.makeText(itemView.context,"Focus change $hasFocus", Toast.LENGTH_SHORT).show()
binding.selected.visibility = if(hasFocus) VISIBLE else View.GONE
binding.group.visibility = if(hasFocus) VISIBLE else View.GONE
}
binding.largeItemView.children.forEach {
it.setOnFocusChangeListener { _, hasFocus ->
Toast.makeText(itemView.context,"Focus change $hasFocus", Toast.LENGTH_SHORT).show()
binding.selected.visibility = if(hasFocus) VISIBLE else View.GONE
binding.group.visibility = if(hasFocus) VISIBLE else View.GONE
}
}
and this is my reycler view:
<ScrollView>
<NestedScrollView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/optionsRV"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingHorizontal="10dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="10"
tools:listitem="#layout/item_question_add_item" />
</NestedScrollView>
</ScrollView>
Thanks in advance
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)
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
}
I have the following layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/content_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_32"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.cardview.widget.CardView
android:id="#+id/online_card_cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="#dimen/margin_16"
android:layout_marginTop="#dimen/margin_25"
android:background="#color/white"
app:cardCornerRadius="#dimen/margin_8">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="#dimen/margin_8">
<androidx.appcompat.widget.AppCompatRadioButton
android:id="#+id/online_card_rb"
android:layout_width="#dimen/margin_22"
android:layout_height="#dimen/margin_22"
android:layout_marginStart="#dimen/margin_16"
app:layout_constraintBottom_toBottomOf="#id/online_card_ll"
app:layout_constraintEnd_toStartOf="#id/online_card_ll"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/online_card_ll" />
<LinearLayout
android:id="#+id/online_card_ll"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_16"
android:background="#color/white"
android:gravity="start"
android:orientation="vertical"
android:paddingVertical="#dimen/padding_16"
app:layout_constraintEnd_toStartOf="#id/cards_iv"
app:layout_constraintStart_toEndOf="#id/online_card_rb"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/online_card_tv"
style="#style/OnlineCardTitleTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/online_card_title"
app:lineHeight="#dimen/default_text_size_28" />
<androidx.appcompat.widget.AppCompatTextView
style="#style/OnlineCardSubTitleTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/online_card_now" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/cards_iv"
android:layout_width="#dimen/margin_80"
android:layout_height="#dimen/margin_20"
android:layout_marginEnd="#dimen/margin_16"
android:src="#drawable/ic_visa_master"
app:layout_constraintBottom_toBottomOf="#id/online_card_ll"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#id/online_card_ll" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/online_card_expanded_info_cl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="#id/online_card_ll">
<View
android:id="#+id/online_card_divider"
style="#style/ViewDividerStyle"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="#dimen/margin_16"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/cards_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/online_card_divider" />
<View
android:id="#+id/cards_list_divider"
style="#style/ViewDividerStyle"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="#id/cards_list" />
<TextView
android:id="#+id/read_more_tv"
style="#style/OnlineCardReadMoreTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_16"
android:layout_marginTop="#dimen/margin_16"
android:layout_marginBottom="#dimen/margin_8"
android:text="#string/read_more"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cards_list_divider" />
<LinearLayout
android:id="#+id/additional_info_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_21"
android:layout_marginTop="#dimen/margin_16"
android:layout_marginEnd="#dimen/margin_10"
android:layout_marginBottom="#dimen/margin_8"
android:orientation="vertical"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cards_list_divider">
<TextView
android:id="#+id/online_card_less_info_tv"
style="#style/OnlineCardReadMoreTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/online_card_less_info" />
<TextView
android:id="#+id/check_card_type_title_tv"
style="#style/OnlineCardBottomTitleTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_4"
android:text="#string/online_check_card" />
<TextView
android:id="#+id/check_card_type_info_tv"
style="#style/OnlineCardBottomDescriptionTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_8"
android:layout_marginTop="#dimen/margin_4"
android:text="#string/online_check_card_info" />
<TextView
android:id="#+id/online_card_data_title_tv"
style="#style/OnlineCardBottomTitleTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_4"
android:text="#string/online_card_data" />
<TextView
android:id="#+id/online_card_data_info_tv"
style="#style/OnlineCardBottomDescriptionTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_8"
android:layout_marginTop="#dimen/margin_4"
android:text="#string/online_card_data_info" />
<TextView
android:id="#+id/online_card_confirmation_title_tv"
style="#style/OnlineCardBottomTitleTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_4"
android:text="#string/online_card_confirmation" />
<TextView
android:id="#+id/online_card_confirmation_info_tv"
style="#style/OnlineCardBottomDescriptionTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_8"
android:layout_marginTop="#dimen/margin_4"
android:text="#string/online_card_confirmation_info" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/bank_account_cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="#dimen/margin_16"
android:layout_marginTop="25dp"
android:background="#color/white"
app:cardCornerRadius="#dimen/margin_8">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginVertical="#dimen/margin_24">
<androidx.appcompat.widget.AppCompatRadioButton
android:id="#+id/bank_account_rb"
android:layout_width="#dimen/margin_22"
android:layout_height="#dimen/margin_22"
android:layout_marginStart="#dimen/margin_16"
app:layout_constraintBottom_toBottomOf="#id/bank_account_tv"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/bank_account_tv" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/bank_account_tv"
style="#style/OnlineCardTitleTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_14"
android:text="#string/title_on_bank"
android:textAlignment="textStart"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/bank_account_rb"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/bank_account_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="#dimen/margin_16"
android:alpha="0.5"
android:background="#000001"
app:layout_constraintTop_toBottomOf="#id/bank_account_tv" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/iban_hint_tv"
style="#style/OnlineCardEditTextHintTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_16"
android:layout_marginTop="15dp"
android:text="#string/step7_iban_hint"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/bank_account_divider" />
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/step1_phone"
android:layout_width="match_parent"
android:layout_height="#dimen/height_edittext"
android:layout_marginHorizontal="#dimen/margin_16"
android:layout_marginTop="#dimen/margin_2"
android:background="#drawable/rounded_edittext_with_border"
android:maxLines="1"
app:layout_constraintTop_toBottomOf="#id/iban_hint_tv" />
<androidx.constraintlayout.widget.Group
android:id="#+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:constraint_referenced_ids="step1_phone, iban_hint_tv, bank_account_divider" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/step7_next_btn"
android:layout_width="#dimen/width_button_next"
android:layout_height="#dimen/height_button_next"
android:layout_marginBottom="#dimen/margin_10"
android:background="#drawable/rounded_button_next"
android:text="#string/next"
android:textColor="#color/button_text_green"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
When the part of the view has gone visibility, my screen looks like this:
As you can see, button is placed in the bottom and it's not overlapped by any other views. But when card is expanded, it looks like this:
As you can see, now button overlaps another card, and I need to place button under the card. So, how can I prevent existing behavior and place the button under all cards in the bottom? Thanks in advance, I will appreciate any help!
See below for an updated answer.
Old
I don't see the purpose of ConstraintLayout. You can remove that and place the button as last item in LinearLayout. The XML would then be like:
NestedScrollingView
LinearLayout
CardView ...
CardView ...
AppCompatButton
You can also use ConstraintLayout instead of LinearLayout, but I don't see the need for both.
This introduces a problem: the button is not at the bottom, when the content does not fill out the screen. For an easy fix, you can use ConstraintLayout with appropriate constraints.
New
I suggest you to use a ConstraintLayout. With it, you can achieve a flat layout hierarchy. You can use chains and vertical bias as such:
Chain the first card to the top of your parent
Chain the button to the bottom of your parent
Chain the second card to the bottom of the first card and to the top of the button. Apply a vertical bias of 0.0.
The result:
And the simple code for the example:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dddddd"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="#+id/card1"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_margin="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="First card" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/card2"
android:layout_width="0dp"
android:layout_height="800dp"
android:layout_margin="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:layout_constraintBottom_toTopOf="#id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/card1"
app:layout_constraintVertical_bias="0.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Second card" />
</androidx.cardview.widget.CardView>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="64dp"
android:layout_marginBottom="16dp"
android:background="#ffffff"
android:text="Button text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>