Pergunta

I am sorry if this have been posted before or may not but I am having issues with several imagebutton where it doesn't change the image when I press them. I used the same logic that I used with my tabs where I call upon x.xml (x is an example name) file that is stored in my drawable folder and call the folder from the corresponding imagebutton.

This is the code for change the imagebutton that I used (drawable/dbmdtndeck.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


<!-- When selected -->
<item android:drawable="@drawable/ydeck2" 
      android:state_selected="true"
      android:state_focused="true" 
      android:state_pressed="true" />


<!-- When not selected -->
<item android:drawable="@drawable/ydeck"  />


</selector>

here is the file that I use that is stored in the layout folder(layout/frmdbm.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dbmbackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<ScrollView
    android:id="@+id/dbmscrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/dbmllbackground1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

<LinearLayout
    android:id="@+id/dbmllbackground2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/imgbtnedit"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnedit"
        android:scaleType="fitXY" />

    <LinearLayout
        android:id="@+id/dbmllblankspace2"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>

    <ImageButton
        android:id="@+id/imgbtndeck"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtndeck" />

</LinearLayout>

<LinearLayout
    android:id="@+id/dbmllblankspacemid"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/dbmllbackground6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
        android:id="@+id/dbmllblankspace4"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>
    <ImageButton
        android:id="@+id/imgbtnsearch"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnsearch" />

    <LinearLayout
        android:id="@+id/dbmllblankspace5"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>
  </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/dbmllbackground3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/imgbtnwish"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnwish"
        android:scaleType="fitXY" />

    <LinearLayout
        android:id="@+id/dbmllblankspace"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>

    <ImageButton
        android:id="@+id/imgbtnrestrict"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnrestrict" />
    </LinearLayout>

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

this is my java class code (dbm.java):

package com.example.yao;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
/* FOR TOAST (POP MESSAGE),  BUTTON,  AND IMAGE BUTTON*/
import android.app.AlertDialog;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.ImageButton;
/*   FOR TAB RESOUCE */
import android.app.TabActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class dbm extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frmdbm);
      /* Calling TOAST MESSAGE
       * 
       * 
       */ 
       addListenerOnButton();

    }

   /* TOAST MESSAGE (POP MESSAGE)
    * 
    */
    public void addListenerOnButton() {

    /*
     * ASSINING OBJECTS TO BUTTON VERIABLES
     */

    ImageButton imageButtonedit = (ImageButton) findViewById(R.id.imgbtnedit);
    ImageButton imageButtondeck = (ImageButton) findViewById(R.id.imgbtndeck);
    ImageButton imageButtonwish = (ImageButton) findViewById(R.id.imgbtnwish);
    ImageButton imageButtonsearch = (ImageButton) findViewById(R.id.imgbtnsearch);
    ImageButton imageButtonrestrict = (ImageButton) findViewById(R.id.imgbtnrestrict);

    /*
     * WHEN CLICK ON THE OBJECT POP MESSAGE APPEARS
     */

    imageButtonedit.setOnClickListener(new OnClickListener()
    {   
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "DECK EDITOR", Toast.LENGTH_SHORT).show();
        }   
    });

    imageButtondeck.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "DECK RECEPICES", Toast.LENGTH_SHORT).show();
        }
    });

    imageButtonwish.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "WISH LIST", Toast.LENGTH_SHORT).show();
        }
    });

    imageButtonsearch.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "CARD SEARCHER", Toast.LENGTH_SHORT).show();        
        } 
    });

    imageButtonrestrict.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "FORBIDDEN / LIMITED LIST", Toast.LENGTH_SHORT).show();   
        }
    });

    }
}

Furthermore, I need to say that this layout(frmdbm.xml) is being called into a framelayout that I called tabcontent that is in my activity_main.xml. When a user uses the corresponding tab and that layout (frmdbm.xml) will appear in the framelayout. In additon, I dont get any errors but I notice that it doesnt change image it keeps the original image on it. I am new to this android developing and I am not sure what is going on because I did this to my tabs and it seems to be working great. Not sure what I am doing different in here.

Thank you

Foi útil?

Solução

Use this :

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/button_idle"
        android:state_focused="false"
        android:state_pressed="false"
        android:state_selected="false"/>
    <item android:drawable="@drawable/button_pressed"
        android:state_focused="false"
        android:state_pressed="true"
        android:state_selected="false"/>

</selector>

Outras dicas

Try this as your selector ...

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_selected="true" android:drawable="@drawable/learn" />
    <item android:drawable="@drawable/learng" />
</selector>

try it..

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_selected="true" android:drawable="@drawable/buttonpress"/>
    <item android:state_focused="true" android:drawable="@drawable/buttonpress"/>
    <item android:state_pressed="true" android:drawable="@drawable/buttonpress"/>
    <item android:drawable="@drawable/button"/>

</selector>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top