How to make a selector in Android Studio

34,534

Solution 1

Though it is already answered in the comment, enter image description here

Solution 2

First of all, there will be no drawable folder when establish a new Android Project in Android Studio likes Eclipse does. Right click on res folder--> New--> Android Resource Directory--> resource type--> drawable, and now you get a drawable folder, and then, you can add your new drawable resource file, selector, shape and .etc

Share:
34,534

Related videos on Youtube

Menachem Hornbacher
Author by

Menachem Hornbacher

Updated on July 09, 2022

Comments

  • Menachem Hornbacher
    Menachem Hornbacher almost 2 years

    While taking a course, I was instructed to make an xml selector for a Button. The course said to make a new Android xml in Eclipse, but i am using Android Studio.

    Also when I custom write the code it gives me an error. It says "element selector must be declared".

    as you can see selector is an error

    Got code from here: Android: How to Make A Drawable Selector.

    Does anyone know how to do this in Android Studio?

    • Marko
      Marko over 9 years
      Is that file of yours under res/drawable/ folder?
    • ataulm
      ataulm over 9 years
      Where does it give you the error "element selector must be declared"? Can you put it in a screenshot? The XML is correct (though the states are incorrect given the naming of the drawable).
    • Menachem Hornbacher
      Menachem Hornbacher over 9 years
      yes it is, and that is a direct quote from mousing over the selector word
    • Marko
      Marko over 9 years
      Can you please check again that your file is not under res/values/ folder, but in res/drawable/ folder where it should be! I have checked, and values folder, gives that exact error.
    • Menachem Hornbacher
      Menachem Hornbacher over 9 years
      yah, messed up. but do you know how to add one within the GUI?
    • Marko
      Marko over 9 years
      Just right click the res/drawable/ folder -> New -> Drawable resource file
    • Menachem Hornbacher
      Menachem Hornbacher over 9 years
      got it thanks for your help
    • Sudhir Singh Khanger
      Sudhir Singh Khanger over 7 years
      @Marko what is the status of anim or anim-v21 folders? Why doesn't it work in anim folder?
    • Marko
      Marko over 7 years
      @SudhirKhanger I guess it is because the Android framework expects you to put certain types of things into their designated folder (drawables, selectors, vector images into drawable folder, strings, dimensions, integers into values folder, etc..). Anim folder doesn't have access for instance to <selector> tag mentioned above, but has access to it's own set of tags (like <set>, <animator>, <objectAnimator>,...). My guess is for reasons like, optimization in loading resources, etc..