Tag: Android Studio

Programming Stuff

Google Glass not showing in Android ADB utility

Recently my Google Glass arrived … Needless to say, I was excited and ready to start working on building apps for Glass.

I unbox and fire up Glass, go through the initial setup steps, and turn on debug (Settings -> Device Info -> Turn on debug). So far, so good.

I then walk over to my PC, fire up the Android SDK manager to confirm that I have the latest revision of the Google USB Driver (Rev. 9 at the time of this writing).

GoogleUSBDriver_AndroidSDKManager

I then connect Google Glass to my PC’s USB port and wait for the USB setup window to tell me Glass is ready to use.

The USB setup appears to be going well but just as the setup process is about to finish, my PC displays a message indicating that there were some problems. At first I was somewhat concerned but, things start looking better…

The Auto Play window opens showing options for the Glass device…

GlassAutoPlayWindow

… and Glass 1 is suddenly listed as an option in the Windows File Explorer.

GlassFileExplorer

Using both the Auto Play window and File Explorer I’m able to browse around the device file system. I can even copy files between the Glass file system and my PC file system.

Clearly the USB setup window was mistaken. 🙂

Feeling confident, I open up a command prompt and enter the command to display the list of Android devices currently connected.

adb devices

Which then displays the following.

List of devices attached

Notice that no devices are listed. Clearly the USB setup had not completed successfully … there is a problem with the USB driver.

If ADB doesn’t see that Glass is attached, tools like Android Studio, Eclipse, DDMS, etc. are not going to see the device either. We need to fix this.

Fixing the USB Driver

For the USB Driver to work with Glass, some tweaks need to be made to the USB driver’s android_winusb.inf file (located in [android sdk install folder]\extras\google\usb_driver ). The list of steps to manually modify the file can be found here.

Alternatively, you can download a modified version of the USB driver from AndroidFileHost.com.

If you choose to use this download be extremely careful! USE THIS DOWNLOAD AT YOUR OWN RISK!!

This is not an official USB installation but rather one created by a person who has been kind enough to share it. I’m using this download on my development PC and it’s working great. That said, you still need to be very careful because there’s always the potential for someone to do something malicious.

Once you have the modified USB driver files (whether you manually make the changes or you download them) you can easily update your PC to use the modified USB Driver installation.

  • From the Control Panel, open the Device Manager
  • Locate the entry for Glass. I believe you’ll find it under Other Devices
  • Right-click on Glass and select Update Driver Software
  • When prompted with How do you want to search for driver software? select Browse my computer for driver software
  • In the Browse for driver software on your computer window, browse to the folder that contains the modified driver files. Be sure the checkbox Include subfolders is checked then click Next

You’ll probably receive a warning that the USB driver can’t be verified. As long as you’re confident that you’re dealing with a safe driver installation (either you made the changes to the android_winusb.inf file yourself or you’ve validated that the USB driver files you downloaded are safe)  allow the installation to run.

Once the USB driver installation completes … Your PC is ready to connect to Glass.

You can verify that the USB driver change worked by again running the adb command.

adb devices

But this time, you should see a device listed.

List of devices attached
015DA77204015006 device

And now you’re ready to go … time to start creating those killer apps for Glass !!

Hopefully Google will soon release a USB driver that supports Glass without requiring these extra steps. But until they do, these steps allow us to begin working with Glass right away.

Jim is currently working on an online course for Pluralsight about creating apps for Glass with the Glass Development Kit.  That class should be available at the end of April 2014.

Remember though that developing native apps for Glass requires a solid understanding of Android app development so checkout Jim’s many Android programming courses and cross-platform iOS/Android programming courses at Pluralsight.

Programming Stuff

Lambda Expressions in Android Studio

I’m often asked by students from my Android courses as to how I’m able to use Lambda expressions like the following in my Android programs.

Lambda Code Folded

The short answer is: I’m not able to use them. 🙂 What I typed was the following.

Lambda Expanded

The Lambda expressions appear in the editor due to a feature of Android Studio called “Code Folding”.

Android Studio does this sort’a thing in a number of scenarios. You can tell the Lambda expression is a result of code-folding by the highlight that appears over the folded portion of code.

You can also easily identify that the Lambda expression is a result of code folding because the editor will display a plus-sign to the left of the statement. You can view the full, original statement by clicking on the plus sign.

Lamba expression substitution is controlled by the following value in the Android Studio Settings dialog.

Code Folding Settings

As this is my first post of 2014, let me take this opportunity to wish everyone … Happy New Year!!

Programming Stuff

AnDevCon Slides and Code

Thanks to everyone for attending the AnDevCon Tutorial and Class this week. Here’s the info from both.

Understanding Android Studio and Android Developer Toolset

Integrate Google Cloud Printing Into Your Android App

You must set the user id and password constants in the GmailHelper class or the program will not work

Thank you to everyone who attended. Please feel free to contact me with any questions.

ADCLogo_large

