0

I want the imageview with the Id = soundtoggle to switch to R.drawable.sound_on under the if condition and otherwise to R.drawable.sound_off. But i am not able to do it. Please help

    package com.twodwarfs.fallanimation;

    import com.tmp.animation.R;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;

    public class ToggleActivity extends Activity 
    { 
         protected void onCreate(Bundle savedInstanceState) 
         {
                super.onCreate(savedInstanceState);
        setContentView(R.layout.homescreen);
        final ImageView img1 = (ImageView) findViewById(R.id.soundtoggle);
        img1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v)
            {
                if(v.getId() == R.id.soundtoggle)
                {
                    img1.setBackground(getResources().getDrawable(R.drawable.sound_on));
                }
                else 
                {
                    img1.setBackground(getResources().getDrawable(R.drawable.sound_off));
                }
            }});
        }
     }

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="@drawable/home_screen">

<ImageView
    android:id="@+id/musictoggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/soundtoggle"
    android:layout_centerHorizontal="true"
    android:src="@drawable/music_off"
    android:layout_marginLeft="5dp"
    android:clickable="true" />
<ImageView
    android:id="@+id/soundtoggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/musictoggle"
    android:layout_below="@+id/playbutton"
    android:src="@drawable/sound_off" 
    android:layout_marginLeft="1dp" 
    />
<Button
    android:id="@+id/playbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/soundtoggle"
    android:layout_alignParentTop="true"
    android:layout_marginTop="178dp"
    android:background="@drawable/play"
    android:layout_marginLeft="1dp" />

my full xml which consists of 3 buttons play,sound and music

XylemRaj
  • 772
  • 4
  • 13
  • 28

3 Answers3

0

instead of setting background

img1.setBackground(getResources().getDrawable(R.drawable.sound_on));

you need to setImageResource

img1.setImageResource(R.drawable.sound_on);

Also your v.getId() will always return the same id, so you need another flag to track

img1.setOnClickListener(new OnClickListener()
{
    private boolean toggle=false;
    @Override
    public void onClick(View v)
    {
        if(toggle)
        {
             img1.setBackground(getResources().getDrawable(R.drawable.sound_on));
             toggle=false;
        }
        else 
        {
             img1.setBackground(getResources().getDrawable(R.drawable.sound_off));
             toggle=true;
        }
    }
}
Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
0

Your if-else is invalid. Make something like this:

private boolean buttonClick = true;

@Override
public void onClick(View v)
{
    if(v.getId() == R.id.soundtoggle)
    {
       if(buttonClick){
           img1.setBackground(getResources().getDrawable(R.drawable.sound_on));
       } else{
           img1.setBackground(getResources().getDrawable(R.drawable.sound_off));
       }
       buttonClick = !buttonClick;    
    }
}

Every time you click the image you see "sound_on" image. Because you set that on image click

Dragan
  • 328
  • 2
  • 12
0

Use this star3.setImageResource(R.drawable.unclickstar);

instead of img1.setBackground(getResources().getDrawable(R.drawable.sound_off));

AndyBoy
  • 564
  • 6
  • 29