How to make Scrollview automatically scroll to the bottom when RadioButton is clicked May I know How to make the Scroll view automatically scroll down to the next_button when any of the Radio Button is clicked , in the same time I need to call "setMeal() method from the ViewModel when the Radio Button is clicked.
<?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"
tools:context=".ui.MainMeal">
<data>
<variable
name="viewModel"
type="com.example.mymeal.model.OrderViewModel" />
<variable
name="mainMeal"
type="com.example.mymeal.ui.MainMeal" />
</data>
<ScrollView
android:layout_width="match_parent"
android:id="@ id/scrollView"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp">
<com.google.android.material.card.MaterialCardView
android:id="@ id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:layout_constraintBottom_toTopOf="@ id/divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioGroup
android:id="@ id/main_meal_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@ id/royal_lobster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{viewModel.menuItems["royal butter lobster"].name}'
android:onClick='@{() -> viewModel.setMeal("royal butter lobster")}'
android:textAppearance="?attr/textAppearanceBody1"
tools:text="Royal Butter Lobster" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@ id/royal_lobster_image"
android:layout_width="match_parent"
android:layout_height="194dp"
android:layout_margin="8dp"
app:shapeAppearanceOverlay="@style/cut_corner"
app:srcCompat="@drawable/royal_butter_lobster" />
<TextView
android:id="@ id/royal_lobster_description"
style="@style/Widget.MenuItem.TextView.Info"
android:text='@{viewModel.menuItems["royal butter lobster"].description}'
tools:text="Entree 3 description" />
<TextView
android:id="@ id/royal_lobster_price"
style="@style/Widget.MenuItem.TextView.Info"
android:paddingTop="8dp"
android:text='@{viewModel.menuItems["royal butter
lobster"].getFormattedPrice()}'
tools:text="$0.00" />
<View
style="@style/Widget.MyMeal.Divider"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/royal_lobster_description" />
<RadioButton
android:id="@ id/kebbeh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{viewModel.menuItems["kebbeh"].name}'
android:onClick='@{() -> viewModel.setMeal("kebbeh")}'
android:textAppearance="?attr/textAppearanceBody1"
tools:text="Kebbeh" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@ id/kebbeh_image"
android:layout_width="match_parent"
android:layout_height="194dp"
android:layout_margin="8dp"
app:shapeAppearanceOverlay="@style/cut_corner"
app:srcCompat="@drawable/kebbeh" />
<TextView
android:id="@ id/kebbeh_description"
style="@style/Widget.MenuItem.TextView.Info"
android:text='@{viewModel.menuItems["kebbeh"].description}'
tools:text="Entree 4 description" />
<TextView
android:id="@ id/kebbeh_price"
style="@style/Widget.MenuItem.TextView.Info"
android:paddingTop="8dp"
android:text='@{viewModel.menuItems["kebbeh"].getFormattedPrice()}'
tools:text="$0.00" />
<View
style="@style/Widget.MyMeal.Divider"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/kebbeh_description" />
<RadioButton
android:id="@ id/shish_tawook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{viewModel.menuItems["shish tawook"].name}'
android:onClick='@{() -> viewModel.setMeal("shish tawook")}'
android:textAppearance="?attr/textAppearanceBody1"
tools:text="Shish Tawook" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@ id/shish_tawook_image"
android:layout_width="match_parent"
android:layout_height="194dp"
android:layout_margin="8dp"
app:shapeAppearanceOverlay="@style/cut_corner"
app:srcCompat="@drawable/shish_tawook" />
<TextView
android:id="@ id/shish_tawook_description"
style="@style/Widget.MenuItem.TextView.Info"
android:text='@{viewModel.menuItems["shish tawook"].description}'
tools:text="Entree 4 description" />
<TextView
android:id="@ id/shish_tawook_price"
style="@style/Widget.MenuItem.TextView.Info"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text='@{viewModel.menuItems["shish tawook"].getFormattedPrice()}'
tools:text="$0.00" />
</RadioGroup>
</com.google.android.material.card.MaterialCardView>
<View
android:id="@ id/divider"
style="@style/Widget.MyMeal.Divider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cardView" />
<TextView
android:id="@ id/subtotal"
style="@style/Widget.MyMeal.TextView.Subtotal"
android:text='@{@string/subtotal(viewModel.subtotal)}'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider"
tools:text="Subtotal $5.00" />
<Button
android:id="@ id/next_button"
style="@style/Widget.Order.Button.Next"
android:onClick="@{()-> mainMeal.goToNextFragment()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/cancel_button"
app:layout_constraintTop_toBottomOf="@id/subtotal" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>
This is the fragment class file
package com.example.mymeal.ui
class MainMeal : Fragment() {
// Binding object instance corresponding to the fragment_main_meal.xml layout.
private var _binding: FragmentMainMealBinding? = null
private val binding get() = _binding!!
//Get instance of OrderViewModel
private val sharedViewModel: OrderViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentMainMealBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.lifecycleOwner = viewLifecycleOwner
binding.viewModel = sharedViewModel
binding.mainMeal = this
}
//Navigate to the salad menu fragment
fun goToNextFragment() {
findNavController().navigate(R.id.action_mainMeal_to_saladMenu)
}
//Cancel the order
private fun cancelOrder() {
//Reset the values
sharedViewModel.resetOrder()
//Navigate back to the start fragment
findNavController().navigate(R.id.action_mainMeal_to_startFragment)
}
private fun doNothing() {
return
}
//confirmation before cancel the order
fun showDialog() {
MaterialAlertDialogBuilder(requireContext()).setMessage(getString(R.string.cancel_order))
.setCancelable(true)
.setNegativeButton(getString(R.string.yes)) { _, _ -> cancelOrder() }
.setPositiveButton(getString(R.string.no)) { _, _ -> doNothing() }.show()
}
/**
Clear out the binding object when the view hierarchy associated with the fragment
is being removed
*/
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
CodePudding user response:
Use the below code when the radio button is checked
scrollView.post(new Runnable() {
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
CodePudding user response:
Try this:
fun scrollToViewBottom(scrollView: ScrollView, childView: View) {
val delay: Long = 500 //delay to let finish with possible modifications to ScrollView
scrollView.postDelayed({ scrollView.smoothScrollTo(0, childView.bottom) }, delay)
}
Call the function:
radioButton.setOnClickListener {
if (radioButton.isChecked) {
println("checked")
scrollToViewBottom(ScrollViewExplore, Mypage)
}
}