0

Want to do

Open navigation drawer by clicking normal button on Fragment as same as clicking navigation icon.

Before clicked button Image

After clicked button Image

Relation of code and xml

MainActivity.cs - activity_main.xml

EventFragment.cs - event_fragment.xml

FilterNavi.cs - filter_navigation.xml (I am not sure that which is the best for FilterNavi.cs Fragment or Activity.)

Code Error Message

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Code

EventFragment.cs

public class EventFragment : AndroidX.Fragment.App.FragmentNavigationView.IOnNavigationItemSelectedListener, TabLayout.IOnTabSelectedListener
{
    DrawerLayout drawer;
    Button btn_drawer ;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.Inflate(Resource.Layout.event_fragment, container, false);

        drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout2);

        btn_drawer = FindViewById<Button>(Resource.Id.btn_drawer);
        btn_drawer.Click += Btn_Drawer_Click;
    }

    private void Btn_Drawer_Click(object sender, EventArgs args)
    {
        drawer.OpenDrawer(GravityCompat.Start);
    }

filter_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
 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/drawer_layout2"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:fitsSystemWindows="true"
                                    tools:openDrawer="start">

  <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"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/test"
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:text="test"
            android:textSize="12dp"
            android:gravity="center">
        </TextView>
    </LinearLayout>

  </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

What I tried

I tried getView() and getActivity() but error told me that they don't exist in the current context .

m_drawer = (DrawerLayout) getActivity().findViewById(Resource.Id.drawer_layout2);

I also tried to get m_drawer and update view.

m_drawer was not null but navigation drawer didn't open.

        View viewFilter = inflater.Inflate(Resource.Layout.filter_navigation, container, false);
        m_drawer = viewFilter.FindViewById<DrawerLayout>(Resource.Id.drawer_layout2);
        View view = inflater.Inflate(Resource.Layout.event_fragment, container, false);
yuki yuki
  • 45
  • 6
  • https://stackoverflow.com/questions/19442841/how-to-open-navigation-drawer-on-button-click-in-main-fragment – NAGA VIKRAM Oct 21 '22 at 11:02
  • Thank you for comment. I tried samething but getActivity() makes an error maybe because C#. Do you know instead of getActivity() or other way? – yuki yuki Oct 21 '22 at 11:52
  • You can check this [link](https://stackoverflow.com/questions/43279971/get-current-activity-xamarin-android), It's about how to get activity. – Jianwei Sun - MSFT Oct 24 '22 at 06:24

1 Answers1

0

I couldn't open navigation drawer by clicking normal button on Fragment. However I found alternative way.

I use normal Activity which has translucent background on half right side. You can change the width of it by layout_weight. Where I don't want to make translucsent, I set white background.

styles.xml

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

Activity.cs

namespace SMP_Android
{
    // add Theme
    [Activity(Label = "FilterActivity", Theme = "@style/Theme.AppCompat.Translucent")]
public class FilterActivity : AppCompatActivity // select AppCompatActivity
    {
    }
}

activity.xml

<?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:background="aa000000"> // add background
yuki yuki
  • 45
  • 6