Tony D
Tony D

Reputation: 1531

android image.setImageResource(..) causes text on button to dissappear

I have a button and an image object defined in a TableLayout in my xml file as:

    <Button
                    android:id="@+id/buttonNext"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.99"
                    android:gravity="center"
                    android:padding="1sp"
                    android:text="@string/buttonNext"/>
    <ImageView
                    android:id="@+id/ImageView02"
                    android:layout_width="100dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="0.33" 
                    android:src="@drawable/sixthstring"
                    android:contentDescription="@string/chart_description"/>

There is an OnClickListener hooked to the next button and it all worked fine.

I created an OnClickListener for the imageView and put in the following code:

  imgChart.setOnClickListener(onChartClick);

. . .

  private OnClickListener onChartClick = new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            imgChart.setImageResource(R.drawable.newPngImage);
        }
     };

The app works well, even when I click the image and retrieve the new image. However, if the phone sleeps and then returns or if I click the menu button (both causing a redraw?) the text of the next button disappears (the image in the imageView is fine and the button itself is still there) - weird.

If I comment out the setImageResource method, it all works fine again.

Upvotes: 0

Views: 2163

Answers (2)

Cloud Chen
Cloud Chen

Reputation: 535

Have exactly the same problem with you Tony. I use "setImageDrawable" instead and everything is still on their position, works perfect!

Upvotes: 3

Aqif Hamid
Aqif Hamid

Reputation: 3521

Use setBackgroundResource(id) instead of setImageResource(id)

Upvotes: 1

Related Questions