ActionBar with Custom View Example in Android

custom_actionbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/black_pattern" >

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:src="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:background="@null"
        android:src="@android:drawable/ic_menu_rotate" />

</RelativeLayout>

Activity Class (MainActivity.java)

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ActionBar mActionBar = getActionBar();
		mActionBar.setDisplayShowHomeEnabled(false);
		mActionBar.setDisplayShowTitleEnabled(false);
		LayoutInflater mInflater = LayoutInflater.from(this);

		View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
		TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
		mTitleTextView.setText("My Own Title");

		ImageButton imageButton = (ImageButton) mCustomView
				.findViewById(R.id.imageButton);
		imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				Toast.makeText(getApplicationContext(), "Refresh Clicked!",
						Toast.LENGTH_LONG).show();
			}
		});

		mActionBar.setCustomView(mCustomView);
		mActionBar.setDisplayShowCustomEnabled(true);
	}

}

References :
http://stacktips.com/tutorials/android/actionbar-with-custom-view-example-in-android

Show a Dialog using DialogFragment in Xamarin Android

Let us first define the layout for your fragment. In this example, I have used two TextViews and Button. My layout looks as follows:

<?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"
    android:padding="10dp">
    <TextView
        android:text="Lorem ipsum"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />
    <TextView
        android:text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit...."
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:layout_marginTop="10dp" />
    <Button
        android:text="Close"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/CloseButton"
        android:layout_marginTop="10dp" />
</LinearLayout>

Now let us inflate the layout from OnCreateView() method. My DialogFragment class looks as follows:

public class DialogFragment1 : DialogFragment
	{
		public static DialogFragment1 NewInstance(Bundle bundle){
			DialogFragment1 fragment = new DialogFragment1 ();
			fragment.Arguments = bundle;
			return fragment;
		}

		public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
		{
			// Use this to return your custom view for this Fragment
			View view =  inflater.Inflate(Resource.Layout.DialogFragment1Layout, container, false);
			Button button = view.FindViewById<Button> (Resource.Id.CloseButton);
			button.Click += delegate {
				Dismiss();
				Toast.MakeText(Activity ,"Dialog fragment dismissed!" , ToastLength.Short).Show();
			};

			return view;
		}
	}

We are pretty much done!. Add the following code snippet in your Activity to instantiate and display the dialog:

FragmentTransaction ft = FragmentManager.BeginTransaction();
//Remove fragment else it will crash as it is already added to backstack
Fragment prev = FragmentManager.FindFragmentByTag("dialog");
if (prev != null) {
	ft.Remove(prev);
}

ft.AddToBackStack(null);

// Create and show the dialog.
DialogFragment1 newFragment = DialogFragment1.NewInstance(null);

//Add fragment
newFragment.Show(ft, "dialog");

References :
http://javatechig.com/xamarin/alertdialog-and-dialogfragment-example-in-xamarin-android

Show a Dialog using AlertDialog in Xamarin Android

//set alert for executing the task
AlertDialog.Builder alert = new AlertDialog.Builder (this);
alert.SetTitle ("Confirm delete");
alert.SetMessage ("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");
alert.SetPositiveButton ("Delete", (senderAlert, args) => {
	Toast.MakeText(this ,"Deleted!" , ToastLength.Short).Show();
});

alert.SetNegativeButton ("Cancel", (senderAlert, args) => {
	Toast.MakeText(this ,"Cancelled!" , ToastLength.Short).Show();
});

Dialog dialog = alert.Create();
dialog.Show();

References :
http://javatechig.com/xamarin/alertdialog-and-dialogfragment-example-in-xamarin-android

Communicate over TCP Socket and Serialize data with json format in C#

Client :

Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            socket.Connect("127.0.0.1",9011);

            Console.WriteLine("connected...");

            Person person=new Person(){Name = "Mahmood"};

            string jsonData = JsonConvert.SerializeObject(person);
            byte[] dataBytes = Encoding.Default.GetBytes(jsonData);

            socket.Send(dataBytes);

            Console.WriteLine("sent...");

            byte[] buffer=new byte[1024*4];
            int readBytes = socket.Receive(buffer);
            MemoryStream memoryStream = new MemoryStream();

            while (readBytes>0)
            {
                memoryStream.Write(buffer, 0, readBytes);

                if (socket.Available > 0)
                {
                    readBytes = socket.Receive(buffer);   
                }
                else
                {
                    break;
                }
            }

            Console.WriteLine("read...");

            byte[] totalBytes = memoryStream.ToArray();

            memoryStream.Close();

            string readData = Encoding.Default.GetString(totalBytes);

            Greeting response = JsonConvert.DeserializeObject<Greeting>(readData);

            Console.WriteLine(response.Msg);

            Console.ReadKey();

