1

I am trying to change the background image of an ImageView for different options on Radio buttons in the calling activity. The calling activity passes the parameters in a bundle to rendering activity and I am receiving the parameters properly (I get Curve1 and Curve2 as parameters in the rendering activity after making choice on radio buttons). But I am not able to change the image background either by setImageResource() or setBackgroundResource(). Here is the snippet doing that:

Rendering Activity

private void validateImage()
    {       wave = (ImageView) findViewById(R.id.image_wave);
            switch (extras.getString("Curve"))
            {
                    case "Curve1":
                            wave.setB (R.drawable.sinewave1);
                            break;
                    case "Curve2":
                            wave.setImageResource(R.drawable.sinewave2);
                            break;
            }
     }

activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"      
android:layout_width="match_parent"
android:layout_height="match_parent"     
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"           
tools:context="shantanu.concussion.usu.concussiontest.TestActivity">


<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/image_wave"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

Can anybody suggest what am I missing? Thanks :-)

8Bit
  • 63
  • 1
  • 9
  • Refer to http://stackoverflow.com/questions/11893826/how-to-refresh-image-view-immediately which is useful for your problem – JZH Sep 16 '15 at 02:08
  • I tried using invalidate() after changing the image background, didnt help and I am doing this on the UI thread so, invalidate should be the choice for a redraw request. – 8Bit Sep 17 '15 at 06:08

2 Answers2

0

Try to call invalidate manually. Violently change least something. You need to understand where the problem is (wrongly chosen View, View does not change) Try to change another property for example.

May cause in the resource. (e.g., it is transparent)

Check the entrance to the branch switch. (perhaps wrongly runs comparison)

Change the properties through the editor. Maybe something prevents display background.

It's just a stupid mistake.

user2413972
  • 1,355
  • 2
  • 9
  • 25
  • I did a little bit of manual debugging like that but it has not helped. Its definitely entering into switch cases and both the images are rendered fine manually. But its not setting the image on the execution of setImageResources() or setBackgroundResources (). So may be I am not making the method calls properly. – 8Bit Sep 17 '15 at 05:23
  • Method calls are invoked exactly? Have you tried just call it? – user2413972 Sep 17 '15 at 05:34
  • Can you please explain a little bit? May be I am missing your point. – 8Bit Sep 17 '15 at 05:42
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); wave.setBackgroundResource(R.drawable.sinewave1); } – user2413972 Sep 17 '15 at 05:45
  • Tried, didnt help. The execution is going in the switch case for sure, I tried logging some information inside switch case, it worked just fine. – 8Bit Sep 17 '15 at 05:54
  • Magic does not happen. Do you've tried to add "wave.invalidate()" after that? – user2413972 Sep 17 '15 at 05:59
  • Seriously, there is a reason I have posted the issue here. All the suggestions I have received I have tried before complaining about them. I did try wave.invalidate() after the switch statement, didnt help. The frustrating part is I am doing everything as per the documentation for android and other questions asked in SO but they are not solving the issue. – 8Bit Sep 17 '15 at 06:07
  • I really do not know how to help. It's just a stupid mistake... Try to run on the other device. Sometimes it happens that resources are not prescribed. – user2413972 Sep 17 '15 at 06:11
  • I really appreciate the community trying to help, you are also suggesting your best. It can be a silly mistake, and its not striking me for now. And these are the only lines of code where I am doing anything on this imageview, thats why putting all the project code here wouldnt help much. – 8Bit Sep 17 '15 at 06:36
  • Make a backup and try remake. Try another device. – user2413972 Sep 17 '15 at 06:45
0

try

wave.setImageDrawable(getResources().getDrawable(R.drawable.sinewave2));
Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41
  • getDrawable is deprecated in API22 and its not refreshing the image. I tried some other methods related to changing background properties as well but they havent helped. Still looking for the solution. – 8Bit Sep 17 '15 at 05:27
  • Use this: ContextCompat.getDrawable(Context.Drawable ID) – nuhkoca Jul 06 '16 at 02:49