September 2011
53 posts
4 tags
Part 95: Accessing the camera
To access a small sized picture from the camera, start an Intent without any extra data:
Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(camera,0);
We’re starting the Intent for a result, the result will have our Bitmap in it. We’re giving it a response int of 0 for brevity.
@Override protected void...
4 tags
Part 94: Finding and playing MP3 files with...
It’s fairly easy to both find and play MP3s in Android.
private List<String> songs = new ArrayList<String>();
File home = Environment.getExternalStorageDirectory(); File f[] = home.listFiles(new Mp3Filter()); if (f!=null && f.length > 0) { for (File file : f) { songs.add(file.getAbsolutePath()); ...
5 tags
Part 93: Using a dimension XML file with different...
If you code in a dimension in dp, sp or whatever, you’ll need to adjust that for the main screen resolutions of android.
Wikipedia has a list of resolutions with devices that use them. 480x800 is quite common at the moment. The HTC Desire and Nexus One use that.
Our default resolutions will be 480x800, and we’ll also deal with 320x480, which is used in the HTC Hero.
In our...
3 tags
Part 92: Using AsyncTask (with ProgressDialog)
We previously used a Handler/Message method to update a ProgressDialog. But we can use a AsyncTask instead. It’s a little cleaner, too.
This can be used for any background task, too. It’s geared towards updating the main UI thread though, for that is not thread-safe.
First setup your ProgressDialog
mProgress = new ProgressDialog(this); ...
3 tags
Part 91: Hiding the soft keyboard
I’ve found I’ve needed to hide the soft keyboard after I’ve ran setText() on a AutoCompleteTextView.
You first need to get the a InputMethodManager, which is the interface between your app and the app’s input device.
You get this via the getSystemService Activity method, passing INPUT_METHOD_SERVICE.
InputMethodManager imm =...
4 tags
Part 90: Launching a specific phone app
Instead of using Intent chooser as before, we can launch a specific application on the phone.
We do this by first looping through all the applications in the system, and getting the ResolveInfo for that application. From there, we can start the Intent.
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType(“text/html”); ...
3 tags
Part 89: Getting screen information
You can get information on the screen size using the Display class. You use the getWindowManager() Activity method to getDefaultDisplay().
Display display = getWindowManager().getDefaultDisplay(); display.getWidth();
You can also get the rotation of the device via getRotation(), which will return a Surface global. Surface.ROTATION_190 is as expected.
Pixel formate and refresh...
5 tags
Part 88: ViewPager aka Homescreen fling/swipe.
If you want the kind of swipe or fling you get on the homescreen, you need to use the ViewPager element. This is in the compatibility pack, if you’re not using 3.0.
You can download this in the Android SDK and ADK manager. If you may already have it. If not, check the available and installed tabs and download it.
Then, in Eclipse, load that by going to Project -> Properties -> Java...
6 tags
Part 87: Gestures: SimpleOnGestureListener
If you want Android to detect when you swipe the screen, you first need to listen for a Touch event.
This gives you access to a MotionEvent, with information about the stop, start, distance etc of the touch. You can pass this to a SimpleOnGestureListener to act on particular swipes.
mViewFlipper.setOnTouchListener(new OnTouchListener() { @Override public boolean...
4 tags
Part 86: Using a ViewFlipper
A ViewFlipper allows us to flip between views, or view groups.
<ViewFlipper android:id=”@+id/ViewFlipper01” android:layout_width=”wrap_content” android:layout_height=”wrap_content”> <TextView android:id=”@+id/TextView01” android:layout_width=”fill_parent” ...
3 tags
Part 85: Making a HTTP request
Strictly speaking, this is Java, not Android. And it’s from here. It uses Apache’s DefaultHttpClient() for its method are a little more user friendly than Java’s UrlConnection.
try { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = null; response = httpclient.execute(new HttpGet(“www.google.com”));
We’re...
3 tags
Part 84: Sending HTML emails
This is method passes the email address, subject and HTML message body to an Intent.
That Intent then asks the user what email program they wish to use, then program then sends that.
The HTML aspect of it simply uses the Html class.
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType(“text/html”); emailIntent.putExtra(Intent.EXTRA_EMAIL, new...
3 tags
Part 83: Reading/picking contacts p2
Previously, we got data about the contact via a Cursor in the passed Intent’s getData() method in onActivityResult().
That cursor, however, only contains some of the contact’s data. Its ID and, its display name, whether its starred or not, etc.
Data such as the phone number and email address are held elsewhere.
We use the ID from the passed Intent to grab the correct row in the...
August 2011
84 posts
3 tags
Part 82: Applying styles
Instead of setting the textColor, gravity etc on the layout XML element itself, we can use styles.
These are XML files that live in res/values/ and have this format:
<?xml version=”1.0” encoding=”utf-8”?> <resources> <style name=”bottomTextStyle” parent=”@android:style/TextAppearance.Medium”> <item...
2 tags
Part 81: HorizontalScrollView
We can get the BBC News app’s [1] horizontal scrolling layout fairly easily.
First define a HorizonalScrolView. Then a LinearLayout within set to ‘horizontal’. The elements within are then set with ‘wrap_content’.
<HorizontalScrollView xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@+id/ScrollView01” ...
2 tags
Part 80: Remove the title bar
In the AndroidManifest.xml file, place this in your <Activity> tag:
It specifies the standard theme, excluding the title bar.
android:theme=”@android:style/Theme.NoTitleBar”
You can also apply other themes to your Activity, or even <application> as whole.
If you add android:theme=”@android:style/Theme.Translucent” as an attribute to your...
3 tags
Part 79: Create a popup menu
This happens when the user pressed the Menu button.
@Override public boolean onCreateOptionsMenu(Menu menu) { boolean b = super.onCreateOptionsMenu(menu); MenuInflater f = new MenuInflater(this); f.inflate(R.menu.options, menu); return b; }
The return value is true if the options menu should be populated.
It populates our options menu via this options.xml file...
6 tags
Part 78: Gallery p2: Gallery image background
The alpha attribute in the last tutorial will not work until each ImageView of the gallery has a background image.
We can set that via passing a standard drawable or resource ID of a drawable as shown
imageView.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.alert_light_frame));
That sets the drawable to a standard android drawable.
The way outlined in the HelloGallery...
3 tags
Part 77: Gallery p1: Setup
The gallery layout is very similar to the GridLayout widget.
The main difference is the style attribute that we will deal with later.
<Gallery
android:id=”@+id/gallery”
android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:animationDuration=”50” android:unselectedAlpha=”0.5” />
...
4 tags
Part 76: AutoComplete spinner
The XML of this has a few interesting attributes:
<AutoCompleteTextView android:id=”@+id/autocomplete_country” android:layout_width=”fill_parent” android:completionHint=”hiya” android:completionThreshold=”1” android:layout_height=”wrap_content”/>
The completeionHint attribute specifies a...