Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel
Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Tuesday, 13 January 2015

Pass data from one activity to another using Intent in Xamarin

In this Article, we will see how to pass data from one activity to another using Intent in Xamarin. In my previous Article, we learnt about Checkbox widget in Xamarin.

Let's Begin:
1. Create a new Android Application.
2. Add a layout (named as Main.axml).
3) Drop TextView, EditText(set id="@+id/txt_Name") and Button(set id="@+id/btn_Submit" and text="Submit") control onto the Main.axml Layout.
4) Add another Layout and name it as Result.axml.
Drop TextView(Large) control on Result.axml layout and set id="@+id/txt_Result".
Activity1.cs Code:
using System;

using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;

namespace IntentDemo
{
    [Activity(Label = "IntentDemo", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        EditText txt_Name;
        Button btn_Submit;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            //Get txt_Name and btn_Submit Button CheckBox control from the Main.xaml Layout. 
            txt_Name = FindViewById<EditText>(Resource.Id.txt_Name);
            btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit);
            //btn_Submit click event
            btn_Submit.Click += btn_Submit_Click;
        }

        void btn_Submit_Click(object sender, EventArgs e)
        {
            //if EditText in not Empty
            if(txt_Name.Text!="")
            {
                //passing the Activity2 in Intent
                Intent i = new Intent(this,typeof(Activity2));
                //Add PutExtra method data to intent.
                i.PutExtra("Name",txt_Name.Text.ToString());
                //StartActivity
                StartActivity(i);
            }
            else
            {
                Toast.MakeText(this,"Please Provide Name",ToastLength.Short).Show();
            }
        }
    }
}
In above Code, We have created an Intent and Bind Activity2 to it. PutExtra method of Intent allows us to store data in Key-Value pairs. We can retrieve the data in the other Activity using Intent. GetTypeExtra method. In above Code, We have passed string using PutExtra() method and for Retrieving the string in another activity, we use Intent.GetStringExtra("Name") method and pass key value in it as a parameter.
Activity2.cs Code:
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;

namespace IntentDemo
{
    [Activity(Label = "Result")]
    public class Activity2 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "Result" layout resource
            SetContentView(Resource.Layout.Result);
            //Get txt_Result TextView control from the Main.xaml Layout. 
            TextView txt_Result = FindViewById<TextView>(Resource.Id.txt_Result);
            //Retrieve the data using Intent.GetStringExtra method
            string name = Intent.GetStringExtra("Name");
            txt_Result.Text ="Hello, "+name;
        }
    }
}
Final Preview:
Hope you like it. Thanks.
[Download Source Code via Google Drive]

Monday, 29 December 2014

CheckBox Widget in Xamarin

In this Article, We will see How to use CheckBox widget control and CheckBox's CheckedChange Event in Xamarin.For Demonstration, I am going to create a Simple application in which if user accept the terms and condition(using CheckBox) then Submit button become enabled. Before starting, I suggest you read my previous articles of this series.


Let's Begin:
1) Create a New Android Application.
Create Android Application in Xamarin
2) Drop a CheckBox (with id="@+id/checkBox1") and Button (with id="@+id/btn_Submit") control onto the Layout, Main.axml.
CheckBox Widget in Xamarin
Set the Text of CheckBox as Accept Terms and Condition and Button's Text as Submit.
CheckBox and Button in Xamarin
Main.axml Code:
<?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">
    <CheckBox
        android:text="Accept Terms and Conditions"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox1" />
    <Button
        android:text="Submit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_Submit"
        android:enabled="false" />
</LinearLayout>
In Activity1.cs, We have created CheckBox CheckedChange Event. The following is Activity1.cs Code:
using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace CheckBoxDemo
{
    [Activity(Label = "CheckBoxDemo", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        //Create instance of Button
        Button btn_Submit;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            //setting the view for the Activity 
            SetContentView(Resource.Layout.Main);
            //Get btn_Submit Button and checkBox1 CheckBox control from the Main.xaml Layout.
            btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit);
            CheckBox checkBox1 = FindViewById<CheckBox>(Resource.Id.checkBox1);
            //CheckBox CheckedChange Event
            checkBox1.CheckedChange += checkBox1_CheckedChange;
            //btn_Submit Click Event
            btn_Submit.Click += btn_Submit_Click;
        }

        void checkBox1_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            //If CheckBox is Checked
            if(e.IsChecked==true)
            {
                btn_Submit.Enabled = true;
            }
            else
            {
                btn_Submit.Enabled = false;
            }
        }

        void btn_Submit_Click(object sender, EventArgs e)
        {
            Toast.MakeText(this,"Thanks for accepting Terms and Condition",ToastLength.Short).Show();
        }
    }
}
Final Preview:
CheckBox and Button in Xamarin / Mono for Android
Hope you like it. Thanks.
[Download Source Code via Google Drive]