Server :

            Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
            EndPoint endPoint=new IPEndPoint(IPAddress.Any, 9011);

            socket.Bind(endPoint);
            socket.Listen(5);

            while (true)
            {
                Console.WriteLine("waiting for new connection...");

                Socket newSocket = socket.Accept();

                MemoryStream memoryStream = new MemoryStream();

                Console.WriteLine("new connection...");

                byte[] buffer=new byte[1024];

                int readBytes = newSocket.Receive(buffer);

                while (readBytes>0)
                {
                    memoryStream.Write(buffer,0,readBytes);

                    if (socket.Available > 0)
                    {
                        readBytes = newSocket.Receive(buffer);    
                    }
                    else
                    {
                        break;
                    }
                }

                Console.WriteLine("data received...");

                byte[] totalBytes = memoryStream.ToArray();

                memoryStream.Close();

                string readData = Encoding.Default.GetString(totalBytes);

                Person p= JsonConvert.DeserializeObject<Person>(readData);

                Greeting g = SayHello(p);

                string dataToSend = JsonConvert.SerializeObject(g);

                byte[] dataToSendBytes = Encoding.Default.GetBytes(dataToSend);

                newSocket.Send(dataToSendBytes);

                newSocket.Close();

                Console.WriteLine("data sent...");
            }

Communicate over TCP Socket and Serialize data with binary format in C#

Client :

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect("127.0.0.1", 9011);

            Console.WriteLine("connected...");

            BinaryFormatter formatter=new BinaryFormatter();
            MemoryStream memoryStream = new MemoryStream();
            Person person = new Person() { Name = "Mahmood" };

            formatter.Serialize(memoryStream,person);

            byte[] dataBytes = memoryStream.ToArray();
            socket.Send(dataBytes);

            Console.WriteLine("sent...");

            memoryStream = new MemoryStream();
            byte[] buffer = new byte[1024 * 4];
            int readBytes = socket.Receive(buffer);
            
            while (readBytes > 0)
            {
                memoryStream.Write(buffer, 0, readBytes);

                if (socket.Available>0)
                {
                    readBytes = socket.Receive(buffer);
                }
                else
                {
                    break;
                }
            }

            Console.WriteLine("read...");
            formatter=new BinaryFormatter();

            // set position to 0 or create a new stream
            memoryStream.Position = 0;
            Greeting response = (Greeting) formatter.Deserialize(memoryStream);

            Console.WriteLine(response.Msg);

            memoryStream.Close();
            socket.Close();

            Console.ReadKey();

Server :

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            EndPoint endPoint = new IPEndPoint(IPAddress.Any, 9011);

            socket.Bind(endPoint);
            socket.Listen(5);

            while (true)
            {
                Console.WriteLine("waiting for new connection...");

                Socket newSocket = socket.Accept();

                Console.WriteLine("new connection...");

                byte[] buffer = new byte[1024];

                int readBytes = newSocket.Receive(buffer);
                MemoryStream memoryStream = new MemoryStream();

                while (readBytes > 0)
                {
                    memoryStream.Write(buffer, 0, readBytes);

                    if (socket.Available>0)
                    {
                        readBytes = newSocket.Receive(buffer);
                    }
                    else
                    {
                        break;
                    }
                }

                Console.WriteLine("data received...");

                BinaryFormatter formatter=new BinaryFormatter();
                memoryStream.Position = 0;
                Person p = (Person) formatter.Deserialize(memoryStream);

                memoryStream.Close();

                Greeting g = SayHello(p);

                formatter=new BinaryFormatter();
                memoryStream=new MemoryStream();

                formatter.Serialize(memoryStream,g);

                newSocket.Send(memoryStream.ToArray());

                memoryStream.Close();
                newSocket.Close();

                Console.WriteLine("data sent...");

Android Exception : Only the original thread that created a view hierarchy can touch its views

 private void startingUp() {
    Thread timer = new Thread() { //new thread         
        public void run() {
            Boolean b = true;
            try {
                do {
                    counter++;
                    title();
                    sleep(1000);

                    runOnUiThread(new Runnable() {  
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub

                        title.clearComposingText();
                    }
                });


                }
                while (b == true);
            } catch (IntruptedException e) {
                e.printStackTrace();
            }
            finally {
            }
        };
    };
    timer.start();
}

References :
http://stackoverflow.com/questions/14978052/only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-views-on-a

Python UUID

>>> import uuid

>>> # make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')

>>> # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

>>> # make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')

>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')

>>> # make a UUID from a string of hex digits (braces and hyphens ignored)
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')

>>> # convert a UUID to a string of hex digits in standard form
>>> str(x)
'00010203-0405-0607-0809-0a0b0c0d0e0f'

>>> # get the raw 16 bytes of the UUID
>>> x.bytes
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'

>>> # make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

References :
https://docs.python.org/3.5/library/uuid.html

Build libcurl static library on Windows

Download the latest curl generic source from: http://curl.haxx.se/latest.cgi?curl=tar.gz
Extract the source to a local directory (we’ll be using C:\libcurl)
Open a command prompt
“C:\Program Files (x86)\Microsoft Visual Studio 15.0\VC\bin\vcvars32.bat” To initialize your VC environment variables (adjust your VS 2015 installation directory as needed)
cd C:\libcurl\winbuild

nmake /f Makefile.vc mode=static VC=14

The build should appear in C:\libcurl\builds\libcurl-vc14-x86-release-static-ipv6-sspi-winssl

References :
http://stackoverflow.com/questions/20171165/getting-libcurl-to-work-with-visual-studio-2013