Search code examples
androidandroid-fragments

Android Java toolbar get icon click from individual fragments


I have a Bottom Navigation Toolbar and a Toolbar in my MainActivity, I also have a FrameLayout that switches from one Fragment to another. This is my MainActivity 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">
        
            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
        
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?attr/colorPrimary"
                    android:minHeight="?attr/actionBarSize"
                    android:theme="?attr/actionBarTheme"
                    tools:layout_editor_absoluteX="0dp"
                    tools:layout_editor_absoluteY="0dp" >
        
                    <ImageView
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:src="@drawable/ic_logo"
        
                        >
                    </ImageView>
        
                    <TextView
                        android:layout_marginLeft="20dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="PollIt"
                        android:textColor="@color/white"
                        android:fontFamily="@font/login_logo_font"
                        >
        
                    </TextView>
        
                    <ImageView
                        android:id="@+id/refreshData"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:src="@drawable/ic_logo"
                        android:layout_gravity="right"
                        android:layout_marginRight="25dp"
                        >
                    </ImageView>
        
                </androidx.appcompat.widget.Toolbar>
        
        
            </com.google.android.material.appbar.AppBarLayout>
        
            <FrameLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="56dp"
                android:text="Fragment"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottomNavigationView"
                android:layout_width="match_parent"
                android:layout_height="75dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:menu="@menu/bottom_navigation"/>
        
        </androidx.constraintlayout.widget.ConstraintLayout>

So in the toolbar I have an ImageView with the id "refreshData", if I'm in Fragment 2, how do I get the click event when I click "refreshData" so that I can refresh the data within different Fragments?

Update


I tried what was suggested, set this code in onCreateView():

    MainActivity main = (MainActivity) getActivity();
    main.imageRefresh.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            Toast.makeText(getActivity(), "Follow News", Toast.LENGTH_SHORT);
        }

    });

But the click wont fire.

I put the debugger on and the breakpoint is hitting but the Toast Message never appears, not sure but maybe this is another question.


Solution

  • Its not easy but we can achive by a trick, also note that it will be memory sensitive.

    first in your mainActivity create a public static variavle for the ImageView with the id "refreshData".

    val refrishView = finViewById(R.Id.refreshData);
    

    initialize it in main activity.

    now the trick is In the fargment/framelayout class access the variable by

    Mainactivity.yourVariable.setOnclickListner();
    

    thats it. Let me know if you dont understand/