0

I am attempting to create an android app, I am a relative novice. I am tryin to use multiple image buttons, however, I cannot get it to work, here is the code that I am using.

Public class MapScreen extends Activity
{

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


  ImageButton ib1,ib2,ib3,ib4,ib5;

   ib1= (ImageButton) findViewById(R.id.go_to_lagan_screen);
   ib2= (ImageButton) findViewById(R.id.go_to_city);
   ib3= (ImageButton) findViewById(R.id.go_to_university);
   ib4= (ImageButton) findViewById(R.id.go_to_icon_screen);
   ib5= (ImageButton) findViewById(R.id.map_to_home_screen);


   ib1.setOnClickListener(new View.OnClickListener()
   {
      @Override
      public void onClick(View v)
      {
         Intent intent = new Intent (v.getContext(), LaganArea.class);
         startActivityForResult(intent,0);
         //To change body of implemented methods use File | Settings | File Templates.
      }
   } );

   ib2.setOnClickListener((new View.OnClickListener()
   {
      @Override
      public void onClick(View v)
      {
         Intent intent1= new Intent (v.getContext(), CityCentre.class);
         startActivityForResult(intent1,0);
         //To change body of implemented methods use File | Settings | File Templates.
      }
   }));
   ib3.setOnClickListener((new View.OnClickListener()
   {
      @Override
      public void onClick(View v)
      {
         Intent intent2= new Intent (v.getContext(), UniversityArea.class);
         startActivityForResult(intent2,0);
         //To change body of implemented methods use File | Settings | File Templates.
      }
   }));
   ib4.setOnClickListener((new View.OnClickListener()
   {
      @Override
      public void onClick(View v)
      {
         Intent intent3= new Intent (v.getContext(), TheIcons.class);
         startActivityForResult(intent3,0);

         //To change body of implemented methods use File | Settings | File Templates.
      }
   }));
   ib5.setOnClickListener((new View.OnClickListener()
   {
      @Override
      public void onClick(View v)
      {
         Intent intent4= new Intent (v.getContext(), MyActivity.class);
         startActivityForResult(intent4,0);
         //To change body of implemented methods use File | Settings | File Templates.
      }
   }));
}

}

I don't know if I've missed something silly, or have I set about it the completely wrong way. But an extra pair of eyes over it would be greatly appreciated.

Scolytus
  • 16,338
  • 6
  • 46
  • 69
user2980885
  • 113
  • 2
  • 7

2 Answers2

1

Try this. Don't forget to add these activities on manifest. You can use switch/case for a clean and small code.

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


          ImageButton ib1,ib2,ib3,ib4,ib5;

           ib1= (ImageButton) findViewById(R.id.go_to_lagan_screen);
           ib2= (ImageButton) findViewById(R.id.go_to_city);
           ib3= (ImageButton) findViewById(R.id.go_to_university);
           ib4= (ImageButton) findViewById(R.id.go_to_icon_screen);
           ib5= (ImageButton) findViewById(R.id.map_to_home_screen);


           ib1.setOnClickListener(new View.OnClickListener()
           {
              @Override
              public void onClick(View v)
              {
                 Intent intent = new Intent (MapScreen.this, LaganArea.class);
                 startActivity(intent);

              }
           } );

           ib2.setOnClickListener((new View.OnClickListener()
           {
              @Override
              public void onClick(View v)
              {
                 Intent intent1= new Intent (MapScreen.this, CityCentre.class);
                 startActivity(intent1);
                 //To change body of implemented methods use File | Settings | File Templates.
              }
           }));
           ib3.setOnClickListener((new View.OnClickListener()
           {
              @Override
              public void onClick(View v)
              {
                 Intent intent2= new Intent (MapScreen.this, UniversityArea.class);
                 startActivity(intent2);
                 //To change body of implemented methods use File | Settings | File Templates.
              }
           }));
           ib4.setOnClickListener((new View.OnClickListener()
           {
              @Override
              public void onClick(View v)
              {
                 Intent intent3= new Intent (MapScreen.this, TheIcons.class);
                 startActivity(intent3);

                 //To change body of implemented methods use File | Settings | File Templates.
              }
           }));
           ib5.setOnClickListener((new View.OnClickListener()
           {
              @Override
              public void onClick(View v)
              {
                 Intent intent4= new Intent (MapScreen.this, MyActivity.class);
                 startActivity(intent4);
                 //To change body of implemented methods use File | Settings | File Templates.
              }
           }));
        }
Midhun Sivaraj
  • 493
  • 5
  • 13
0

First declare the activity in Manifest.

Try for each activity something like this :

 ib1.setOnClickListener(new View.OnClickListener()
   {
      @Override
      public void onClick(View v)
      {
         startActivity(new Intent (MapScreen.this , LaganArea.class));

      }
   } );
  • hi i have added the java classes to android manifest but nothin else, am i missing something regards the use of image buttons? thanks! – user2980885 Nov 30 '13 at 12:20
  • If you want to make a image buttons here is a example : http://www.mkyong.com/android/android-imagebutton-example/ –  Dec 01 '13 at 08:25
  • If that's all you can just checkmark to my answer if i helped you , thank you ! –  Dec 01 '13 at 08:27