0

I have 2 imagebuttons which refer to the same openTheBag method onClick. But weird thing which I can't get my head around right now is that when I click @id/purse imageButton activates openBug, but @id/red_paint doesn't which even can be seen by debugger. What on Earth is happening here, please help. Here is piece of the xml.

    <FrameLayout android:id="@+id/frame" android:layout_weight="1.0"
            android:layout_width="match_parent" android:layout_height="0dip"
            android:background="@drawable/shelf_wall">
            <ImageButton android:id="@+id/purse" android:layout_width="67dp"
                android:layout_height="82dp" android:layout_gravity="right"
                android:contentDescription="@string/bag" android:onClick="openTheBag"
                android:scaleType="fitCenter" android:src="@drawable/purse"
                android:background="@null" />

            <ImageButton android:id="@+id/red_paint"
                android:layout_width="75dp" android:layout_height="112dp"
                android:layout_gravity="right" android:layout_marginRight="10dp"
                android:layout_marginTop="170dp" android:contentDescription="@string/red_paint"
                android:onClick="openTheBag" android:src="@drawable/kyzyl_paint"
                android:scaleType="fitCenter" android:adjustViewBounds="true"
                android:background="@null" />
</FrameLayout>

Here is the method openTheBag():

public void openTheBag(View view) 
{
     RelativeLayout storage = (RelativeLayout)this.findViewById(R.id.storage);
        storage.setVisibility(View.VISIBLE);
}

If the @id/storage is relevant:

<RelativeLayout android:id="@+id/storage"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:orientation="horizontal" android:visibility="gone" >

        <ImageButton android:id="@+id/ImageButton07"
            android:layout_width="55dp" android:layout_height="55dp"
            android:layout_alignLeft="@+id/square1"             android:layout_alignTop="@+id/ImageButton03"
            android:adjustViewBounds="true" android:alpha=".4"
            android:src="@drawable/transparent_background" />

        <ImageButton android:id="@+id/ImageButton04"
            android:layout_width="55dp" android:layout_height="55dp"
            android:layout_alignLeft="@+id/ImageButton01"
            android:layout_alignTop="@+id/ImageButton07"
            android:adjustViewBounds="true" android:alpha=".4"
            android:src="@drawable/transparent_background" />

        <ImageButton android:id="@+id/square1"
            android:layout_width="55dp" android:layout_height="55dp"
            android:layout_above="@+id/ImageButton03" android:layout_marginLeft="11dp"
            android:layout_toRightOf="@+id/ImageButton02"
            android:adjustViewBounds="true" android:alpha=".4"
            android:src="@drawable/transparent_background" />

        <ImageButton android:id="@+id/ImageButton01"
            android:layout_width="55dp" android:layout_height="55dp"
            android:layout_alignTop="@+id/square1" android:layout_marginLeft="10dp"
            android:src="@drawable/transparent_background"
            android:layout_toRightOf="@+id/square1" android:adjustViewBounds="true"
            android:onClick="redSplashClicked" />

        <ImageButton android:id="@+id/ImageButton05"
            android:layout_width="55dp" android:layout_height="55dp"
            android:layout_above="@+id/ImageButton03" android:layout_marginLeft="11dp"
            android:layout_toRightOf="@+id/ImageButton01"
            android:adjustViewBounds="true" android:alpha=".4"
            android:src="@drawable/transparent_background" />

        <ImageButton android:id="@+id/ImageButton03"
            android:layout_width="55dp" android:layout_height="55dp"
            android:layout_below="@+id/ImageButton02" android:layout_marginTop="13dp"
            android:layout_toLeftOf="@+id/square1" android:adjustViewBounds="true"
            android:alpha=".4" android:src="@drawable/transparent_background" />

        <ImageButton android:id="@+id/ImageButton09"
            android:layout_width="55dp" android:layout_height="55dp"
            android:layout_alignLeft="@+id/ImageButton05"
            android:layout_alignTop="@+id/ImageButton04"
            android:adjustViewBounds="true" android:alpha=".4"
            android:src="@drawable/transparent_background" />

        <ImageButton android:id="@+id/ImageButton08"
            android:layout_width="55dp" android:layout_height="55dp"
            android:layout_alignTop="@+id/ImageButton05"
            android:layout_centerHorizontal="true" android:adjustViewBounds="true"
            android:alpha=".4" android:src="@drawable/transparent_background" />

        <ImageButton android:id="@+id/ImageButton06"
            android:layout_width="55dp" android:layout_height="55dp"
            android:layout_alignLeft="@+id/ImageButton08"
            android:layout_alignTop="@+id/ImageButton09"
            android:adjustViewBounds="true" android:alpha=".4"
            android:src="@drawable/transparent_background" />

        <ImageButton android:id="@+id/ImageButton02"
            android:layout_width="55dp" android:layout_height="55dp"
            android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
            android:layout_marginLeft="11dp" android:layout_marginTop="10dp"
            android:adjustViewBounds="true" android:alpha=".4"
            android:src="@drawable/transparent_background" />

    </RelativeLayout>
Nazerke
  • 2,098
  • 7
  • 37
  • 57

2 Answers2

0

Why don't you try calling the event programmatically inside your code using onClick() method. Something like:

ImageButton mBtnPurse = (ImageButton) findViewById(R.id.purse);
ImageButton mBtnRedPaint = (ImageButton) findViewById(R.id.red_paint);

mBtnPurse.setOnClickListener(this);
mBtnRedPaint.setOnClickListener(this);

Let your activity implement onClickListener, then add unimplemented methods, which is onClick method:

@Override
public void onClick(View v)
{
    if(v == mBtnPurse || v == mBtnRedPaint)
    {
        openTheBag(v);
    }
}

and remove android:onClick="openTheBag" from both the buttons in xml.

  • interestingly enough, I get the same result as my original code. So the problem must be in the layout I guess. idk – Nazerke Oct 06 '13 at 16:57
  • Why do you need to pass the view for openTheBag() method? Is there any specific reason? –  Oct 08 '13 at 04:55
  • so that I can say which view the method called from: @id/purse button or @id/red_paint button, and take appropriate actions – Nazerke Oct 08 '13 at 06:12
  • You dont need to pass it until you are using it for any later use or inside the method that you are calling. The onClick method will manage it. When you put condition `v == mBtnPurse` or `v == mBtnRedPaint`, it internally knows which view is receiving the click. –  Oct 10 '13 at 04:15
0

Could it be a typo of your method names

openTheBag()

and

openBag() 

?

Shaz
  • 55
  • 2
  • 11