Problème avec selector

Résolu/Fermé
YameFAZE Messages postés 201 Date d'inscription mardi 24 avril 2012 Statut Membre Dernière intervention 14 mars 2021 - 24 mars 2017 à 15:26
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 - 30 mars 2017 à 09:03
Bonjour,

J'ai un tout petit problème. J'ai créé une animation de bouton grâce à un fichier XML contenant un
selector
. Ci-dessous le code.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/bouton_off" />
<item android:state_pressed="true" android:drawable="@drawable/bouton_on" />
<item android:state_focused="true" android:drawable="@drawable/bouton_off" />
<item android:state_enabled="true" android:drawable="@drawable/bouton_off" />
</selector>


L'animation fonctionne mais pourtant mon fichier est souligné en rouge dans Android Studio. Le message d'erreur est :
Selector XML file should be in either "animator" or "drawable", not "anim". Element selector must be declared.
. Je ne comprends pas le problème.
A voir également:

3 réponses

BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 3 895
24 mars 2017 à 16:15
Un selector n'est pas une animation à proprement parlé. C'est un object qui permet de décrire les différents états d'un composant graphique, bouton notamment, utilisable comme un Drawable.
D'après le message, tu l'utilises comme ceci:
R.anim.selector
. Il faut l'utiliser comme ceci:
R.drawable.selector 

0
YameFAZE Messages postés 201 Date d'inscription mardi 24 avril 2012 Statut Membre Dernière intervention 14 mars 2021
29 mars 2017 à 23:13
Merci. En fait je n'utilise pas java pour contrôler l'état du bouton. Tout se fait par xml.

Voici le code du bouton dans le layout :
<Button
android:id="@+id/startActivity_boutonInscription"
android:background="@anim/bouton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:padding="3dp"
android:gravity="center"
android:text="@string/startActivity_boutonInscriptionSTRING"
android:textColor="@color/texte_bouton" />


Le code java dans l'activité qui permet le passage d'une vue à l'autre :
final Button startActivity_boutonInscription = (Button) findViewById(R.id.startActivity_boutonInscription);
startActivity_boutonInscription.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent lien_Start_InscriptionImperative = new Intent(Start.this, InscriptionImperative.class);
startActivity(lien_Start_InscriptionImperative);
}
});


Et le code du fichier xml qui contrôle l'animation et qui se trouve dans le dossier "anim" :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/bouton_off" />
<item
android:state_pressed="true"
android:drawable="@drawable/bouton_on" />
<item
android:state_focused="true"
android:drawable="@drawable/bouton_off" />
<item
android:state_enabled="true"
android:drawable="@drawable/bouton_off" />
</selector>
0
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 3 895
30 mars 2017 à 09:03
Ma réponse ici est valable, mais si c'est dans le code xml, il faut utiliser
@drawable/button
et mettre ton selector dans le dossier drawable
0