Implement OnClick in Image

1

I'm doing a project (a memory game) with this tutorial link I'm doing it a little different  Create a menu_activity that says the name of the game and an image with the Play icon. This brings us to the second activity, where is the problem I have 3 images which indicate the difficulty.

In the project I do not see any error, I run but when going to my LevelActivity and click on the difficulty, for example to go EasyActivity (and to show the memorama) the application does nothing. I do not know much about it but I think it's a problem when implementing OnClick. Try to create the OnClick as I did with the main menu:

public void goGame(View view){
    Intent intent = new Intent(this, LevelActivity.class);
    startActivity(intent);

}
}

But when you do this and click on the difficulty the application is closed.
I show you part of the code.

The LevelActivity

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class LevelActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_level);
}
}

The XML of the image that tells us the difficulty

<ImageView
            android:id="@+id/easylevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/facil"
            android:layout_gravity="center"
             />

I have my EasyActivity class

public class EasyActivity extends AppCompatActivity implements 
View.OnClickListener { 

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_easy); 

// I skipped the code that I think does not affect the problem

for (int r = 0; r < numRows; r++){
        for(int c = 0; c < numColumns; c++){
            MemoryButton tempButton = new MemoryButton(this, r, c, 
buttonGraphics[buttonGraphicLocations[r * numColumns + c]]);
            tempButton.setId(View.generateViewId());
            tempButton.setOnClickListener(this);
            buttons[r * numColumns + c] = tempButton;
             gridLayout.addView(tempButton);
        }
    }
@Override
public void onClick(View view) {....}
    
asked by Madelyn Reyes Osuna 23.05.2017 в 01:28
source

1 answer

0

If your Activity implements OnClickListener , you must implement onClick() and you can detect when the ImageView was activated by means of its id, in this way:

 @Override
 public void onClick(View view){
     switch (view.getId()){
        case R.id.easylevel: //id de ImageView.

        //realiza operación al dar clic al imageView.
         Intent intent = new Intent(this, LevelActivity.class);
         startActivity(intent);

         break;
        default:
         break;
     }
   }
    
answered by 23.05.2017 / 22:19
source