JohnAnders
JohnAnders

Reputation: 79

How hide/show icon on toolbar when opening some fragments

I have few fragments and one activity. The app contains toolbar which is always visible. There is icon on my toolbar. And i need to hide this icon when user opens 2,4,5 fragments and show this icon when users open 1 and 3 fragment. I don't need all code for this logic, I need advice how can I implement it and where add some logic for this behavior

Upvotes: 2

Views: 1829

Answers (3)

mohosyny
mohosyny

Reputation: 1052

Step 1 : create an interface FragmentListener with one method :

public interface MyFragmentListener {
    void onChangeToolbarImage(boolean show);
}

Step 2 : implement in YourActivity :

ImageView toolarImage= findViewById(R.id.toolbarimage)()///

     @Override
    public void onChangeToolbarImage(boolean show) {
       if()
        { //check your imageView is Visible or not

        toolbarImage.setVisibility(show); //change your ImageView's visibility
       }
    }

Step 3 : in each fragment you need get instance from interface :

private MyFragmentListener fragmentListener;

Step 4 : override onAtach in your Fragment :

  @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof MyFragmentListener)
            fragmentListener = (MyFragmentListener) context;

        fragmentListener.onChangeToolbarTitleImage(true or false);
    }

Upvotes: 1

Hritik Gupta
Hritik Gupta

Reputation: 632

Create a companion object or static variable if you are using java.

class Util {companion object { lateinit var toolbarImg: ImageView }}  

Inside your Main Activity onCreate initialize your toolbar and imageview

toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
Util.toolbarImg = toolbar.findViewById(R.id.cartImg)

XML

    <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <ImageView
            android:id="@+id/cartImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:visibility="visible"
             />

    </androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>

Now all you need to do is control the visibility of this ImageView. On Fragment transaction

To Hide

if(Util.toolbarImg.visibility == View.VISIBLE){
Util.toolbarImg.visibility = View.GONE }

Upvotes: 1

ror
ror

Reputation: 3500

The following is true assuming you are using jetpack's navigation and single activity:

Add destination change listener to your main nav controller inside your activity (addOnDestinationChangedListener, the interface is NavController.OnDestinationChangedListener). Inside the listener, you could check for destination.id in onDestinationChanged implementation. Actually, you could create two sets like this

private val twoFourFiveDestinations =
        setOf(R.id.two, R.id.four, R.id.five) 
private val oneThreeDestinations =
        setOf(R.id.one, R.id.three)

just to make check like this if(twoFourFiveDestinations.contains(destination.id) ... and manage your icon visibility accordingly, it will make life easier.

Alternative solution would be to hand over icon management to fragments. You could define some interface for comm with activity, and manage toolbar icon when respective fragment is up and running. But you'd need to do that in every fragment of your question.

Upvotes: 2

Related Questions