In this recipe, we will modify the default search text in the quick search widget. The aim of this recipe is to make the reader comfortable with making changes, compiling them, and testing those changes.
The search widget's code is in the directory named QuickSearchBox
under ANDROID_SRC/packages/apps/
. Navigate to this directory and open up ANDROID_SRC/packages/apps/QuickSearchBox/res/drawable/text_field_search_empty_google.xml
.
Edit the XML layout file by replacing:
<item android:drawable="@drawable/hint_google" />
with
<item android:drawable="@drawable/packt" />
Now, use a popular image editor to create an icon of your choice with dimensions of 97 x 40 pixels. We create a Packt logo variant for this task in GIMP. Make sure you save the image as a PNG type. Copy this image to three locations:
drawable-hdpi
,drawable-ldpi
, anddrawable-mdpi
.Now we will build the new
QuickSearchWidget
application. In a terminal, as usual, navigate to the Android source directory, include the environment setup and lunch the emulator target. Issue the following command:mmm packages/app/QuickSearchWidget
This will build only the search widget. After building is complete, start the emulator and drop to an
adb
shell. We need to remount the system partition as read/write, since system applications are installed on to this partition which is mounted read-only. To be able to write a new executable of theQuickSearchWidget
application, and without rebuilding and reflashing the entire operating system, we will simply "push" the generated APK onto the remounted directory.We need to find out where the
/system
partition is mounted. In anadb
shell, execute the following command:cat /proc/mtd
The output will be similar to the following:
dev: size erasesize name mtd0: 0c200000 00020000 "system" mtd1: 0c200000 00020000 "userdata" mtd2: 04000000 00020000 "cache"
We see that
/system
exists inmtd0
. To remount as read/write, in anadb
shell:mount -o rw,remount -t yaffs2 /dev/block/mtd0 /system chmod 777 /system/app
Now, exit the
adb
shell and navigate toANDROID_SRC/out/target/product/generic/system/app
. This is where the built APK is stored.In a terminal, execute the following command:
adb push QuickSearchWidget.apk /system/app
This will install the new widget. The change should be visible on the emulator:
The mmm
command selectively builds code. The argument to it is the path to the source directory containing an Android.mk
file. The path should end with the name of a target that can be built. For example, if you open the Android.mk
file, the target is identified by the LOCAL_PACKAGE_NAME
tag.
As we have seen in earlier recipes, the system partition is mounted as read-only for security reasons. To install a system application, we need to remount this. The remount can only be done by a root user. On the emulator, ADB runs as root by default.
The side effect of simply pushing an APK to /system/app
results in its installation.
The different drawable
directories are used based on different screen densities. This has the same meaning as the directories used when writing SDK-based apps.