Friday, August 29, 2014

Switching an Android Menu Icon via Themes

First, edit attrs.xml to include the name:  <attr name="undo_icon"

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="undo_icon" format="reference" />
</resources>

Then, edit the styles.xml to define the style: <item name="undo_icon">

<resources>
    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->     
    </style>

    <!-- Application theme. -->
    <style name="AppThemeLight" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="undo_icon">@drawable/halo_dark_content_undo</item>       
    </style>
    <style name="AppThemeDark" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="undo_icon">@drawable/halo_light_content_undo</item>
    </style>
</resources>

Finally, define the menu.xml: android:icon="?undo_icon"

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/menu_undo"
        android:icon="?undo_icon"
        android:title="Undo"
        android:titleCondensed="Undo">
    </item>
</menu>

In the Manifest: android:theme="@style/AppThemeLight" >  OR switch dynamically via Context.setTheme

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppThemeLight" >


To reference in code:

        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.
undo_icon, typedValue, true);
        btnUndo.setImageResource(typedValue.resourceId);


Thursday, August 28, 2014

How to fix Android SDK Content Loader stuck at 0% in Eclipse

Have you encountered the case where Eclipse hangs upon startup, in particular when you are developing an Android applications with Android SDK?

When that happens you see "Android SDK Content Loader" stuck at 0% in the bottom right hand of the Eclipse status bar.

There are four things you can try...

Solution 1:
  1. Make sure that eclipse is not active. If it is active kill eclipse from the processes tab of the task manager 
  2. Check if the adb process is running. If so, kill the adb process, and restart Eclipse. 

 Solution 2: (Works best for me, and likely the safest)
  1.  Make sure that eclipse is not active. If it is active kill eclipse from the processes tab of the task manager
  2. From the command line run: C:\eclipse\eclipse.exe -clean

Solution 3:
  1. Make sure that eclipse is not active. If it is active kill eclipse from the processes tab of the task manager
  2. Open %USERPROFILE%/ (You can locate this folder from desktop) (or paste it into Explorer on windows)
  3. Go to .android folder (This may be a hidden folder)
  4. Delete the folder "cache" which is located inside .android folder
  5. Delete the file ddms.cfg which is located inside .android folder
  6. Start Eclipse
Solution 4: (Most drastic, and one I have not tried)

Go to your workspace directory \workspace\.metadata\.plugins\org.eclipse.core.resources\\.projects

  1. Copy .projects folder to make a temporary backup.
  2. Now Delete .projects folder from workspace directory. (you will not loose your projects)
  3. Start Eclipse and wait for all progress ends at right/bottom corner. Once completed all processes, shutdown Eclipse.
  4. Paste .projects folder which you have backup earlier to \workspace\.metadata\.plugins\org.eclipse.core.resources\ directory. Overwrite existing .projects folder.
  5. Start Eclipse again. And all will work.
In above scenario Eclipse will automatically find your earlier projects. You do not have to import them manually.

Good Luck