Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

Exposed Dropdown Menu On Bottom Sheet Fragment - Android Studio

I'm currently developing a BottomSheetDialogFragment and inside it 2 Exposed Dropdown Menu from this tutorial https://blog.usejournal.com/there-is-no-material-design-spinner-for-android-3261b7c77da8 and https://material.io/components/text-fields/android#using-text-fields. The problem is when the user clicked the AutoCompleteTextView for the first time, it shifts the dropdown. Picture Of The Problem

Code for the InsertTopUpFragment, I use view binding

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/til_coin"
    android:hint="Harga"
    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/label_coin"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.MaterialAutoCompleteTextView
        android:id="@+id/act_harga"
        android:editable="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"
        tools:ignore="Deprecated" />

</com.google.android.material.textfield.TextInputLayout>

This how i call the layout, i'm using navigation component

navHostFragment = supportFragmentManager.findFragmentById(binding.navHostFragment.id) as NavHostFragment
NavigationUI.setupWithNavController(binding.bottomNavigationView, navHostFragment.navController)

binding.btnFab.setOnClickListener {
    navHostFragment.navController.navigate(R.id.insertTopUpFragment)
}

This is for nav graph

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/siswa_navigation_graph"
    app:startDestination="@id/home">

    **
    //omitted
    **

    <dialog
        android:id="@+id/insertTopUpFragment"
        android:name="com.example.tanyapr.view.user.siswa.tools.InsertTopUpFragment"
        android:label="insert_top_up_fragment"
        tools:layout="@layout/insert_top_up_fragment" />

</navigation>

This is the BottomSheetDialogFragment

class InsertTopUpFragment : BottomSheetDialogFragment() {
    private lateinit var binding: InsertTopUpFragmentBinding
    
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = InsertTopUpFragmentBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val coin = listOf(
            Coin("5000", "Rp. 5.000"),
            Coin("25000", "Rp. 25.000"),
            Coin("50000", "Rp. 50.000"),
            Coin("75000", "Rp. 75.000"),
            Coin("100000", "Rp. 100.000")
        )

        val actAdapter2 = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, coin)
        binding.actHarga.apply {
            setAdapter(actAdapter2)
            setOnItemClickListener { _, _, position, _ ->
                val value = actAdapter2.getItem(position) as Coin
                binding.actHarga.setText(value.harga, false)
            }
        }
    }
}

What I've tried so far

  1. Use MaterialAutoCompleteTextView instead AutoCompleteTextView, problem still persist
  2. Change nav graph from dialog to fragment, yes it fixes the problem but it's not BottomSheetDialogFragment anymore it just a normal fragment

What I have not tried so far

  1. Refactor BottomSheetDialogFragment with onCreateDialog

Question: how to use ExposedDropdownMenu On BottomSheetFragment without it shift the Dropdown?

question from:https://stackoverflow.com/questions/65851177/exposed-dropdown-menu-on-bottom-sheet-fragment-android-studio

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...