Posts in the “android” category

Android flashcards

One thing I learned about ten years ago is that when I need to memorize things, flashcards work really well for me. More recently, because I often bounce between many technologies, I have been making flashcards as a way of bringing me back up to speed after I’ve been away from a technology for a while.

The image shows one example of this, where I created a stack of flashcards to help me remember/relearn Android, which I haven’t used in several months. In this case I also have my Android cheat sheet to fall back on, but even then I still like using the flashcards. I think the theory is that rather than reading something passively, flashcards force you to try to recall something, and that’s a much more active way of using your brain and memory.

An Android Java, JSON, and Twitter REST API example

I don't get to parse too much JSON code with Java because the biggest JSON source I work with is Twitter, and I always use the Twitter4J project to interact with their web services. But a few days ago while working on an Android project, I just wanted to access their "Twitter Trends" REST service, and I used Java and the json.org Java library that comes with Android to parse the Twitter Trends JSON feed like this:

Moto 360 - Pull down settings menu not working

Important note: I wrote the following text just after receiving my Moto 360, charging it, and connecting it to my Android device. At that time these instructions were 100% accurate. But what I learned the next morning is that the Moto 360 user interface changes on the second day (or perhaps within the first 24 hours). I don’t know why it works this way, but in the morning I received a notification saying that pressing the power button now brings up the Settings menu as well as a menu of apps, and sure enough it does.

How to put a background image behind a translucent/opaque TextView in Android

I just had an unusual Android need: I wanted to put an image behind a TextView, where the TextView was occupying the fullscreen. But, I didn’t want the image to be completely visible, I wanted the TextView to be mostly opaque so that you would only get a hint of the image. You can think of this as wanting a watermark image behind a large text editing area.

Jumping right to the solution, this Android XML layout code gave me the solution:

Android permissions needed to access the internet and check the network state/status

Not much to see here, just a couple of Android permissions that an Android app may need to access the network/internet:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

As the names imply, the first permission requests access to the internet, and the second permission is needed to check the internet status/state. These permissions need to be put in an Android app’s AndroidManifest.xml file.

How to show an Android Toast message from a fragment

This is how you can show an Android Toast message from a Fragment:

Toast.makeText(getActivity(), "Click!", Toast.LENGTH_SHORT).show();

As shown, don’t forget to call .show() at the end of the makeText method. Forgetting to call show() is a common mistake.

If it helps to see this in more context, this is a complete onListItemClick method from a ListFragment subclass I’m currently working on:

How to set an image on an Android ImageButton

To set an image on an Android ImageButton, you basically just need to put your image in the “res/drawable” directories, and then add this tag to your ImageButton XML definition:

android:src="@drawable/my_image"

That assumes the image file is named my_image.png.

Here’s a full ImageButton example to demonstrate this:

Android: How to create a new Fragment and put it in a layout

I’ve been working on an Android app that uses a navigation drawer, and uses fragments for each item in the drawer that you tap on. One of the items in the nav drawer is a “Preferences” item, so when I tap on that item, I run the following code from my nav drawer code:

Where should the 'assets' directory be when using Android Studio?

Android FAQ: Where should the ’assets’ directory be when using Android Studio?

Solution: If you want to include an assets folder in your project when using Android Studio, create the folder as src/main/assets, i.e., as an assets folder under src/main.

You can later open files in the assets folder using code like this:

InputStream is = getBaseContext().getAssets().open(relativeFilename);

where relativeFilename is the name of your file, like foo.jpg.

What does Android isScrollContainer do? (ScrollView, TextView, EditText)

Android FAQ: What does the Android isScrollContainer XML setting do?

From the Android docs:

Set this if the view will serve as a scrolling container, meaning that it can be resized to shrink its overall window so that there will be space for an input method. If not set, the default value will be true if “scrollbars” has the vertical scrollbar set, else it will be false.

How do I make my Android app display in landscape mode only?

Android FAQ: How do I make my Android app display in landscape mode only?

To get an Android application to display in landscape mode only (to lock it in landscape mode), add this line to your Activity definition in the AndroidManifest.xml file:

android:screenOrientation="landscape"

For example, I’m writing an Android football game right now, and this is the definition for my main activity in the Android manifest file: