I've got one activity here is what I am trying do. When app starts an image is displayed. When the image is clicked the next image is displayed. I've got it all setup with main.xml and imageview. The app builds and runs and displays the first image no problem. Now trying to setup onclick for the image so the next image will be displayed. I've added android:onClick="onClick" to the image vew in main.xml. I am using (this) for setOnClickListner and I implemented View.onClickListener for the class but the switch case I setup I get duplicate method for onClick(View v) and I'm not sure why.
Also trying to figure out the findViewById I think the way I have it image1 will always be displayed.
getting error on line public void onClick(View v) that it is a duplicate method.
Here is the code I have. Thanks for any assistance on this.
main.xml is a linear layout not sure that matters.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imageView1"
android:src="@drawable/bear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="onClick"
android:contentDescription="@string/desc"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/bear_n"
android:onClick="onClick"
android:contentDescription="@string/desc" />
This is the .java file.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class VTFCActivity extends Activity implements View.OnClickListener{
ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.imageView1);
ImageView image1 = (ImageView) findViewById(R.id.imageView1);
ImageView image2 = (ImageView) findViewById(R.id.imageView2);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.imageView1:
image.setImageResource(R.drawable.bear);
break;
case R.id.imageView2:
image.setImageResource(R.drawable.bear_n);
break;
}
}
See Question&Answers more detail:os