Programming Stuff

Android for .NET Developers Series Complete!

Part 4 of my four-part series on Android programming for developers with a .NET background just went live today.

UnderstandingTheAndroidPlatform_WithLogo

 

This course delves into the unique features of the Android platform and how those features and the related components affect the way apps are created.

I hope you’ll check out this course and the whole series if you haven’t already seen it. I think you’ll be happy you did.

Here’s the complete series

Part 1: Getting Started

Android for ,NET Developer Series: Getting Started

 

Part 2: Building Apps with Android Studio

Apps with Android Studio

 

Part 3: Adopting the Android Mindset

AndroidMindsetBanner_WithLogo

 

Part 4: Understanding the Android Platform

UnderstandingTheAndroidPlatform_WithLogo

Programming Stuff

Android Updatable Swipe Navigation with FragmentStatePagerAdapter

Android Studio makes adding swipe navigation to your Android apps easy through the “Scrollable Tabs + Swipe” option of the Navigation Type selection in the New Project Wizard. Choosing this option works great as long as you have a static list of screens that the user “swipes” between.

3095_05_03

When Static Screen Lists Aren’t Enough

The way swipe navigation works is that each screen is represented by an instance of a Fragment-derived class. These Fragment classes are then managed by a PagerAdapter-derived class which makes them, in effect, a scrollable list. Passing that PagerAdapter to a ViewPager presents that list in a way that the user can use a swipe-motion to move between screens.

The code generated by Android Studio provides a custom PagerAdapter class that derives from  FragmentPagerAdapter. Basically all one has to do is override the getItem method and return the desired Fragment for each screen position (you’ll also need to override getCount and getPageTitle but those are super simple). What happens though is that the FragmentPagerAdapter class is designed such that once a Fragment instance is returned for a given position that Fragment is permanently in that position. Once this happens, you can, of course, make changes to the contents of the Fragment but there’s no way to provide a different Fragment instance for that position which is often what’s necessary.

The comments in the generated class indicate that using FragmentStatePagerAdapter instead of FragmentPagerAdapter as the base class allows for more dynamic management of the Fragment instances. Reading the FragmentStatePagerAdapter documentation indicates that we can notify our FragmetStatePagerAdapter instance of a change in the list of screens (in other words that we’d like to use new Fragment instances) by calling the notifyDataSetChanged method. But that’s only part of the story.

Once you call this method what you’ll normally see is that screens that were previously visited still have the old Fragment instances but screens being visited for the first time have the new Fragment instances. If you have a large number of screens and scroll back and forth between them you may see some of the older screens eventually show a new Fragment instance.

Not really the consistent user experience we’re looking for 🙂

So what’s the problem?

What’s happening is that FragmentStatePagerAdapter is trying to be efficient and only create new Fragment instances when necessary. To determine when to request new Fragment instances after a call to the notifyDataSetChanged method, FragmentStatePagerAdapter calls its getItemPosition method to see if an existing Fragment can be used in its current or possibly a different position without having to recreate it. What we have to do is tell the FragmentStatePagerAdapter instance that we don’t want to use the existing Fragment instance.

To do that we need to override getItemPosition as follows

public int getItemPosition(Object object) {
 // Causes adapter to reload all Fragments when
 // notifyDataSetChanged is called
 return POSITION_NONE;
}

By returning POSITION_NONE we’re telling the FragmentStatePagerAdapter instance to discard that Fragment and just create new ones for every screen position.

Summary: How To Create Updatable Swipe Navigation

To summarize what to do, here’s the list of steps…

  1. Select “Scrollable tabs + swipe” as the Navigation Type when generating your project in Android Studio
  2. Change the FragmentPagerAdapter base class to FragmentStatePagerAdapter
  3. Override the getItemPosition method to return POSITION_NONE
  4. Call notifyDataSetChanged in your code when you’d like to load new Fragment instances.

And with that, you have the ease of swipe navigation with the ability to reload Fragments instances as needed

Adapted from Jim’s Pluralsight course Android for .NET Developers: Adopting the Android Mindset

AndroidMindsetBanner_WithLogo

Checkout Jim’s latest book: Creating Dynamic UI with Android Fragments

Creating Dynamic UI with Fragments

Programming Stuff

How to Clean an Android Studio Project

You’ve probably noticed that Android Studio doesn’t have a “clean” menu option. But we know that, like any build environment, there are intermediate files created that we may want to occasionally remove.

It turns out the solution is the command line. Simply open a command line in your project’s root folder (Usually named something like MyAppProject) and run the following command

gradlew clean

You’ll see something like this…

gradlew_clean

Notice that you get a bit of an unexpected message, “BUILD SUCCESSFUL”. This simply indicates that it has gone through and successfully cleaned out the intermediate files. I’ve found that when dealing with larger projects its not uncommon to reduce the amount of disk space used by the project by 70% or more.

So a bit of an unexpected solution (especially for those who’ve been in a Windows environment for a long time) but an easy one.

