Hello,
Xamarin android and net android are the same?
No. Xamarin android based on Mono.Android.
can we create fully functionality android app with .net android.P.S. I can't create bottom tabs with .net android
You can refer to following steps to create bottom tabs with .net android.
- Search and install
Xamarin.AndroidX.ViewPager2
andXamarin.Google.Android.Material
nuget packages. These packages could be used in the .Net Android as well. - Open your MainLayout, add
androidx.viewpager2.widget.ViewPager2
andcom.google.android.material.tabs.TabLayout
like following code.com.google.android.material.tabs.TabLayout
is show bottom tabs.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.google.android.material.tabs.TabLayout
android:layout_alignParentBottom="true"
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
- Open your
MainActivity.cs
, I usedAppCompatActivity
, you need to change the extend activity toAppCompatActivity
and addTheme = "@style/Theme.AppCompat"
for MainActivity.
For Viewpager2, you need to create FragmentStateAdapter and set ITabConfigurationStrategy as well, I add all of them in the following code.
using AndroidX.AppCompat.App;
using AndroidX.Lifecycle;
using AndroidX.ViewPager2.Adapter;
using AndroidX.ViewPager2.Widget;
using Google.Android.Material.Tabs;
using static Google.Android.Material.Tabs.TabLayoutMediator;
namespace AndroidApp1
{
[Activity(Label = "@string/app_name", MainLauncher = true, Theme = "@style/Theme.AppCompat")]
public class MainActivity : AppCompatActivity
{
public static List<string> fragmentTitles;
TabLayout tabLayout;
ViewPager2 pager;
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
tabLayout = FindViewById <TabLayout > (Resource.Id.tabLayout);
pager = FindViewById < ViewPager2 > (Resource.Id.pager);
CustomViewPager2Adapter adapter = new CustomViewPager2Adapter(this.SupportFragmentManager, this.Lifecycle);
fragmentTitles = new List< string> () {
"FragmentA", "FragmentB", "FragmentC"};
pager.Adapter = adapter;
adapter.NotifyDataSetChanged();
new TabLayoutMediator(tabLayout, pager, new CustomStrategy()).Attach();
}
}
public class CustomStrategy : Java.Lang.Object, ITabConfigurationStrategy
{
public void OnConfigureTab(TabLayout.Tab p0, int p1)
{
p0.SetText(MainActivity.fragmentTitles[p1]);
}
}
public class CustomViewPager2Adapter : FragmentStateAdapter
{
public CustomViewPager2Adapter(AndroidX.Fragment.App.FragmentManager fragmentManager, Lifecycle lifecycle) : base(fragmentManager, lifecycle)
{
}
private AndroidX.Fragment.App.Fragment fragment = new AndroidX.Fragment.App.Fragment();
public override int ItemCount => 3;
public override AndroidX.Fragment.App.Fragment CreateFragment(int position)
{
switch (position)
{
case 0:
fragment = new ViewPage2Fragment();
break;
case 1:
fragment = new ViewPage2FragmentB();
break;
case 2:
fragment = new ViewPage2FragmentC();
break;
}
return fragment;
}
}
}
- I create three Fragments for testing, here is a simple Frament, you can create other Fragments.
Layout1
is a Android layout template and add textview.
public class ViewPage2Fragment : AndroidX.Fragment.App.Fragment
{
public override View? OnCreateView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState)
{
return inflater.Inflate(Resource.Layout.Layout1, container, false); ;
}
public override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.