-1

I am currently making an android application that is Hangman and i need to update my ImageView with every time the user guesses a letter that is not in the phrase but it updates oddly, it doesn't delete the old photo and replace it, it just add's a new one to the screen and not in the same boundaries that the old photo was set to. Here's my code.

Java to replace the photo:

    ImageView hangmanPhoto = (ImageView)findViewById(R.id.hangmanPhoto);
    //This updates the loss counter
    pictureEnumerator++;
    //This set's the photo
    if(pictureEnumerator==1){
        hangmanPhoto.setImageResource(R.drawable.hangman_1);
    }
    else if(pictureEnumerator==2){
        hangmanPhoto.setImageResource(R.drawable.hangman_2);
    }
    else if(pictureEnumerator==3){
        hangmanPhoto.setImageResource(R.drawable.hangman_3);
    }
    else if(pictureEnumerator==4){
        hangmanPhoto.setImageResource(R.drawable.hangman_4);
    }
    else if(pictureEnumerator==5){
        hangmanPhoto.setImageResource(R.drawable.hangman_5);
    }
    else if(pictureEnumerator==6){
        hangmanPhoto.setImageResource(R.drawable.gameend);
    }

And this is how i have the base image once my app starts:

<ImageView> android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/hangmanPhoto" android:background="@drawable/hangman_0" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:paddingBottom="40dp" android:paddingTop="10dp" android:layout_weight="0.51" android:paddingLeft="20dp" android:padding="20dp" android:paddingRight="20dp" />

Here's the screen:

This is my screen when it happens.

ElectroMan
  • 156
  • 1
  • 13
  • replace your imageView xml with android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/hangmanPhoto" android:layout_gravity="center_vertical" android:paddingBottom="40dp" android:paddingTop="10dp" android:paddingLeft="20dp" android:paddingRight="20dp" /> – dex Dec 04 '15 at 17:58

1 Answers1

1

Use src instead background in your Xml file and in your Java too

android:background="@drawable/hangman_0"

To

android:src="@drawable/hangman_0"

see the difference here

Community
  • 1
  • 1
Context
  • 1,873
  • 1
  • 20
  • 24