How to make a selector in Android Studio
34,534
Solution 1
Though it is already answered in the comment,
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
Related videos on Youtube
Author by
Menachem Hornbacher
Updated on July 09, 2022Comments
-
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".
Got code from here: Android: How to Make A Drawable Selector.
Does anyone know how to do this in Android Studio?
-
Marko over 9 yearsIs that file of yours under res/drawable/ folder?
-
ataulm over 9 yearsWhere 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 over 9 yearsyes it is, and that is a direct quote from mousing over the selector word
-
Marko over 9 yearsCan 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 over 9 yearsyah, messed up. but do you know how to add one within the GUI?
-
Marko over 9 yearsJust right click the res/drawable/ folder -> New -> Drawable resource file
-
Menachem Hornbacher over 9 yearsgot it thanks for your help
-
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 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..
-