Sunday, 14 December 2014

AutoCompleteTextView in Xamarin

AutoCompleteTextView is an Editable TextView used for displaying suggestion when user starts typing in the control. Suggestion is displayed in a drop down menu and when user select the item from the drop down list, the selected text is displayed in a TextView. Before starting, I suggest you to read my previous Article.


Let's Start:

Method 1:(using Default Android Resources)
1) Create New Android Application.

2) Drop TextView, AutoCompleteTextView(with id="@+id/autoComplete1") and a Button control(with id="@+id/btn_Submit" and text="Submit") on Layout i.e. Main.axml
Main.axml Source Code:
<?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">
    <TextView
        android:text="Enter Name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:padding="5dp" />
    <AutoCompleteTextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/autoComplete1" />
    <Button
        android:text="Submit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_Submit" />
</LinearLayout>
In Activity1.cs, Create a String Array (named as names) used for Displaying AutoComplete Suggestion and an ArrayAdapter for filling the View i.e. AutoCompleteTextView with the list of names(Array). We have also created a btn_Submit.Click event. On Clicking the btn_Submit it will display the text present in autoComplete1 AutoCompleteTextView in a Toast message and if AutoCompleteTextView is empty than Display a Toast Message(in other words Please Enter Name!).
Activity1.cs Code:
using Android.App;
using Android.Widget;
using Android.OS;

namespace AutoCompleteTextViewExample
{
    [Activity(Label = "AutoCompleteTextViewExample", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        //Creating Instance of AutoCompleteTextView and Button
        AutoCompleteTextView autoComplete1;
        Button btn_Submit;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            //setting the view for the Activity
            SetContentView(Resource.Layout.Main);
            //Get autoComplete1 AutoCompleteTextView and btn_Hello Button control from the Main.axml Layout.
            autoComplete1 = FindViewById<AutoCompleteTextView>(Resource.Id.autoComplete1);
            btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit);

            //string array used for displayling AutoComplete Suggestion
            var names = new string[] {"Anoop","Arjit","Akshay","Ankit","Rakesh"};
            //Use ArrayAdapter for filling the View i.e. AutoCompleteTextView with the list of names(Array).
            //Use SimpleSpinnerItem(Predefined layout in Android Resources) for displaying the dropdown list
            ArrayAdapter adapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleSpinnerItem,names);
            autoComplete1.Adapter = adapter;

            btn_Submit.Click += btn_Submit_Click;
        }
        //btn_Submit Click Event
        void btn_Submit_Click(object sender, System.EventArgs e)
        {
            //Checking if autoComplete1.Text is not empty
            if (autoComplete1.Text != "")
            {
                Toast.MakeText(this, "Name Entered =" + autoComplete1.Text, ToastLength.Short).Show();
            }
            else
            {
                Toast.MakeText(this, "Please Enter Name!", ToastLength.Short).Show();
            }
        }
    }
}
Preview:


Method 2:(Using Custom Resources)
In above example, We have used SimpleSpinnerItem for displaying the list in AutoCompleteTextView but we can also create Custom Resouces for displaing the Auto Suggested list.
Let's Add a new Layout(named as ListName.axml) and Drop TextView Control on it. Adjust textColor, textSize, padding etc according to your need.

ListName.axml Code:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="Text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:padding="5dp"
    android:textSize="20dp"
    android:textColor="@android:color/black" />
Go to Activity1.cs and in ArrayAdapter, use the Custom Layout Resource which we have created. Rest of the code remains same.
ArrayAdapter adapter = new ArrayAdapter<string>(this, Resource.Layout.ListName, names);
Preview:
Hope you like it. Thanks.
[Download Source Code via Google Drive]

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook