Search code examples
c#androidxamarinxamarin.android

Open navigation drawer by clicking normal button on Fragment


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);

Solution

  • 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