Wednesday, August 17, 2011

How To Replace Image with another in Button click in Android

This very simple programme and it is very good for Android beginners. Those who new to Android and want to learn android, for them this may useful.

Java Code

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

public class MytestdroidActivity extends Activity implements OnClickListener {


    Button b1;
    ImageView iw;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       b1 = (Button) findViewById(R.id.button1);
       b1.setOnClickListener(this); 

       iw = (ImageView) findViewById(R.id.icon);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == b1)
        {
            iw.setImageResource(R.drawable.camara);
        }

    }
}

XML Code









you should have 2 images in your drawable folder call icon and camera