B-Rad
B-Rad

Reputation: 1557

Why does Android Activity move button after changing background?

I have an Activity with the following image button:

<ImageButton android:id="@+id/nextPage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"       
    android:contentDescription="@string/nextPageString"
    android:alignParentRight="True"
    android:alignParentBottom="True"
    android:onClick="NextPage" />

My Button appears in the bottom right corner of the screen as I would expect.

When I click the button I change the background with the following code:

public void NextPage(View view){
    Resources res = this.getResources();
    Drawable d = res.getDrawable(R.drawable.nextBackgroundImage);
    view.setBackground(d);
}

After I click the button the first time it displays my new background exactly as I want, however, it moves the image button to the center of the screen. Not sure why. I would like it to stay in the bottom right corner.

Upvotes: 1

Views: 227

Answers (1)

Charlie Collins
Charlie Collins

Reputation: 8876

As several of the comments here have noted, the SIZE of your background image is likely why you're button is "moving." With layout_width and layout_height set to wrap, the size will change if the content of the view (including its background) changes.

+1 for Alex, try setting a specific size there just to TEST and see what happens (200dp width x 80dp height, for example).

Upvotes: 1

Related Questions