Android: Creating a Tab Layout With TabHost and TabWidget

Android Tabhost Example

Straight out of the box! This example uses the built in tabhost and tabwidget in a project. In this example we will be placing the all three tab’s content in one layout and not in separate layouts. To customize the tabs: Android: Styling the Tabs in a TabWidget.

Continue reading Android: Creating a Tab Layout With TabHost and TabWidget

iOS MonoTouch: Adding a Background Pattern to Your Application

From what I can tell there isn’t any real easy way to just set a background pattern to the entire app in iOS. So to get around this I put a UIView under everything and filled it horizontally and vertically.

public override void ViewDidLoad(){
	bgView.BackgroundColor = UIColor.FromPatternImage (UIImage.FromBundle("bg_app_pattern.png"));	
}

iOS MonoTouch: Show / Hide the Toolbars or Individual Toolbar Buttons

When you are navigating across screens in your application, you trigger “ViewWillAppear”. This function is helpful when you need something to happen every time you view the screen.

public override void ViewWillAppear (bool animated){
	base.ViewWillAppear (animated);
	this.NavigationController.SetNavigationBarHidden (true, animated); //-- hide the navigation bar
	this.NavigationController.SetToolbarHidden(false,animated); //-- show the bottom toolbar
	if (someCondition == null) {
		//-- show these buttons in the toolbar
		this.SetToolbarItems( new UIBarButtonItem[] {
			spacer,createButton,spacer,helpButton,spacer
		} , false);
	} else {
		//-- show these buttons in the toolbar otherwise
		this.SetToolbarItems( new UIBarButtonItem[] {
			spacer,loginButton,spacer,passwordButton,spacer,helpButton,spacer
		} , false);
	}
}

iOS MonoTouch: Add a Nav Bar Bottom ToolBar in MonoTouch

Adding a toolbar at the bottom of an iOS application is really easy.
See more information here: MonoTouch Docs

//-- using the built in icons
var refreshButton = new UIBarButtonItem(UIBarButtonSystemItem.Refresh, (s, e) => {
    Console.WriteLine("Refresh clicked");
})

//-- using your own icon
var imageButton = new UIBarButtonItem(UIImage.FromBundle("myIcon.png"), (s, e) => {
    Console.WriteLine("Pause clicked");
})

var spacer = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace) { Width = 50 };

this.SetToolbarItems( new UIBarButtonItem[] {
    spacer, refreshButton, spacer, imageButton, spacer
}, false);

this.NavigationController.ToolbarHidden = false;

iOS MonoTouch: Setting a Button as a Toolbar Button Item in MonoTouch

I recently got involved in a project where I was given screens with UIButtons along the bottom of the screen; I was told to make these into a toolbar.

iOS already has a built in toolbar at the bottom, you just need to tap into it by injecting UIBarButtonItem into it. Using MonoTouch, here’s what I did:

//-- initial code for button
this.HelpButton.SetBackgroundImage (UIImage.FromBundle("vgift_small.png"), UIControlState.Normal);
this.HelpButton.SetTitle (String.Empty, UIControlState.Normal);
this.HelpButton.TouchUpInside += (sender, e) => {
	if (this.helpScreen == null) {
		this.helpScreen = new VF3ConfirmOrderHelp ();
	}
	this.NavigationController.PushViewController (this.helpScreen, true);
};

//-- set toolbar button
UIBarButtonItem NavButton1 = new UIBarButtonItem(this.HelpButton);

var spacer = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace) { Width = 50 };
			
this.SetToolbarItems( new UIBarButtonItem[] {
	spacer,NavButton1,spacer
} , false);

this.NavigationController.ToolbarHidden = false;

Android: A Basic Alert Dialog Created Programmatically

In order for this to work, an AlertDialog must extend some kind of Activity.

final Context context = this;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

// set title
alertDialogBuilder.setTitle("Your Title");

// set dialog message
alertDialogBuilder
	.setMessage("Click yes to exit!")
	.setCancelable(false)
	.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog,int id) {
			// if this button is clicked, close
			// current activity
			//MainActivity.this.finish();
		}
	})
	.setNegativeButton("No",new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog,int id) {
			// if this button is clicked, just close
			// the dialog box and do nothing
			dialog.cancel();
		}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

Android: Animation Drawable Using Animation List

Adding an animation to your project is easy in android.
Note: animating large images can crash your app and cause it to force close.

MainActivity.java

public class MainActivity extends Activity{

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

    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        if(hasFocus){
            ImageView imageView = (ImageView) findViewById(R.id.imageConfirmOrderGift);
            AnimationDrawable animation = (AnimationDrawable) imageView.getDrawable();
            animation.start();
        }
    }
}

res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@anim/animate_image" />

</RelativeLayout>

res/anim/animate_image.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/selected" 
    android:oneshot="false" 
    android:visible="true">

    <item android:drawable="@drawable/image1" android:duration="50"/>
    <item android:drawable="@drawable/image2" android:duration="50"/>
    <item android:drawable="@drawable/image3" android:duration="50"/>
    <item android:drawable="@drawable/image4" android:duration="50"/>
    <item android:drawable="@drawable/image5" android:duration="50"/>

</animation-list>