Posts Tagged ‘ Android Development ’

Formatting and Styling Strings (Java/Android)

So, let’s say you’re reading the Android document on formatting and styling strings and come across the line

Hello, %1$s! You have %2$d new messages.

The document explains that %1$s and %2$d are a string and decimal value, respectively. The next chunk of code is

String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

Now, how does this relate? Well, the 1$ and 2$ means first and second parameter, respectively. So in this case %1$s is replaced with the variable username and %2$d is replaced with mailCount.

Why should you construct strings this way instead of constructing a string with the plus sign? Well, either way is valid, but this is just the “old” way of constructing strings. There was a time where using the plus sign to construct strings required converting decimal numbers to a string or else the compiler would complain. It was an obnoxious intermediate step. With Java though, the plus sign is smart enough to change the datatype of a decimal to a string if the rest of the line contains strings. To make a string (that contains a number) to a decimal, just add “+ 0” to the end and Java will take care of it. Pretty slick, right?

Advertisements

Using a ViewSwitcher in your Android xml layouts

ViewSwitcher is an interesting thing, and you may find use for it in your project for various uses.  Ive implemented it for you in a thread type fashion, where it could act as a loading screen.  Don’t think this is all it is useable for however.  I’ve attached the project below.

The basic premise is you put the <ViewSwitcher> tag around 2 different layouts in your xml file, and then declare a variable in your main activity, a ViewSwitcher.  Then, to switch  between views, just call switcher.showNext() and switcher.showPrevious().

private ViewSwitcher switcher;
private static final int REFRESH_SCREEN = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);

	switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
	startScan();
}
... Finish the rest of your program

And this is how you will switch between the views

switcher.showNext();  // Switches to the next view
switcher.showPrevious();  // Switches to the previous view

Showing the ViewSwitcher tag in action!

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/profileSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true" />
    <TextView
        android:text="Loading…"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/progressbar"
        android:gravity="center"/>
</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal" >
    <TextView
        android:text="Finished!"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerVertical="true" />
</RelativeLayout>

</ViewSwitcher>

Heres some screenshots, and the source as promised, enjoy!

http://www.inphamous.com/code/examples/ViewSwitcherExample.zip

 

-Kevin Grant

Using Javadocs in your Android / Java Programs

Coming from college, one thing that stuck in my head was “COMMENT YOUR CODE BECAUSE NOBODY KNOWS WHAT YOU’RE DOING!!!”

Well, thats not always true.  And the more and more I work in industry, the less need there becomes to comment every single line of code.  Actually, in most situations, as long as the variables and functions are named intuitively, you’re fine.

For example, the following line, you should probably heavily comment, or rename to variables to things that anyone other than you can understand.

 return this.tmpThing % x((int)y*52.5 math.sqrt(evilol) + tmpx)

Actually, you should probably just rewrite this.  Its awful.  Moving on…

In java, there is this handy awesome thing called “Javadocs” and if you’ve been programming in eclipse, you are probably familiar with them and you didn’t even know it.  View this picture.  Notice how when you mouse over the function, it tells you the purpose, the variables it takes, and what it returns?  These are javadocs!

Well, for a while, I thought this was specific to eclipse, or android, or built in Java stuff, or whatever.  Point being, I never thought it was something I could do myself (I know, so naive).  But, good news!  You can incorporate this functionality into your own code, how cool is that?  It’s quite simple, and and pretty soon all of your comments will look like this.  You and I are now one step closer on our way to becoming a real, professional developers.

Its really just this simple

/**
* This function takes in 1 bunny, and 1 horsie.  It calls
* the "produceCrap" function on both of them, and then
* returns the total crap produced between the 2 of them.
*
* @param tmpBunny a Bunny, of any size or color, that has eaten within the last 10 minutes (probably your internet cable)
* @param tmpHorsie a Horsie, preferrably large, that has eaten a diet of broccoli and asparagus for the last week
* @return an int that could not possibly represent the actual horror that you have just requested
*/

Just put comments in this style right above the function, and the Javadocs take care of the rest.  In no time, you can have bunnies and horsies fertilizing your land like the pros!