How to enable/disable fragment button, within main activity in Kotlin

34 Views Asked by At

There are several answers to questions similar to this one, but none in Kotlin.

I have a main activity with a button and two fragments. In the first fragment there is a navigation button that launches the second fragment when clicked.

What I want to do is to be able to enable/disable the navigation button from the main activity.

Is it possible at all?

Here is the main activity 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"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/demo_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="409dp"
        android:layout_height="729dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation_graph" />

    <Button
        android:id="@+id/activateButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="92dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

First fragment 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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FirstFragment">

    <EditText
        android:id="@+id/userText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="164dp"
        android:ems="10"
        android:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/navButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Navigate"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="373dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

and Second fragment 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:id="@+id/constraintLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondFragment" >

    <TextView
        android:id="@+id/argText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

And here is the main activity code:

package com.ebookfrenzy.navigationdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.net.Uri
import com.ebookfrenzy.navigationdemo.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity(),
    SecondFragment.OnFragmentInteractionListener {
    private lateinit var binding: ActivityMainBinding

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

    override fun onFragmentInteraction(uri: Uri) {
    }

    private fun listenerSetup() {

        binding.activateButton.setOnClickListener{

            // here I want to change the isEnabled property of the fragment button navButton
        }
    }
}
1

There are 1 best solutions below

3
Tolga Erbaş On

You can set a boolean value to the button in the activity, like enable/disable, then save its value when you click button, true or false. Since you are using Navigation, you can pass the boolean value as arguments to the first fragment, like this

<fragment
    android:id="@+id/firstFragment"
    android:name="com.example.FirstFragment"
    android:label="First Fragment">
    <argument
        android:name="isClicked"
        app:argType="boolean" />
</fragment>

So when you click button in the activity, you can save it to the boolean variable and then pass its last value to the first fragment.