Tag Archive: Activity


Recently, I had to implement Twitter sharing in my new app that I have been working on recently. Upon searching about how to do this, I found out a library called Twitter4J. This library has some decent functions but it’s not that simple to implement as compared to the Facebook SDK which handles all the stuff within and exposes some handlers for post-authentication tasks.

Anyhow, I continued my research about using this library and found out some implementations that launch the browser for authentication and then invoking your application back when the process is complete. From there onwards, things become simple.

I however didn’t like the idea and rolled over my sleeves to code what I had in my mind. Instead of loading the browser, all I did was put a webview in my activity to load the authentication url in it. Upon completing authentication, it is redirected to the callback url that is specified while initializing the twitter object. Checkout the code below:

 Capture

You can specify any custom URL in place of CALLBACKURL (which is defined as a constant in my class). After this, all you have to do is to place a check in your WebViewClient’s onPageStarted method to find out if the authentication is done and the twitter has redirect you back to your callback URL.

Capture

Almost done! Smile Now you can use the updateStatus function to update the status on Twitter.

Advertisements

Menu System is an important part of a game, no matter which platform you want to target. Same is the case with Android and I found the platform flexible enough to facilitate any such requirement. While developing my first game, i too got into multiple approaches while i was searching for the best (and of-course ‘easier’ ;)) way to develop a menu system for my game. I found some ways on different forums that involve making an activity for each menu screen. This has some advantages but I personally didn’t like this approach. Finally I found a way, through some experimentation, that was more understandable (for an indie PC game developer, like me) and is described below:

1. Create a layout file (.xml) for your menu screen just like you do to create any other screen and place some controls on it. You can add as many screens as you want using this way.

2. Now you have to modify your game activity class to initialize these layouts using View objects. You can either keep separate variables for each view or you can declare and array of views and initialize them all.

image

3. The currentView variable is an index that determines the view that should be passed on to the activity to show on the screen.

4. Now initialize your view depending on its type. You must use the LayoutInflator class to get the view object that you want to store.

image

This is a simple utility function that takes a layout resource id and returns the required view object.

5. Write event handlers for your controls in each layout in your activity class and use the currentView variable to determine the next view to show.

6. Now you can use the setContentView(View) function in your event handlers to switch to the next screen by using currentView index and using it in allViews array. Look at the sample call below:

image 

7. Optionally, you can pass parameters among these views by utilizing the view.setTag(object) function and passing it a custom parameter class instance.

This way, you can think of your whole game as a single activity (just like a PC game) and switching between screens as required. I don’t assume that this approach is the best one out there and i look forward for some positive feedback on this.