简体   繁体   中英

How To Open Menu In Activity From Fragment

I am making app in which I have added fragment in activity. Now I have added search bar in fragment section, so I want to open navigation drawer(which is in activty) and action menu) when clicked on Image.

Fragment Looks Like This - image here

Fragment XML -

 <?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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:layout_width="match_parent" android:id="@+id/swiper" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.cardview.widget.CardView app:cardUseCompatPadding="true" android:layout_margin="8dp" app:cardCornerRadius="8dp" android:layout_width="match_parent" android:layout_height="60dp"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginStart="8dp" android:src="@drawable/ic_menu_black_24dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/edit_search" android:layout_width="297dp" android:layout_height="match_parent" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:cursorVisible="false" android:elevation="5dp" android:fontFamily="@font/robolightfont" android:gravity="center" android:hint="Search" android:inputType="text" android:minWidth="100dp" android:textColor="@color/white" android:textColorHint="@color/white" android:textStyle="normal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="@+id/imgmore" app:layout_constraintLeft_toRightOf="@+id/imageView2" app:layout_constraintStart_toEndOf="@+id/imageView2" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/imgmore" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:src="@drawable/ic_more_vert_black_24dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <!-- <EditText android:padding="10dp" android:background="@drawable/rounddesign" android:layout_margin="12dp" android:elevation="5dp" android:fontFamily="@font/robotolight" android:id="@+id/edit_search" android:textColorHint="@color/white" android:textColor="@color/white" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:gravity="center" android:textStyle="normal" android:cursorVisible="false" style="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle" android:hint="Search" android:visibility="visible"/>--> <androidx.recyclerview.widget.RecyclerView android:id="@+id/list" android:background="@color/mainback" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> </LinearLayout> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </RelativeLayout> 

MAIN ACTIVTY XML

 <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".ui.NewMainActivity" tools:openDrawer="start"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> <ImageView android:id="@+id/logimage" android:layout_width="55dp" android:layout_height="55dp" android:visibility="gone" android:background="@drawable/ic_access_time_black_24dp" android:layout_margin="20dp" android:layout_gravity="center" /> <TextView android:id="@+id/logtxt" android:visibility="gone" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:layout_margin="10dp" android:layout_gravity="center" android:gravity="center" android:fontFamily="@font/robolightfont" android:layout_marginTop="30dp" android:textSize="20dp" android:text="It Seems, No Notification Has Been Logged Yet !\\n\\nCome Back Once You Start Receiving Notification"/> <Button android:id="@+id/button_check" android:visibility="gone" android:gravity="center" android:textColor="@color/mainback" android:layout_margin="14dp" android:fontFamily="@font/robofont" android:background="@drawable/roundbtndesign" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Retry"/> <ImageView android:id="@+id/acimage" android:layout_width="80dp" android:layout_gravity="center" android:layout_margin="30dp" android:layout_height="80dp" android:visibility="gone" android:background="@drawable/ic_tag_faces_black_24dp"/> <TextView android:id="@+id/actitle" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Notification Access" android:layout_gravity="center" android:fontFamily="@font/fontmossemi" android:layout_margin="10dp" android:visibility="gone" android:gravity="center"/> <TextView android:id="@+id/acsubtext" android:visibility="gone" android:textSize="15sp" android:textColor="@color/white" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="To Get Started, Enable Notification Access For Notification Log" android:layout_gravity="center" android:fontFamily="@font/robolightfont" android:layout_margin="10dp" android:gravity="center"/> <Button android:id="@+id/button_allow" android:visibility="gone" android:gravity="center" android:background="@drawable/roundbtndesign" android:textColor="@color/mainback" android:layout_margin="24dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Allow Permission"/> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> </LinearLayout> <com.google.android.material.navigation.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header" app:menu="@menu/drawer" /> </androidx.drawerlayout.widget.DrawerLayout> 

Now how can I open both action menu and drawer menu from fragment.java code. Please Help in this out

Create a public method in your activity which will open the navigation drawer and call that method from your fragment.

Activity Code:

public void openDrawer(){
    drawer.openDrawer(Gravity.LEFT);
}

In your fragment image on click code:

imageView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        ((MainActivity)getActivity()).openDrawer();
    }
}

Hope this will work for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM