0

I have created a bottom menu bar using material , with five fragments. I have created xml file of each fragment along with their kt files. Also I have applied View Binding but the issue is when I use binding to refer to my buttons or text views from my fragments in the Main Activity.kt , I am Unable to do so. Below i have inflated the fragement in its kt file, have a look at it.

convert_fargment.xml This is the xml file of the feagment

    <?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:paddingHorizontal="32dp"
    tools:context=".fragments.ConvertFragment">

    <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="92dp"
            android:text="Converter"
            android:textColor="@color/black"
            android:textSize="35sp"
            app:layout_constraintBottom_toTopOf="@+id/guideline2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.564" />


        <TextView
            android:id="@+id/tvFrom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="From "
            app:layout_constraintStart_toStartOf="@+id/spFromCurrency"
            app:layout_constraintTop_toTopOf="@+id/guideline2" />

        <TextView
            android:id="@+id/tvTo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="To "
            app:layout_constraintStart_toStartOf="@+id/spToCurrency"
            app:layout_constraintTop_toTopOf="@+id/guideline2" />


        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/tilFrom"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:hint="Amount"
            app:layout_constraintBottom_toBottomOf="@+id/spFromCurrency"
            app:layout_constraintEnd_toStartOf="@+id/spFromCurrency"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/spFromCurrency">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/etFrom"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="numberDecimal"
                tools:layout_editor_absoluteX="-3dp"
                tools:layout_editor_absoluteY="153dp" />


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

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.32" />


        <Spinner
            android:id="@+id/spFromCurrency"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:entries="@array/currency_codes"
            android:padding="15dp"
            app:layout_constraintEnd_toStartOf="@+id/spToCurrency"
            app:layout_constraintTop_toBottomOf="@+id/tvFrom" />

        <Button
            android:id="@+id/btnConvert"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:text="Convert"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/spToCurrency" />

        <TextView
            android:id="@+id/tvResult"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:textColor="@color/black"
            app:layout_constraintBottom_toBottomOf="@+id/btnConvert"
            app:layout_constraintEnd_toStartOf="@+id/btnConvert"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/btnConvert"
            app:layout_constraintVertical_bias="0.586"
            tools:text="10 EUR = 15.0 USD" />

        <ProgressBar
            android:id="@+id/progresBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="@+id/btnConvert"
            app:layout_constraintEnd_toStartOf="@+id/btnConvert"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/btnConvert"
            tools:visibility="invisible" />


        <Spinner
            android:id="@+id/spToCurrency"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:entries="@array/currency_codes"
            android:padding="15dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tvTo" />

</androidx.constraintlayout.widget.ConstraintLayout>

ConvertFragment.kt This is the kt file of the fragment.

package com.example.currency.fragments


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.example.currency.R
import com.example.currency.databinding.FragmentConvertBinding
import com.example.currency.main.MainViewModel

class ConvertFragment : Fragment(R.layout.fragment_convert) {
    private val  viewModel: MainViewModel by viewModels()
    private  var _binding: FragmentConvertBinding? = null
   private val binding get() = _binding!!

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentConvertBinding.inflate(inflater,container,false)
        binding.btnConvert
        return binding.root
    }



    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

MainActivity.kt This is the Main activity file

package com.example.currency

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.Fragment
import com.example.currency.databinding.ActivityMainBinding
import com.example.currency.fragments.*
import com.example.currency.main.MainViewModel


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private val  viewModel: MainViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)




        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

        val convertFragment = ConvertFragment()
        val chartsFragment = ChartsFragment()
        val sendFragment = SendFragment()
        val favoritesFragment = FavoritesFragment()
        val moreFragment = MoreFragment()

        makeCurrentFragment(convertFragment)
        binding.bottomNavigation.setOnNavigationItemSelectedListener {
            when (it.itemId){
                R.id.ic_rupee -> makeCurrentFragment(convertFragment)
                R.id.ic_charts -> makeCurrentFragment(chartsFragment)
                R.id.ic_send_money -> makeCurrentFragment(sendFragment)
                R.id.ic_favorites -> makeCurrentFragment(favoritesFragment)
                R.id.ic_more -> makeCurrentFragment(moreFragment)

            }
            true


        }

    }

    private fun makeCurrentFragment(fragment: Fragment) =
            supportFragmentManager.beginTransaction().apply {
                replace(R.id.fl_wrapper, fragment)
                commit()
            }


    }

activity_main.xml This is the xml file of main activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity"
    android:background="#EEEEEE">

    <FrameLayout
        android:id="@+id/fl_wrapper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"/>


    <com.google.android.material.bottomnavigation.BottomNavigationView
       android:id="@+id/bottom_navigation"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:background="?android:attr/windowBackground"
       app:itemBackground="@color/purple_700"
       app:itemIconTint="@color/white"
       app:itemTextColor="@color/white"
       app:menu="@menu/nav_menu"/>

</RelativeLayout>
Ritesh Geddam
  • 63
  • 1
  • 3
  • Welcome to Stack Overflow! Maybe take a look at how to produce a [mre], you seem to be missing the details on what your actual issue is! – Henry Twist Mar 12 '21 at 18:35
  • Ok i got it! The issue is I am not being able to refer my button(in fragment) for on Click action in the main activity file. I have used view binding and guess messed up something. – Ritesh Geddam Mar 13 '21 at 19:17
  • Is the issue because the fragment is in bottom menu bar? So is it difficult for view binding then? – Ritesh Geddam Mar 13 '21 at 19:22
  • Where exactly in your code is the issue? I can make a guess at `binding.btnConvert` - this line? – Henry Twist Mar 13 '21 at 21:42
  • The issue is I am not being able to call the btnConvert to write its onClick actions in the MainActivity.kt. My question is have I messed up with view binding is that the reason for issue or is it something else? – Ritesh Geddam Mar 14 '21 at 10:42
  • I don't see any code in your sample `MainActivity` trying to do that? Anyway it would be quite strange to want to do something like that when it probably should be done in the fragment? – Henry Twist Mar 14 '21 at 11:27

0 Answers0