Search code examples
androidandroid-layoutandroid-imageview

Why does ImageView setImageResource() sets image size to match_parent?


Here is the XML layout.

<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:text="@string/title"
        android:textSize="@dimen/title_textSize"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/main_text_color"/>

<ImageView
        android:id="@+id/play_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_centerHorizontal="true"
        android:onClick="clickPlay"
        android:src="@drawable/pause"
        />

<SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginTop="15dp"
        android:background="@null"
        android:paddingBottom="8dp"
        android:paddingLeft="16dp"
        android:paddingRight="10dp"
        android:paddingTop="12dp"
        android:progressDrawable="@drawable/seek_progress"
        android:thumb="@drawable/seek_thumb"/>

<ImageView
        android:id="@+id/restart_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/play_img"
        android:layout_centerHorizontal="true"
        android:onClick="clickRestart"
        android:src="@drawable/restart"
        android:visibility="invisible"
        />

Here is a method clickPlay()

public void clickPlay(View v) {
    if (playerCurrent != null && playerCurrent.isPlaying()) {
        playerCurrent.pause();
        playerBackground.pause();
        imgPlayPause.setImageResource(R.drawable.play);
        mVolumeSeekBar.setVisibility(View.INVISIBLE);
        imgRestart.setVisibility(View.VISIBLE);
        mIsPlaying = false;
    } else {
        playerCurrent.start();
        playerBackground.start();
        imgPlayPause.setImageResource(R.drawable.pause);
        mVolumeSeekBar.setVisibility(View.VISIBLE);
        imgRestart.setVisibility(View.INVISIBLE);
        mIsPlaying = true;
    }
}

However, when I change image from Pause to Play, Play image get size of match_parent, but when I press again and image changes to Pause image, it gets a normal size of wrap_content.

Both Play and Pause images are identical!

Here is the image of how it looks before and after (made them colored so you better view the size)


Solution

  • It was NOT an issue with Android SDK. It was an issue either with Android Emulator or Android Studio.

    Namely, when I uninstalled the app from the emulator and did Build->Rebuild Project, the bug was gone. Obviously either of these two did not properly refresh images in /res folders.