Forums - C# Corner

Forum guidelines
Qing Ryder

Qing Ryder

  • 1.5k
  • 196
  • 2.3k

Xamarin: Error While adding Spinner/Button in Drawer Layout

Apr 16 2018 1:53 PM
Hello Everyone,
Please i want to add Spinner, Text view and Button in  Drawer Layout of my Main.axml
i tried putting it in the FrameLayout, but it wouldnt.
i created a Relative Layout and then it jampacks at the header and wont leave even after navigating to a different page.
 
 
s
 From the screenshot, i have navigated to another page, but the main is still showing
 
 
 
 
 
 
 
Here is my Main.axml code
  1. "1.0" encoding="utf-8"?>  
  2. "http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:focusableInTouchMode="true"  
  7.     tools:context=".MainActivity">  
  8.     "http://schemas.android.com/apk/res-auto"  
  9.         android:id="@+id/drawer_layout"  
  10.         android:layout_height="match_parent"  
  11.         android:layout_width="fill_parent"  
  12.         android:fitsSystemWindows="true">  
  13.         
  14.             android:id="@+id/framelayout"  
  15.             android:layout_width="match_parent"  
  16.             android:layout_height="match_parent" />  
  17.         
  18.             layout="@layout/toolbar" />  
  19.         
  20.             android:layout_width="match_parent"  
  21.             android:layout_height="match_parent"  
  22.             android:id="@+id/rl">  
  23.             
  24.                 android:text="App/Home/Diagnose"  
  25.                 android:textAppearance="?android:attr/textAppearanceSmall"  
  26.                 android:layout_width="match_parent"  
  27.                 android:layout_height="wrap_content"  
  28.                 android:id="@+id/textView1" />  
  29.             
  30.                 android:layout_width="fill_parent"  
  31.                 android:layout_height="wrap_content"  
  32.                 android:layout_marginTop="10dip"  
  33.                 android:text="@string/component_prompt" />  
  34.             
  35.                 android:id="@+id/spinner"  
  36.                 android:layout_width="fill_parent"  
  37.                 android:layout_height="wrap_content"  
  38.                 android:prompt="@string/component_prompt" />  
  39.             
  40.                 android:text="Final Analysis"  
  41.                 android:textAppearance="?android:attr/textAppearanceMedium"  
  42.                 android:layout_width="match_parent"  
  43.                 android:layout_height="wrap_content"  
  44.                 android:id="@+id/textView3"  
  45.                 android:layout_marginTop="45.0dp" />  
  46.             
  47.                 android:layout_width="match_parent"  
  48.                 android:layout_height="43.5dp"  
  49.                 android:id="@+id/spinner2" />  
  50.             
  51.                 android:text="Deep Scan"  
  52.                 android:layout_width="match_parent"  
  53.                 android:layout_height="wrap_content"  
  54.                 android:id="@+id/checkBox1"  
  55.                 android:layout_marginTop="42.0dp" />  
  56.             
  57.                 android:text="ANALYSE..."  
  58.                 android:layout_width="192.0dp"  
  59.                 android:layout_height="wrap_content"  
  60.                 android:id="@+id/button1"  
  61.                 android:layout_marginTop="25.5dp" />  
  62.           
  63.         
  64.             android:id="@+id/nav_view"  
  65.             android:layout_height="match_parent"  
  66.             android:layout_width="300dp"  
  67.             android:layout_gravity="start"  
  68.             android:fitsSystemWindows="true"  
  69.             app:headerLayout="@layout/nav_header" />  
  70.       
  71.  


 