To learn more about Android programming, checkout Jim’s Android courses on Pluralsight.

Jim's Android courses on Pluralsight

Jim’s latest book, “Creating Dynamic UI with Android Fragments” is now available on amazon.

Creating Dynamic UI with Fragments

Programming Stuff

Switch to Android Studio 0.2.0 a little bumpy

Android Studio 0.2.0 update … encountering issues with both new AND existing projects.

I’m fortunate that  using the latest and (sometimes not so) greatest is actually my job. With that I’ve been able to heavily use Android Studio ever since it was first released. To my knowledge, I’ve installed and heavily used every public update since the release.

Everyone of those updates went pretty smooth … until this one.

The switch to Android Studio 0.2.0 has, by far, been the bumpiest upgrade yet. After installing it…

  • I can’t generate new projects
  • I can’t build existing projects.

Not much I can get done without being able to do one or the other. 🙂

The good news is that resolving these issues was pretty easy.

Can’t generate new projects

This issue appears to be specific to folks using Android Studio on Windows.

I initially let Android Studio handle the install as I had always done previously. Once the install completed, any attempt to create a new project resulted in the following error…

CantGenProjectBadDependencyMSg

Basically, the message indicates that Android Studio can’t find a dependency.

The solution turned out to be that a fresh install is requried ….

  1. I exited all instances of Android Studio
  2. I changed the name of the existing installation folder
    • The default installation folder is
      C:\Users\[user name]\AppData\Local\Android\android-studio
  3. I then downloaded and installed the latest Android Studio installer

And that took care of the problem

Can’t build existing projects

This turned out to be a two-fold problem for me.

The first issue has to do with a fairly well documented change that’s been made to Android Studio. They’ve changed to a new version of Gradle that’s not backward compatible. As a result an attempt to build a project that was created with a prior version of Android Studio shows the following error.

GradleBuildIssue

The fix is to change the version of Gradle that’s referenced in your project’s build.gradle file (located in top-level of project folder). If you click on the “Search in build.gradle files” link in the above dialog it’ll open the build.gradle file for you. Or you can just open the build.gradle file yourself. It’s quite small and easy to navigate.

To fix the problem simply change the line that reads

classpath ‘com.android.tools.build:gradle:0.4

to

classpath ‘com.android.tools.build:gradle:0.5.+’

This will cause Android Studio to use the required version of Gradle.

But trouble continues…

With that fix made, I would then get an error indicating

FAILURE:Could not determine which tasks to execute

The problem is related to extraneous entries that were added to the [ProjectName].iml file (in your project’s top-level folder) by earlier versions of Android Studio.

To fix the problem, exit Android Studio and open the [ProjectName].iml file in an editor and delete the entire component element with the name “FacetManager”.

This changes the file from…

<?xml version="1.0" encoding="UTF-8"?>
<module external.system.id="GRADLE" type="JAVA_MODULE" version="4">
  <!-- *** Remove From here *** -->
  <component name="FacetManager">
    <facet type="android-gradle" name="Android-Gradle">
      <configuration>
        <option name="GRADLE_PROJECT_PATH" value=":" />
      </configuration>
    </facet>
  </component>
  <!-- ***  To Here   *** -->
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <excludeFolder url="file://$MODULE_DIR$/.gradle" />
      <excludeFolder url="file://$MODULE_DIR$/build" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
</module>

to

<?xml version="1.0" encoding="UTF-8"?>
<module external.system.id="GRADLE" type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <excludeFolder url="file://$MODULE_DIR$/.gradle" />
      <excludeFolder url="file://$MODULE_DIR$/build" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
</module>

And with that … SUCCESS!!  … I’m now able to build the project without difficulty.

Wrap Up

So the cleanup isn’t too difficult but does require some work. If you’re encountering issues other than those I’ve mentioned above the Android Studio folks have posted a couple of helpful pages addressing a variety of issues

To Learn more about Android programming, checkout Jim’s courses at Pluralsight.

PSAndroid_216x155

Programming Stuff

Android Studio 0.1.9 Brings the New Resource Directory Dialog Back

If you’ve been working with the last few builds of Android Studio you know that the ability to create new resource directories was broken. The only work-around was a hacky process of going into some of the settings dialogs and creating the folder from there.

Well the 0.1.9 patch just came out and the New Resource Directory dialog is back (actually it came back in the short-lived 0.1.7 release from a few days ago).

NewResourceFolderDlg

I love the way that this is implemented because it takes care of all of that subtle folder naming that goes into resource specialization.

Android’s support for resource specialization is one of the real powers of the platform in my opinion but manually building those names can be easy to mess up. With this dialog you simply select the resource type, the qualifiers and then enter or select (depending on the qualifier) the value to qualify by.

This is a great feature and goes a long way to making one’s life simpler.

For more information on working with Android Studio, check out Jim’s latest course:

Apps with Android Studio