B-Rad
B-Rad

Reputation: 1557

View.setBackground() setting background of ImageButton?

I have an ImageButton:

<ImageButton 
android:id="@+id/nextPageB"
android:layout_width="50dp"
android:layout_width="50dp"
android:layout_alignParentBottom="True"
android:contentDescription="@string/nextPage"
android:onClick="NextPage"
android:src="@drawable/ic_launcher"
/>

This is part of a seperate activity that is started from my main activity:

Intent intent = new Intent(this, BookRead.class);
startActivity(intent);

Once in the new activity I call the NextPage method upon ImageButton click:

public void NextPage(View view)
{
    view.setBackgroud(res.getDrawable(R.Drawable.MyNewImage));
}

'res' is defined in my new activity in the onCreate method:

res = this.getResources();

For some reason when I click my ImageButton it changes the background on the ImageButton itself instead of the activity layout.

Upvotes: 0

Views: 266

Answers (2)

Stefan de Bruijn
Stefan de Bruijn

Reputation: 6319

The onClick method receives the view which is actually clicked.

Upvotes: 2

Heinrisch
Heinrisch

Reputation: 5935

An imagebutton is also a view, and you are probably passing the imagebutton view to the NextPage method.

You could either get the correct view by referencing it from XML or do something like view.getParent().

Upvotes: 4

Related Questions