and here is my Main activity Code
  1. {  
  2.     [Activity(Label = "AutoDoctor Car OBD", Theme = "@style/Theme.DesignDemo",Icon = "@drawable/icon")]  
  3.     public class MainActivity : AppCompatActivity  
  4.     {  
  5.        
  6.         DrawerLayout drawerLayout;  
  7.         NavigationView navigationView;  
  8.         protected override void OnCreate(Bundle bundle)  
  9.         {  
  10.             base.OnCreate(bundle);  
  11.   
  12.             // Set our view from the "Main" layout resource  
  13.             SetContentView(Resource.Layout.Main);  
  14.               
  15.             /*FragmentTransaction transaction = this.FragmentManager.BeginTransaction(); 
  16.             diagnose home = new diagnose(); 
  17.             transaction.Add(Resource.Id.framelayout, home).Commit();*/  
  18.               
  19.             Spinner spinner = FindViewById(Resource.Id.spinner);  
  20.   
  21.             spinner.ItemSelected += new EventHandler(spinner_ItemSelected);  
  22.             var adapter = ArrayAdapter.CreateFromResource(  
  23.                     this, Resource.Array.component_array, Android.Resource.Layout.SimpleSpinnerItem);  
  24.   
  25.             adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);  
  26.             spinner.Adapter = adapter;  
  27.         
  28.             drawerLayout = FindViewById(Resource.Id.drawer_layout);  
  29.   
  30.   
  31.             // Create ActionBarDrawerToggle button and add it to the toolbar  
  32.             var toolbar = FindViewById(Resource.Id.toolbar);  
  33.             SetSupportActionBar(toolbar);  
  34.   
  35.             var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.drawer_open, Resource.String.drawer_close);  
  36.             drawerLayout.SetDrawerListener(drawerToggle);  
  37.             drawerToggle.SyncState();  
  38.   
  39.             navigationView = FindViewById(Resource.Id.nav_view);  
  40.             setupDrawerContent(navigationView);  
  41.             var naviview = FindViewById(Resource.Id.nav_view);  
  42.             naviview.NavigationItemSelected += (sender, e) =>  
  43.             {  
  44.                 e.MenuItem.SetChecked(true);  
  45.                 FragmentTransaction transaction1 = this.FragmentManager.BeginTransaction();  
  46.                 if (e.MenuItem.ItemId == Resource.Id.home)  
  47.                 {  
  48.                    diagnose  dia = new diagnose();  
  49.                     // The fragment will have the ID of Resource.Id.fragment_container.  
  50.                     transaction1.Replace(Resource.Id.framelayout, dia).AddToBackStack(null).Commit();  
  51.                 }  
  52.                else  if (e.MenuItem.ItemId == Resource.Id.about)   
  53.                 {  
  54.                     about abt = new about();  
  55.                     // The fragment will have the ID of Resource.Id.fragment_container.  
  56.                     transaction1.Replace(Resource.Id.framelayout, abt).AddToBackStack(null).Commit(); ;  
  57.                 }  
  58.                 else if (e.MenuItem.ItemId == Resource.Id.settings)  
  59.                 {  
  60.                     settings video = new settings();  
  61.                     // The fragment will have the ID of Resource.Id.fragment_container.  
  62.                     transaction1.Replace(Resource.Id.framelayout, video).AddToBackStack(null).Commit(); ;  
  63.                 }  
  64.                 else if (e.MenuItem.ItemId == Resource.Id.dev)  
  65.                 {  
  66.                     dev devo = new dev();  
  67.                     // The fragment will have the ID of Resource.Id.fragment_container.  
  68.                     transaction1.Replace(Resource.Id.framelayout, devo).AddToBackStack(null).Commit(); ;  
  69.                 }  
  70.                 //...  
  71.                 drawerLayout.CloseDrawers();  
  72.             };  
  73.         }  
  74.   
  75.         public void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)  
  76.         {  
  77.             Spinner spinner = (Spinner)sender;  
  78.             string toast = string.Format("The Selected Component is {0}", spinner.GetItemAtPosition(e.Position));  
  79.             Toast.MakeText(this, toast, ToastLength.Long).Show();  
  80.         }  
  81.  void setupDrawerContent(NavigationView navigationView)  
  82.         {  
  83.             navigationView.NavigationItemSelected += (sender, e) => {  
  84.                 e.MenuItem.SetChecked(true);  
  85.                 drawerLayout.CloseDrawers();  
  86.             };  
  87.         }  
  88.   
  89.         public override bool OnCreateOptionsMenu(IMenu menu)  
  90.         {  
  91.               
  92.             navigationView.InflateMenu(Resource.Menu.nav_menu);  
  93.             return true;  
  94.   
  95.         }  
  96.       
  97.         }  

 
 
 Someone please Help me out

Answers (16)