0

I saw a number of examples that seemed a bit overkill for what I needed, so I just wanted to do something simple like:

In my activity I would do this:

ProgressBar bar = bar=(ProgressBar)findViewById(R.id.progress);

and then in my layout I would do this:

<ProgressBar android:id="@+id/progress"
    style="?android:attr/progressBarStyleHorizontal"
    android:src="@drawable/ajax-loader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

and I had put this image http://problemio.com/img/ajax-loader.gif into my images directories.

But I got a compile error that .gif files are not supposed to be in those directories. Here is the error:

res\drawable-hdpi\ajax-loader.gif: Invalid file name: must contain only [a-z0-9_.]

I am also not sure whether I am creating the Progress bar layout correctly. What would be the right way to go for me here? Or am I totally off track? :)

Thanks!

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Genadinik
  • 18,153
  • 63
  • 185
  • 284

1 Answers1

1

Your drawable name is incorrect as error message clearly states. You have to name your images like this ajax_loader NOT ajax-loader. The reason for this is ADT creates R array with auto-assigned IDs of used resources. And since R is just regular Java code and all identifies are build based of file names therefore name like this ajax-loader is invalid as you should be able to reference this drawable as R.drawable.ajax-loader simply means substract loader variable from R.drawable.ajax variable. And this is not what you want (for the same reason you are not able to have i.e. spaces in resource names). Another thing is that you should use PNG as your resources, not GIF. If you want animation, please convert your GIF animation to separate images and use Frame Animation feature of Android.

So to solve your issue, you shall rename your resource to be ajax_loader.gif as this is correct naming scheme. But then, it will be rejected because it is GIF not PNG. My suggestion is to load this GIF into i.e. GIMP, save each frame as separate drawable (i.e. ajax_loader_01.png, ajax_loader_02.png etc). And the create ajax_loader.xml in res/drawable folder with content like:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/ajax_loader_01" android:duration="200" />
    <item android:drawable="@drawable/ajax_loader_02" android:duration="200" />
    ...
</animation-list>

and then use this as your drawable in layout (so you should reference R.drawable.ajax_loader, not frames separately). Adjust duration to match GIF animation and you are done.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    That will fix that error, after that you'll run into the fact that Android doesn't support animated GIFs for drawables. At least [not easily](http://stackoverflow.com/questions/3660209/android-display-animated-gif). – dmon Sep 05 '12 at 15:46
  • Oh, I was not correcting you, just adding more color :) Good edits too. – dmon Sep 05 '12 at 15:48
  • So should I just get a different image which is a .png spinner? Or can spinners only be gifs? Whats the reasonable approach here? Thanks! – Genadinik Sep 05 '12 at 15:54
  • @WebnetMobile.com by any chance, would you have the loading images that you could possibly share? I keep searching google for such images that would make an animation together and can't find anything suitable. – Genadinik Sep 05 '12 at 16:45
  • @Genadinik no, not really, but google.com -> images and "busy icon" or so would give you enough. I also use different approach with still image like `ic_menu_refresh` (see platfroms/xxx/res/drawable-mdpi in your SDK folder) and then apply endless rotate animation to it. – Marcin Orlowski Sep 05 '12 at 16:57