Saturday, September 20, 2014

Xamarin.Android Ways to Customize Dialogs

From generic Android to Custom




On the left is Standard Android, on the right is customized. Now I am aware that it is nothing special, however I hope that readers will find the properties they need to modify in order to customize their dialogs by reading this. It is a very simple push in the right direction. I, unfortunately, am no UI designer.


Standard Android Dialog Code

First is the layout. 

Below is my layout. There are extra bits here in the EditTexts, Buttons, and Slider. You can use any layout you wish to design for dialogs, below is only an example and will not compile if you try to copy this xml right into your project. I will show you how to customize the dialog appearance and location not the controls in the dialog. That will come in a later post.

Now the Code
This code should appear in the overridden OnCreateDialog(int id) method in the activity in which you wish to show the dialog. In my case the code was found in the Details Activity. In the code below there is a const int NewGame=1 that has been previously declared. Also you will need to override the OnCreateDialog method in your class. This method override is important and if you don't override it properly your dialog won't show. So line by line. case NewGame which is equal to 1 because I previously defined it as 1. You could simply use the integer 1 and not define an integer constant this just makes it more readable as to which dialog you are working with because you may show more than one in any given activity.


This code tells the system how to build your dialog. To show it you simply call ShowDialog(NewGame) if you are on the UI Thread or RunOnUiThread(()=>ShowDialog(NewGame)) if on a background thread. An example of how you might call Show Dialog is below.

Custom Dialog

In the example if I change the root element or the first linear layout to the following. You will need to know about .9.png(9 patch).There are online tools to create them. Here is another explanation. I am no expert. I found one and edited it with paint. It is shown below.

The legbg.9.png
 

Custom Code

(Scroll to see comments)
After calling the show dialog code you will now see the customized dialog.

Saturday, September 13, 2014

Xamarin.Android- Lock Activity Page Orientation

Lock Screen Orientation

To lock the Orientation of an activity you simply add the ScreenOrientation attribute to the [Activity()] block in your activity. Also you need to add- using Android.Content.PM; Thats all, set the attribute to ScreenOrientation.Portrait or ScreenOrientation.Landscape.

I am not to keen and trying to guess where things should go so I add a complete activity definition below which gives the same answer as this stack overflow post.

Friday, September 12, 2014

Xamarin Android - Application Overview- Using the application class for global variables

Here I just wanted to give a very basic tutorial on how to make a global memory space for an android application so you don't have to constantly read from sharedpreferences or pass things back and forth between activities with bundles.

Create the application class

To start add a blank class to your project and name it whatever you want. I named mine ExampleApp. Then make it inherit the Application class by simply adding : Application. Then add the two methods show below. The code in the OnCreate method will be called before any code in the activity marked as MainLauncher.

Update AndroidManifest.xml

Once you have done that you will need to go to your AndroidManifest.xml and add the android:name to your application tag. make sure that the value of name matches the name of your class that inherited form application. So notice that the value for my android:name is ExampleApp and that is the name of my class that inherits from Application.

Putting it all together

To access (get or set) the public property myString in any other activity use the code below. Since we can get and set variables in the application class we can use this as a mens of passing values back and forth between activities. I am not saying this is the best way, you should be careful about how you use this, just showing another way to handle global variables using Xamarin.Android.

Friday, September 5, 2014

Xamarin.Android Custom View

Here is a simple outline for a custom view. I needed to create one for my own app and I didn't find all the info in one place. Hopefully this will help


The class definition

You must have at minimum the following in your class file. Then in the OnDraw method, tell it how to draw your view.

Adding your view to a layout

For the class attribute make sure that it is the namespace of the custom view in all lowercase. Then it is the class name of the custom view exactly as it is written in the class file. Example: As you can see below the class is "mynamespace.CustomView"

Events

You can wire up touch event listeners in the activity that shows the view with

Xamarin.Android Loading Images From the web

I recently decided to write an app and release it to the play store. The game is called Connect the Game and is available at connectthegame.com or from the play store. I decided I was going to allow login with facebook and give the ability to invite facebook friends. However there was no facebook development kit that had a friend picker that I could just plug in. So I went to my friend google and found this post on Xamarin's website which points you to MonoDroid.UrlImageViewHelper. I attempted to use the Monodroid ViewHelper but it was too slow when trying to load up 500 facebook friend thumbnails. So I started pulling apart the code to find the bottleneck and I came up with a slimmed down version and it is fast. So without further ado the code.

If you wish to see how well this works you can try try the app. Login with your facebook account and then select new game. From the popup dialog touch Invite with facebook and watch the images load.

This is used to load thumbnails asynchronously. If you don't want to loose quality then change the call

bit.Compress(Bitmap.CompressFormat.Jpeg, 70, mstream);

to whatever fits your needs.



Image Helper Class


Facebook Friend Object

This is an object I created, you can create your own or modify this to make the class more generic. I only needed it for Facebook hence the non generic object

Putting them together in a List Adapter