- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:minWidth="25px"
- android:id="@+id/linearlayout2"
- android:minHeight="25px">
- <TextView
- android:text="Main Text"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView1"
- android:gravity="center" />
- <ListView
- android:minWidth="25px"
- android:minHeight="25px"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/listview" />
- </LinearLayout>
- using System;
- using Android.App;
- using Android.Content;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
- using Android.Graphics;
- using System.Collections.Generic;
- namespace SatyapraashTabs
- {
- [Activity(Label = "SatyapraashTabs", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- private TextView textView;
- private ListView listnames1;
- private ListView listnames2;
- private ListView listnames3;
- private ListView listnames4;
- private List<string> itemlist1;
- private List<string> itemlist2;
- private List<string> itemlist3;
- private List<string> itemlist4;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
- textView = FindViewById<TextView>(Resource.Id.textView1);
- ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
- // Add the tabs to Action Bar
- AddTab(Resource.Drawable.article, "Article");
- AddTab(Resource.Drawable.blog, "Blog");
- AddTab(Resource.Drawable.news, "News");
- AddTab(Resource.Drawable.award, "Awards");
- }
- private void AddTab(int tabimg, string tabText)
- {
- ActionBar.Tab tab = ActionBar.NewTab();
- tab.SetText(tabText);
- tab.SetIcon(tabimg); //This is for icon file.
- tab.TabSelected += OnTabSelected;
- ActionBar.AddTab(tab);
- }
- private void OnTabSelected(object sender, ActionBar.TabEventArgs args)
- {
- var CurrentTab = (ActionBar.Tab)sender;
- if (CurrentTab.Position == 0)
- {
- // var textView = FindViewById<TextView>(Resource.Id.textView1);
- textView.Text = "Article Tab Selected";
- textView.SetBackgroundColor(Color.Red);
- listnames1 = FindViewById<ListView>(Resource.Id.listview);
- //listnames.SetBackgroundColor(Color.MidnightBlue);
- LinearLayout mainLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout2);
- mainLayout.SetBackgroundColor(Color.RoyalBlue);
- itemlist1 = new List<string>();
- itemlist1.Add("Create App Using Xamarin.Android Using C# - Part Four");
- itemlist1.Add("Introduction To SQL Operations Studio And Connecting It With SQL Server");
- itemlist1.Add("Client Side Routing Using Angular In MVC");
- itemlist1.Add("Partial View In ASP.NET MVC Using Entity Framework");
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemlist1);
- listnames1.Adapter = adapter;
- listnames1.ItemClick += Listnames1_ItemClick;
- }
- else if (CurrentTab.Position == 1)
- {
- //var textView = FindViewById<TextView>(Resource.Id.textView1);
- textView.Text = "Blog Tab Selected";
- textView.SetBackgroundColor(Color.OrangeRed);
- listnames2 = FindViewById<ListView>(Resource.Id.listview);
- LinearLayout mainLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout2);
- mainLayout.SetBackgroundColor(Color.Blue);
- itemlist2 = new List<string>();
- itemlist2.Add("Boost Your Career Through C# Corner");
- itemlist2.Add("Google Map GPS Locator Using MVC And BootBox - Part Thirteen");
- itemlist2.Add("Google Maps- Route Direction Information Between Source And Destination");
- itemlist2.Add("Google Maps Travelling Mode Using Bootstrap In ASP.NET MVC - PART Seven");
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemlist2);
- listnames2.Adapter = adapter;
- listnames2.ItemClick += Listnames2_ItemClick;
- }
- else if (CurrentTab.Position == 2)
- {
- var textView = FindViewById<TextView>(Resource.Id.textView1);
- textView.Text = "News Tab Selected";
- textView.SetBackgroundColor(Color.RoyalBlue);
- listnames3 = FindViewById<ListView>(Resource.Id.listview);
- LinearLayout mainLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout2);
- mainLayout.SetBackgroundColor(Color.Orchid);
- itemlist3 = new List<string>();
- itemlist3.Add("C# Corner Android App v0.5.7 Released");
- itemlist3.Add("Google Releases ARCore Developer Preview 2");
- itemlist3.Add("Artificial Intelligence Microsoft Launches AI Platform And AI School Silently");
- itemlist3.Add("Amazon Launches A New Test Simulator (Beta) for Alexa Skills");
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemlist3);
- listnames3.Adapter = adapter;
- listnames3.ItemClick += Listnames3_ItemClick;
- }
- else if (CurrentTab.Position == 3)
- {
- var textView = FindViewById<TextView>(Resource.Id.textView1);
- textView.Text = "Awards Tab Selected";
- textView.SetBackgroundColor(Color.Blue);
- listnames4 = FindViewById<ListView>(Resource.Id.listview);
- LinearLayout mainLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout2);
- mainLayout.SetBackgroundColor(Color.Olive);
- itemlist4 = new List<string>();
- itemlist4.Add("The Month of April 2017 Winners Announced");
- itemlist4.Add("2017 First Half of the Year MVPs Announced ");
- itemlist4.Add("Microsoft Announces New AI MVP Award Category");
- itemlist4.Add("Xamarin MVPs Join Microsoft MVP Program");
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemlist4);
- listnames4.Adapter = adapter;
- listnames4.ItemClick += Listnames4_ItemClick;
- }
- }
- private void Listnames1_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
- {
- Toast.MakeText(this, e.Position.ToString(), ToastLength.Long).Show();
- Toast.MakeText(this, "You Clicked Article at " + itemlist1[e.Position], ToastLength.Long).Show();
- }
- private void Listnames2_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
- {
- Toast.MakeText(this, e.Position.ToString(), ToastLength.Long).Show();
- Toast.MakeText(this, "You Clicked Blog at " + itemlist2[e.Position], ToastLength.Long).Show();
- }
- private void Listnames3_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
- {
- Toast.MakeText(this, e.Position.ToString(), ToastLength.Long).Show();
- Toast.MakeText(this, "You Clicked News at " + itemlist3[e.Position], ToastLength.Long).Show();
- }
- private void Listnames4_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
- {
- Toast.MakeText(this, e.Position.ToString(), ToastLength.Long).Show();
- Toast.MakeText(this, "You Clicked Awards at " + itemlist4[e.Position], ToastLength.Long).Show();
- }
- }
- }
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
- textView = FindViewById<TextView>(Resource.Id.textView1);
- ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
- // Add the tabs to Action Bar
- AddTab(Resource.Drawable.article, "Article");
- AddTab(Resource.Drawable.blog, "Blog");
- AddTab(Resource.Drawable.news, "News");
- AddTab(Resource.Drawable.award, "Awards");
- }
- private void AddTab(int tabimg, string tabText)
- {
- ActionBar.Tab tab = ActionBar.NewTab();
- tab.SetText(tabText);
- tab.SetIcon(tabimg); //This is for icon file.
- tab.TabSelected += OnTabSelected;
- ActionBar.AddTab(tab);
- }
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
- textView = FindViewById<TextView>(Resource.Id.textView1);
- ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
- // Add the tabs to Action Bar
- AddTab(Resource.Drawable.article);
- AddTab(Resource.Drawable.blog);
- AddTab(Resource.Drawable.news);
- AddTab(Resource.Drawable.award);
- }
- private void AddTab(int tabimg)
- {
- ActionBar.Tab tab = ActionBar.NewTab();
- tab.SetIcon(tabimg); //This is for icon file.
- tab.TabSelected += OnTabSelected;
- ActionBar.AddTab(tab);
- }
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
- textView = FindViewById<TextView>(Resource.Id.textView1);
- ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
- // Add the tabs to Action Bar
- AddTab("Article");
- AddTab("Blog");
- AddTab("News");
- AddTab("Awards");
- }
- private void AddTab(string tabText)
- {
- ActionBar.Tab tab = ActionBar.NewTab();
- tab.SetText(tabText);
- tab.TabSelected += OnTabSelected;
- ActionBar.AddTab(tab);
- }
- private TextView textView;
- private ListView listnames1;
- private ListView listnames2;
- private ListView listnames3;
- private ListView listnames4;
- private List<string> itemlist1;
- private List<string> itemlist2;
- private List<string> itemlist3;
- private List<string> itemlist4;
- ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
- // Add the tabs to Action Bar
- AddTab("Article");
- AddTab("Blog");
- AddTab("News");
- AddTab("Awards");
- SetContentView(Resource.Layout.Main);
- base.OnCreate(bundle);
- textView = FindViewById<TextView>(Resource.Id.textView1);
- ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
- Standard
- Lists
- Tabs
- private void AddTab(string tabText)
- {
- ActionBar.Tab tab = ActionBar.NewTab();
- tab.SetText(tabText);
- tab.TabSelected += OnTabSelected;
- ActionBar.AddTab(tab);
- }
- private void OnTabSelected(object sender, ActionBar.TabEventArgs args)
- {
- var CurrentTab = (ActionBar.Tab)sender;
- if (CurrentTab.Position == 0)
- {
- // var textView = FindViewById<TextView>(Resource.Id.textView1);
- textView.Text = "Article Tab Selected";
- textView.SetBackgroundColor(Color.Red);
- listnames1 = FindViewById<ListView>(Resource.Id.listview);
- //listnames.SetBackgroundColor(Color.MidnightBlue);
- LinearLayout mainLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout2);
- mainLayout.SetBackgroundColor(Color.RoyalBlue);
- itemlist1 = new List<string>();
- itemlist1.Add("Create App Using Xamarin.Android Using C# - Part Four");
- itemlist1.Add("Introduction To SQL Operations Studio And Connecting It With SQL Server");
- itemlist1.Add("Client Side Routing Using Angular In MVC");
- itemlist1.Add("Partial View In ASP.NET MVC Using Entity Framework");
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemlist1);
- listnames1.Adapter = adapter;
- listnames1.ItemClick += Listnames1_ItemClick;
- }
- else if (CurrentTab.Position == 1)
- {
- //var textView = FindViewById<TextView>(Resource.Id.textView1);
- textView.Text = "Blog Tab Selected";
- textView.SetBackgroundColor(Color.OrangeRed);
- listnames2 = FindViewById<ListView>(Resource.Id.listview);
- LinearLayout mainLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout2);
- mainLayout.SetBackgroundColor(Color.Blue);
- itemlist2 = new List<string>();
- itemlist2.Add("Boost Your Career Through C# Corner");
- itemlist2.Add("Google Map GPS Locator Using MVC And BootBox - Part Thirteen");
- itemlist2.Add("Google Maps- Route Direction Information Between Source And Destination");
- itemlist2.Add("Google Maps Travelling Mode Using Bootstrap In ASP.NET MVC - PART Seven");
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemlist2);
- listnames2.Adapter = adapter;
- listnames2.ItemClick += Listnames2_ItemClick;
- }
- else if (CurrentTab.Position == 2)
- {
- var textView = FindViewById<TextView>(Resource.Id.textView1);
- textView.Text = "News Tab Selected";
- textView.SetBackgroundColor(Color.RoyalBlue);
- listnames3 = FindViewById<ListView>(Resource.Id.listview);
- LinearLayout mainLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout2);
- mainLayout.SetBackgroundColor(Color.Orchid);
- itemlist3 = new List<string>();
- itemlist3.Add("C# Corner Android App v0.5.7 Released");
- itemlist3.Add("Google Releases ARCore Developer Preview 2");
- itemlist3.Add("Artificial Intelligence Microsoft Launches AI Platform And AI School Silently");
- itemlist3.Add("Amazon Launches A New Test Simulator (Beta) for Alexa Skills");
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemlist3);
- listnames3.Adapter = adapter;
- listnames3.ItemClick += Listnames3_ItemClick;
- }
- else if (CurrentTab.Position == 3)
- {
- var textView = FindViewById<TextView>(Resource.Id.textView1);
- textView.Text = "Awards Tab Selected";
- textView.SetBackgroundColor(Color.Blue);
- listnames4 = FindViewById<ListView>(Resource.Id.listview);
- LinearLayout mainLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout2);
- mainLayout.SetBackgroundColor(Color.Olive);
- itemlist4 = new List<string>();
- itemlist4.Add("The Month of April 2017 Winners Announced");
- itemlist4.Add("2017 First Half of the Year MVPs Announced ");
- itemlist4.Add("Microsoft Announces New AI MVP Award Category");
- itemlist4.Add("Xamarin MVPs Join Microsoft MVP Program");
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemlist4);
- listnames4.Adapter = adapter;
- listnames4.ItemClick += Listnames4_ItemClick;
- }
- }
- private void Listnames1_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
- {
- Toast.MakeText(this, e.Position.ToString(), ToastLength.Long).Show();
- Toast.MakeText(this, "You Clicked Article at " + itemlist1[e.Position], ToastLength.Long).Show();
- }
- private void Listnames2_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
- {
- Toast.MakeText(this, e.Position.ToString(), ToastLength.Long).Show();
- Toast.MakeText(this, "You Clicked Blog at " + itemlist2[e.Position], ToastLength.Long).Show();
- }
- private void Listnames3_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
- {
- Toast.MakeText(this, e.Position.ToString(), ToastLength.Long).Show();
- Toast.MakeText(this, "You Clicked News at " + itemlist3[e.Position], ToastLength.Long).Show();
- }
- private void Listnames4_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
- {
- Toast.MakeText(this, e.Position.ToString(), ToastLength.Long).Show();
- Toast.MakeText(this, "You Clicked Awards at " + itemlist4[e.Position], ToastLength.Long).Show();
- }
- var CurrentTab = (ActionBar.Tab)sender;
- if (CurrentTab.Position == 0)
- {
- // var textView = FindViewById<TextView>(Resource.Id.textView1);
- textView.Text = "Article Tab Selected";
- textView.SetBackgroundColor(Color.Red);
- listnames1 = FindViewById<ListView>(Resource.Id.listview);
- //listnames.SetBackgroundColor(Color.MidnightBlue);
- LinearLayout mainLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout2);
- mainLayout.SetBackgroundColor(Color.RoyalBlue);
- itemlist1 = new List<string>();
- itemlist1.Add("Create App Using Xamarin.Android Using C# - Part Four");
- itemlist1.Add("Introduction To SQL Operations Studio And Connecting It With SQL Server");
- itemlist1.Add("Client Side Routing Using Angular In MVC");
- itemlist1.Add("Partial View In ASP.NET MVC Using Entity Framework");
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemlist1);
- listnames1.Adapter = adapter;
- listnames1.ItemClick += Listnames1_ItemClick;
- }
- private void Listnames1_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
- {
- Toast.MakeText(this, e.Position.ToString(), ToastLength.Long).Show();
- Toast.MakeText(this, "You Clicked Article at " + itemlist1[e.Position], ToastLength.Long).Show();
- }
- ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
- ActionBar.Tab tab = ActionBar.NewTab();
- tab.SetText(tabText);
- tab.SetIcon(Resource.Drawable.Icon); //This is for icon file.
- tab.TabSelected += OnTabSelected;
- ActionBar.AddTab(tab);
Each tab in the action bar should be associated with a fragment. When the user selects a tab, the application will display the fragment that is associated with the tab. The ActionBar is not responsible for displaying the appropriate fragment to the user. Instead, the ActionBar will notify an application about state changes in a tab through a class that implements the ActionBar.ITabListener interface. This interface provides three callback methods that Android will invoke when the state of the tab changes,
OnTabSelected
This method is called when the user selects the tab. It should display the fragment.OnTabReselected
This method is called when the tab is already selected but is selected again by the user. This callback is typically used to refresh/update the displayed fragment.OnTabUnselected
This method is called when the user selects another tab. This callback is used to save the state in the displayed fragment before it disappears.
Xamarin.Android wraps the ActionBar.ITabListener with events on the ActionBar.Tab class. Applications may assign event handlers to one or more of these events. There are three events (one for each method in ActionBar.ITabListener) that an action bar tab will raise,
- TabSelected
- TabReselected
- TabUnselected
Create a new tab using ActionBar.NewTab().Assign event handlers or provide a custom ActionBar.ITabListener implementation that will respond to the events that are raised when the user interacts with the ActionBar tabs.Add the tab that was created in the previous step to the ActionBar.
Event Handlers and ActionBar.ITabListener
Applications should use event handlers and ActionBar.ITabListener for different scenarios. Event handlers do offer a certain amount of syntactic convenience; they save you from having to create a class and implement ActionBar.ITabListener. This convenience does come at a cost – Xamarin.Android performs this transformation for you, creating one class and implementing ActionBar.ITabListener for you. This is fine when an application has a limited number of tabs.
When dealing with many tabs, or sharing common functionality between ActionBar tabs, it can be more efficient in terms of memory and performance to create a custom class that implements ActionBar.ITabListener, and sharing a single instance of the class. This will reduce the number of GREF's that a Xamarin.Android application is using.
A ListView consists of the following parts,
Rows – The visible representation of the data in the list.
Adapter – A non-visual class that binds the data source to the list view.
Fast Scrolling – A handle that lets the user scroll the length of the list.
Section Index – A user interface element that floats over the scrolling rows to indicate where in the list the current rows are located. The primary classes used to display ListViews are shown here:
ListView – user interface element that displays a scrollable collection of rows. On phones it usually uses up the entire screen (in which case, the ListActivity class can be used) or it could be part of a larger layout on phones or tablet devices.
View – a View in Android can be any user interface element, but in the context of a ListView it requires a View to be supplied for each row.
BaseAdapter – Base class for Adapter implementations to bind a ListView to a data source.
ArrayAdapter – Built-in Adapter class that binds an array of strings to a ListView for display. The generic ArrayAdapter<T> does the same for other types.
CursorAdapter – Use CursorAdapter or
SimpleCursorAdapterto display data based on an SQLite query.
To add rows to a ListView you need to add it to your layout and implement an IListAdapter with methods that the ListView calls to populate itself. Android includes built-in ListActivity and ArrayAdapter classes that you can use without defining any custom layout XML or code. The ListActivity class automatically creates a ListView and exposes a ListAdapter property to supply the row views to display via an adapter.The built-in adapters take a view resource ID as a parameter that gets used for each row.
ArrayAdapter<string> is great because of its simplicity, but it's extremely limited. However, often times you have a collection of business entities, rather than just strings that you want to bind. For example, if your data consists of a collection of Employee classes, then you might want the list to just display the names of each employee. To customize the behavior of a ListView to control what data is displayed you must implement a subclass of BaseAdapter overriding the following four items,
Count – To tell the control how many rows are in the data.
GetView – To return a View for each row, populated with data. This method has a parameter for the ListView to pass in an existing, unused row for re-use.
GetItemId – Return a row identifier (typically the row number, although it can be any long value that you like).
this[int] indexer – To return the data associated with a particular row number.
- ListView.FastScrollEnabled = true;
To implement ISectionIndexer you need to add three methods to an adapter,
GetSections – Provides the complete list of section index titles that could be displayed. This method requires an array of Java Objects so the code needs to create a Java.Lang.Object[] from a .NET collection. In our example it returns a list of the initial characters in the list as Java.Lang.String .
GetPositionForSection – Returns the first row position for a given section index.
GetSectionForPosition – Returns the section index to be displayed for a given row.


- What is actionbar.
- What is actionbar tabs.
- How to set image and text in tab.
- Listview functionality in individual tabbed page.

Ahsan SiddiquePosted Dec 25, 2017, 12:56 AM
Not bad......Nice try