Jump to content

[Résolu] Textview: Changer la couleur quand je clique dessus


Z!dd0

Recommended Posts

Salut!

J'ai un LinerLayout avec une TextView et une ImageView J'aimerais faire la meme chose qu'avec un bouton: wquand on cliques, le background change. J'arrive a faire ca quand je focus sur l'objet (avec la trackball), mais pas quand je clique :(

J'ai essaye plein de choses differentes, mais rien n'a fonctionne... Des idees?

merci d'avance

Mon Linear Layout:

        android:orientation="horizontal"
       android:id="@+id/widgetProvider"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:focusableInTouchMode="true"
       android:clickable="true"
       android:layout_margin="1dip"
       android:background="@drawable/textview">

           android:id="@+id/imgProvider"
           android:layout_width="60dip"
           android:layout_height="fill_parent"
           android:layout_marginRight="5dip"
           android:src="@drawable/icon" />

               android:layout_width="fill_parent"
               android:layout_height="fill_parent" 
               android:maxHeight="10dip"
               android:text="@string/provider"
               android:gravity="center_vertical">


Mon background qui change (textview.xml)





Edited by Z!dd0
Link to comment
Share on other sites

Fait le avec un bouton plutôt qu'un textview.

Ensuite dans ton code tu rajoutes.

Imaginons que ton bouton se nomme : bouton

bouton.setBackgroundResource(R.***.textview);

Remarque ca doit aussi fonctionner avec une textview ;)

Link to comment
Share on other sites

Comme ca tu veux dire???

        android:orientation="horizontal"
       android:id="@+id/widgetProvider"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:focusableInTouchMode="true"
       android:clickable="true"
       android:layout_margin="1dip"
       android:background="@drawable/textview">

           android:id="@+id/imgProvider"
           android:layout_width="60dip"
           android:layout_height="fill_parent"
           android:layout_marginRight="5dip"
           android:src="@drawable/icon" />

               android:layout_width="fill_parent"
               android:layout_height="fill_parent" 
               android:maxHeight="10dip"
               android:text="@string/provider"
               android:gravity="center_vertical">


Link to comment
Share on other sites

Oui comme ça en séparant bien :

                            android:id="@+id/btnProvider" 
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" 
               android:maxHeight="10dip"
               android:text="@string/provider"
               android:gravity="center_vertical">

Ensuite il faut que tu codes ton click avec un OnClickListener

Tu peux le faire avec un TextView aussi puisque TextView dérive de View qui a OnClickListener (Button dérive de TextView aussi :p)

Edited by Fluckysan
Link to comment
Share on other sites

Merci a tous de vos reponses :)

En fait, on peut voir dans le layout que mon element principal se compose d'une image (a gauche) et d'une zone de texte (a droite). Basiquement, c'est ca:

*********************************

| | |

| Image | Texte Texte |

| | |

*********************************

Ce que je veux, c'est que quand je clique sur le tout (n'importe ou), cela fonctionne comme un bouton et ca fasse une action.

J'arrive a faire ca, sauf que cet element ne se highlighte pas. Il ne change pas de couleur quand on appuie dessus alors que j'ai bien mentionne dans le layout le "state_pressed"

J'espere que vous comprenez un peu mieux mon probleme ;)

Link to comment
Share on other sites

Bah il suffit d'utiliser un Bouton et de lui affecter une image avec l'attribut drawableLeft :

                                       android:drawableLeft="@android:drawable/ic_menu_add"
                                       android:layout_width="fill_parent"
                                       android:layout_gravity="center_horizontal"
                                       android:id="@+id/BtnCreateAlbum" />

non ?

Link to comment
Share on other sites

Oui, j'avais jete un oeil la-dessus.

J'ai resolu le probleme: en fait, j'avais un onTouchListener au lieu d'un onClicklistener, et le highlight n'etait pas possible.

Avec le onClickListener, tout se passe bien!

Merci beaucoup!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...