Home » Products » Mono for Android » Story Details
Printable Version

Toggle Button using Mono for Android

by Manish Tewatia on Sep 26, 2011

Lets learn how to create toggle button using Mono for Android.
Comments: 1    Views: 1744

Toggle Button : Displays checked/unchecked states using a light indicator

Step for creating Toggle Button
  1. Start new Android application

    android application
     
  2. Go to Main.axml and make following changes

    main page in android

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
      <
    ToggleButton android:id="@+id/togglebutton"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textOn="Sound on"
              android:textOff="Sound off"/>
    </LinearLayout>
     
  3. Go to Activity1.cs class make following changes

    activity class in android

        public class Activity1 : Activity
        {       
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                
                SetContentView(Resource.Layout.Main);
                
                ToggleButton togglebutton = FindViewById<ToggleButton>(Resource.Id.togglebutton);
     
                togglebutton.Click += (a, b) =>
                {               
                    if (togglebutton.Checked)
                        Toast.MakeText(this, "Checked", ToastLength.Short).Show();
                    else
                        Toast.MakeText(this, "Not checked", ToastLength.Short).Show();
                }; 
            }
        }


    As above we create a toggle button and use If-else condition on button click event to check the state of button whether it is clicked or not and shows message and change the view of button according the state
     
  4. Run the application

    toggle button in android

    After click on button:

    android toggle button

    Again click on button:

    toggle button

Thank You.....
Post a Comment
*
Hi Manish, thank you for this helpful tutorial on creating toggle buttons using Mono for Android! I compiled a list of some top resources around this topic. I included your post. Check it out/ feel free to share. http://www.verious.com/board/Giancarlo-Leonio/creating-a-toggle-button-in-android/ Hope other developers find this useful too. :)
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative Suite® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName