Archives for category: App Development

On the day I bought it, the quickly deepening love I felt for my new LG G3 was seriously dampened when I plugged it in to my computer. I had unlocked Developer options and enabled USB debugging only to enter the default Installer Mode. It all came back to me: the same USB mode-switching rigmarole with past Verizon LG devices.

Today, I varied my Google search and stumbled on the solution, no root required:

  1. Open the Dialer
  2. Dial ##3328873 and press Send
  3. Enter the service code: 000000
  4. Uncheck the Tool Launcher enable box
  5. Reboot the phone

Now when you plug in your phone, it should enter the last connection mode you selected—which, if you need USB debugging, will be Internet connection – Ethernet.

$ logout

Advertisements

We were having trouble POSTing emojis correctly on our app using RoboSpice, where each emoji or non-Latin character would render as Ÿ‰ð. We knew this wasn’t a server issue, as the iOS app wasn’t having problems. We were using a separate RestTemplate for this particular request, but it turned out we simply needed to provide charset=utf-8 as part of our Content-Type header:

Before:

headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

After, based on this StackOverflow answer:

HttpHeaders headers = new HttpHeaders();
headers.set("Connection", "Close");
Charset utf8 = Charset.forName("UTF-8");
MediaType mediaType = new MediaType("application", "x-www-form-urlencoded", utf8);
headers.setContentType(mediaType);

There seems to be another way to continue using the APPLICATION_FORM_URLENCODED constant with the MediaType constructor while passing in the charset, but I couldn’t figure it out in 5 minutes from the documentation.

$ logout

Icons that are different sizes cause problems when you’re working on an app, but don’t want to spend multiple hours individually adjusting padding around each image. So I created a script that uses ImageMagick to size a batch of images to the same dimensions. To prevent clipping, I note the maximum width and height of each image in the batch before running the script, and supply this as my desired-size:

#!/bin/bash

# Name some args
FINALSIZE=$1

# Gotta have some parameters to continue
# First parameter should be image dimensions, i.e. contain 'x' in the middle.
if [ -z "$FINALSIZE" ] || [[ $FINALSIZE != *x* ]]; then
	if [[ $FINALSIZE != *x* ]]; then
		echo "desired-size not given!"
	fi
	echo "usage: normalizeicons.sh desired-size file [file2 ...]"
	echo "         desired-size: Desired image dimensions, e.g. 164x120"
	echo
	exit 1
fi

# Make sure we've got the magic butter
hash convert 2>/dev/null || { echo >&2 "MISSING convert COMMAND! INSTALL ImageMagick TO CONTINUE."; echo; exit 1; }

for INFILE in ${@:2}; do
	FILENAME=$(basename $INFILE)

	# Do the stuff
	# If your grays are fine, use this. Otherwise comment out, and use command below.
	convert $INFILE -background transparent -gravity center -extent $FINALSIZE $FILENAME

	# Sometimes this script messes up grays in your PNGs. If so, uncomment this:
	#convert $INFILE -define png:big-depth=16 -define png:color-type=6 -background transparent -gravity center -extent $FINALSIZE $FILENAME
done

Also on GitHub.

You could probably also modify this to find the correct dimensions based on the images you provide.

This script is meant for PNGs, but can easily be modified for any other image type, like a JPEG, by giving something other than transparent for the -background argument.

Known issues: I occasionally noticed that gray-colored icons got darker after running this script. After searching around, the commented-out convert line solved my problem. Utilizing it increases the file size slightly, but without a finer knowledge of PNGs, this was a quick and easy solution.

$ logout

Adding new image assets to our Android app is tedious. Since our designers target iOS first, there are a few things the Android team has to do when we get new graphics:

  1. Ensure graphics have valid file names (no hyphens, no unnecessary @2x in the name).
  2. Ensure similar graphics (e.g. icons in the drawer) are the same size.
  3. Provide the graphic at all possible densities.

I’ve created a script to speed up the renaming process. Before running it, I only copy the retina (…@2x…) graphics to my working directory, and then run it on my Mac or Linux machine:

#!/bin/bash

# Standardizes image file names to be usable as Android resources.

# Make sure we have some files to rename before continuing
if [ -z "$1" ]; then
	echo "usage: renamer.sh file [file2...]"
	echo
	exit 1
fi

for INFILE; do
	# Do the stuff
	if [ "$INFILE" != "${INFILE//-/_}" ]; then
		# Replace hyphens with underscores
		NEWFILE=${INFILE//-/_}
		mv -i "$INFILE" "$NEWFILE"
	else
		NEWFILE=$INFILE
	fi

	if [ "$NEWFILE" != "${NEWFILE//@2x/}" ]; then
		# Remove retina-ified filenames
		mv -i "$NEWFILE" "${NEWFILE//@2x/}"
	fi

	# Sanity check
	echo "Moved $INFILE to ${NEWFILE//@2x/}"
done

Also on GitHub.

This is useful for doing multiple images at once, e.g. renamer.sh icon-*.png, and, of course, it helps when you like the designer’s naming scheme.

$ logout

I was trying to add a custom font to the LunchTable app when an exception thrown by Typeface.createFromAsset gave me “Native typeface cannot be made.” For me, this was related to Gradle.

The solution was simple: I moved my assets folder directly under main, at the same level as java and res. It might not be necessary, but my .ttf file was in assets/fonts/, as well.

$ logout

There is an issue with the Sony Xperia Z (C6833) keyboard that causes any EditText using an InputFilter to misbehave. For example, typing h-e-l-l-o in one of these fields comes out as hhehelhellhello.

In order to keep my filtering functionality (I want to only allow alphanumeric characters), I removed my old code ( myText.setFilters(new InputFilter[] { ... }); ) and made a few simple changes to any EditTexts I need filtered so they look like this:

<EditText
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    android:singleLine="true"
    android:imeOptions="actionNext"
    />

The android:digits property does the filtering here. Give it any and all valid characters you want to accept, and for extra credit, throw it in a string resource to reduce copy-and-paste errors.

If you’d like the “Next” button to show on your user’s keyboard for this field, android:imeOptions="actionNext" alone isn’t going to cut it—the android:digits property causes the keyboard to show an Enter key on every device I tested, so throw in android:singleLine="true" to fix it.

$ logout

Sometimes error messages don’t really do their job and tell you how to rectify them, like this one I found today in Android Studio (v0.4.5). While attempting to add a page indicator for my ViewPager, I got “CirclePageIndicator is not allowed here.”

Way to be ambiguous...

As it turns out, this is less about Mama Android telling you where to put your controls, and more about using the correct namespace when adding controls (I had only copied over this class, not the whole project).

Mind your namespaces!

$ logout