0

I'm making an app and when it launches it starts Mainactivity.java Mainactivity.java opens a layout with 9 Imagebuttons. How can I implement in my code in Mainactivity.java that once one is clicked it opens another activity (like telefoonnummers.java)? Sorry for my bad English but I'm dutch and a non-native speaker. I have this code in Mainactivity.java:

package com.example.rome;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

Very clean as you see, but how can I add the implementation, would you guys please help??? My Imagebuttons are all just called imagebutton1, imagebuttton2 etc. btw.

Ide
  • 65
  • 7

1 Answers1

0

After

setContentView(R.layout.activity_main);

add for each ImageButton:

findViewById(R.id.imagebutton1).setOnClickListener(this);

Make class implement OnClickListener

class MainActivity extends Activity implements View.OnClickListener {

and add this method:

@Override
public void onClick(View v){
  switch(v.getId()){
    case R.id.R.id.imagebutton1:
      startActivity(new Intent(telefoonnummers.class));
      break;
    case R.id.R.id.imagebutton2:
      startActivity(new Intent(telefoonnummers.class));
      break;
    //-- more cases --
  }
}
S.D.
  • 29,290
  • 3
  • 79
  • 130
  • I get an error telling: the method setOnClickListener (View.OnClickListener) in the type View is not applicable for the arguments (Mainactivity) And a second one: The nested type MainActivity cannot hide an enclosing type. How can I fix those? – Ide Mar 03 '13 at 15:58