Forum Xamarin.Android

Drawable Menu new page

CastielTRCastielTR Member ✭✭
edited February 18 in Xamarin.Android

When click on drawable menu things, I want to the open new page, how when click on drawable menu items triggered and open new activity page?
My main Page:

My drawable menu:

I want to add new activity page:

Tagged:

Best Answer

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    @CastielTR said:
    When I want to open the new page, the toolbar disappears and cannot be returned.

    1. Make Activity1 inherit from AppCompatActivity .

    2. Enable the back button on Toolbar .

    3. Override the back button event .

    Sample code

     [Activity(Label = "Activity1")]
        public class Activity1 : AppCompatActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                // Create your application here
    
                View v = this.LayoutInflater.Inflate(Resource.Layout.MyLayout, null);
    
                SetContentView(v);
    
                SupportActionBar.SetDisplayHomeAsUpEnabled(true);  //show the back button 
            }
    
            public override bool OnOptionsItemSelected(IMenuItem item)
            {
                if (item.ItemId == Android.Resource.Id.Home)
                {
                    Finish();
                }
                return true;
            }
        }
    

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    1. Create a new Activity named MyActivity.cs .

    2. Reference the layout to the new Activity in OnCreate method .

    [Activity(Label = "MyActivity")]
        public class MyActivity: Activity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                // Create your application here
    
                View v = this.LayoutInflater.Inflate(Resource.Layout.page1, null);
    
                SetContentView(v);
            }
        }
    
    1. Start the new activity somewhere (click on drawable menu items)
    this.StartActivity(typeof(Activity1));
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • CastielTRCastielTR Member ✭✭

    There seems to be some trouble.

  • CastielTRCastielTR Member ✭✭

    When I want to open the new page, the toolbar disappears and cannot be returned.

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    @CastielTR said:
    When I want to open the new page, the toolbar disappears and cannot be returned.

    1. Make Activity1 inherit from AppCompatActivity .

    2. Enable the back button on Toolbar .

    3. Override the back button event .

    Sample code

     [Activity(Label = "Activity1")]
        public class Activity1 : AppCompatActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                // Create your application here
    
                View v = this.LayoutInflater.Inflate(Resource.Layout.MyLayout, null);
    
                SetContentView(v);
    
                SupportActionBar.SetDisplayHomeAsUpEnabled(true);  //show the back button 
            }
    
            public override bool OnOptionsItemSelected(IMenuItem item)
            {
                if (item.ItemId == Android.Resource.Id.Home)
                {
                    Finish();
                }
                return true;
            }
        }
    
Sign In or Register to comment.