Pressed android button state

I’ve been following a tutorial that explains how to use background for a button with different states but it doesn’t seem to work :S

Here is my code :

  • write and read strings to/from internal file
  • How to run activity in background in Android
  • Enums and android annotation intDef
  • Android CSS position:fixed after a device rotate
  • Android 5(HTC) EACCES (Permission denied)
  • How to let user know he/she needs to install another app that my app depends on
  •     <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/boutonn" android:state_window_focused="false"/>
        <item android:drawable="@drawable/boutonnpousse" android:state_pressed="true"/>
        <item android:drawable="@drawable/boutonnpousse" android:state_focused="true"/>
        <item android:drawable="@drawable/boutonn" android:state_focused="false" 
        android:state_pressed="false" />
    
    </selector>
    

    This is an xml code that I’ve placed in my drawable folder, here is a part of the xml of the activity that uses these buttons :

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/backgrounddd"
        android:orientation="vertical" >
    
                <Button
                    android:id="@+id/bNoteRemind"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center"
                    android:background="@drawable/imagebutton1" /> 
        ...
    

    And here is the java class :

    public class MenuPrincipal extends Activity {
    
        Button NoteRemind;          
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
    
            //on lui associe le layout menuprincipal.xml
            setContentView(R.layout.menuprincipal);
    
            NoteRemind = (Button) findViewById(R.id.bNoteRemind);     
    
            // Si on choisit de rédiger une nouvelle task on va être rediriger sur l'activité NoteReminder
    
            NoteRemind.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    //On créé l'Intent qui va nous permettre d'afficher l'autre Activity
                    //Mettez le nom de l'Activity dans la quelle vous êtes actuellement pour le premier parametre
                    v.setPressed(true);
    
                    Intent intent = new Intent(MenuPrincipal.this, NoteReminder.class);
                    //Intent intent = new Intent(MenuPrincipal.this, Teste2.class);
                    //On démarre l'autre Activity
                    startActivity(intent);
    
    
                }
            }); ....
    

    The button displays well but when I press it it doesn t show the pressed image :s I don’t understand what I am doing wrong !

    Does anyone see an error somewhere ???


    Where should I put those lines ? I ve put them in my button xml

        <Button
            android:id="@+id/bNoteRemind"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:background="@drawable/imagebutton1"
            android:focusable="true"
            android:focusableInTouchMode="true" />
    

    But now my button background changed to the pressed image without me pressing it :p and it doesn’t change

    Related posts:

    Add item to ListView without reloading
    Uncaught TypeError: Object #&lt;Object&gt; has no method &#039;exec&#039; at file://...
    How to use IntentCompat.makeRestartActivityTask()?
    How to align TextView around an ImageView?
    Obtain the Linux UID of an Android App
    How to detect touch events within a certain Renderer-area in android?
  • Android - Creating a folder in the data/data/pkg/files directory
  • Best way of implementing a scrolling navigation drawer
  • Add Context Menu Icon in android
  • Applying setRotation to Maps API v2
  • Android sharing Files, by sending them via email or other apps
  • Android Wear app not installing through handset
  • One Solution collect form web for “Pressed android button state”

    Is the Button the only thing you have displayed in your Activity? If so, then it will be focused (triggering the third item in your selector) when the window loads, and you won’t be able to navigate away from it. If you want to change only when pressed, delete that third line. While you’re at it, delete the first line, as the button will never be pressed when the window isn’t focused.

    In fact, I suggest this code:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/boutonnpousse" android:state_pressed="true"/>
        <item android:drawable="@drawable/boutonn"/>
    </selector>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.