Monday, August 12, 2013

How to download image from web in Mono for Android?

Note:The  Image downloaded can be seen only in Mobile Device not in Emulator.

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Java.Net;
using Android.Util;
using Java.IO;
using System.Net;
using System.IO;

namespace AndroidApplication3
{
    [Activity(Label = "AndroidApplication3", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        public static string SD_CARD = "sdCard";
        string filename = "/sdcard/barn.jpg";

         string path = Android.OS.Environment.ExternalStorageDirectory.Name + "/image_name.png";

        //string path = "/sdcard/MyImages/image_name.png";

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            button.Click += delegate
            {
                string imageUrl = "http://t0.gstatic.com/images?q=tbn:ANd9GcS1EVyi66BGmy-b2Nqsyw2lPjsyb3MFUxYillTeiQ4ecnPG7qPVRA";
                byte[] imageBytes;
                HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
                WebResponse imageResponse = imageRequest.GetResponse();
                Stream responseStream = imageResponse.GetResponseStream();
                using (BinaryReader br = new BinaryReader(responseStream))
                {
                    imageBytes = br.ReadBytes(500000);
                    br.Close();
                }
                responseStream.Close();
                imageResponse.Close();
                FileStream fs = new FileStream(path, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                try
                {
                    bw.Write(imageBytes);
                }
                finally
                {
                    fs.Close();
                    bw.Close();
                }
            };
        }
    }
}

Saturday, August 3, 2013

Sliding Between Activities in Android or Monodroid

Take two layouts for Sliding In and Sliding Out Transition.

SlidingIn.axml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%"
        android:toXDelta="0%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="200" />
</set>
SlidingOut.axml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%"
        android:toXDelta="0%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="200" />
</set>

Take Another two Layouts for Switching between Activities:

layout1.axml:

<?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:layout_width="match_parent"
        android:textStyle="bold"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="0dp"
        android:textSize="20dp"
        android:text="Problem is starting now.I need a list view inside my Custom Alert Dialog with each row contains a radio button. I use the same method. Here is my code."
        android:id="@+id/tvItemTitle" />

</LinearLayout>

Main.axml:

<?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">
    <Button
        android:id="@+id/MyButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Hello" />

</LinearLayout>

Activity1.cs:

using System;

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

namespace SlidingBetweenActivities
{
    [Activity(Label = "SlidingBetweenActivities", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        int count = 1;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);

            button.Click += new EventHandler(button_Click);
        }

        void button_Click(object sender, EventArgs e)
        {
             var NextPage= new Intent(this, typeof(MyActivity));
                       
            StartActivity(NextPage);

            OverridePendingTransition(Resource.Layout.SlidingIn, Resource.Layout.SlidingOut);
        }
    }
}

MyActivity.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace SlidingBetweenActivities
{
    [Activity(Label = "My Activity")]
    public class MyActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.layout1);
            // Create your application here
        }
    }
}

Friday, August 2, 2013

Tab Bar at Bottom in Android

Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabHostLayouts"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:background="#B70940"
            android:layout_marginTop="0dp"
            android:layout_weight="0"
            android:tabStripEnabled="false"
            android:layout_marginBottom="-07dp"
            android:layout_height="wrap_content" />
    </LinearLayout>
</TabHost>