2

I already know how to start a one new activity when you click a button, but I have three buttons on the one layout. And I want each of the three buttons on that ONE activity to link to three other activities.

I have on the activity I have called 'main', Button 1 which is called services and I want to link it to the services activity. Button 2 which is called Search and I want it to go to the Search activity and thirdly 'map' which I want to link to the map activity.

Can someone help me do this please? Thanks

EDIT:Also, I'm a beginner with Android coding, could you explain in a little bit more detail please?

cnfw
  • 770
  • 2
  • 11
  • 28
  • possible duplicate of [How do I start another activity when a button defined in main.xml is clicked](http://stackoverflow.com/questions/10007070/how-do-i-start-another-activity-when-a-button-defined-in-main-xml-is-clicked) – Esailija Jun 15 '12 at 08:25

4 Answers4

3

So where is problem? Just set that your class will implements View.OnClickListener and override onClick() method a you got it.

public class MainActivity extends Activity implements View.OnClickListener {
   // body
}

@Override
public void onClick(View view) {
   switch (view.getId()) {

      case R.id.serviceBtn:
         Intent serviceIntent = new Intent(this, ServiceActivity.class);
         startActivity(serviceIntent);
         break;
      case R.id.searchBtn:
         Intent searchIntent = new Intent(this, SearchActivity.class);
         startActivity(searchIntent);
         break;
      case R.id.mapBtn:
         Intent mapIntent = new Intent(this, MapActivity.class);
         startActivity(mapIntent);
         break;
   } 
}
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • -1 for overuse of copy&paste, not understanding parametrization. – Seva Alekseyev Jun 14 '12 at 18:21
  • what?? are u kiding me? i wrote this fragment now. – Simon Dorociak Jun 14 '12 at 18:22
  • Oh it does. Why 3 calls to intent constructor and startActivity()? They only differ in one variable. – Seva Alekseyev Jun 14 '12 at 18:22
  • and? i can also do with anonymous listeners of each Button i don't know where problem is, sorry. – Simon Dorociak Jun 14 '12 at 18:26
  • @ Seva "Why 3 calls to intent constructo" can you can this call in one ? – Dheeresh Singh Jun 14 '12 at 18:27
  • @hawaii.five-0: copy&paste from yourself still counts. Your code is extraneous. – Seva Alekseyev Jun 14 '12 at 18:28
  • @ Seva please tell first , can you can this call in one ? – Dheeresh Singh Jun 14 '12 at 18:30
  • copy and paste? i wrote in without copy and paste man, so i don't know here is problem...really not. – Simon Dorociak Jun 14 '12 at 18:30
  • See my answer to see what I mean. Code duplication is bad for you. – Seva Alekseyev Jun 14 '12 at 18:32
  • it only for style how it is written?? and for this i have -1? :-D okey...oh man he can always click only on one button so always will be called one case of switch. i could -1 for you but for what? for what that is differently wriitten than mine? No... – Simon Dorociak Jun 14 '12 at 18:33
  • -1 is my way of saying I disapprove of this style. Now imagine for a second you need to specify extra intent parameters, like, 10 of them... Would you spell those out three times as well? – Seva Alekseyev Jun 14 '12 at 18:37
  • 1
    +1 For a perfectly reasonable answer. If each Intent were to require the same N-extras bundled in, I'm sure that would be in the question. Anything else is purely redundant. – Knossos Jun 14 '12 at 18:45
  • Thanks! I get an Eclipse error saying that the local variable 'i' is duplicated. Am I doing something wrong here – cnfw Jun 14 '12 at 18:56
  • just rename intents for example serviceIntent, searchIntent, mapIntent. – Simon Dorociak Jun 14 '12 at 18:57
  • @hawaii.five-0 : There are some issues with your code. Firstly you've shown the `onClick(...)` method outside of the `Activity` 'body' (as commented by `//body` in your code). Secondly the name of your example `Activity` is `MainActivity` but you're providing `YourActivity.this` as the `Context` parameter for the `Intent`. Less major points...it's not necessary to use `NameOfActivity.this` when the `onClick(...)` method is implemented by an `Activity` - just using `this` will suffice and, personally, I'd probably just use `startActivity(new Intent(this, SomeActivity.class));` – Squonk Jun 14 '12 at 19:47
  • @Squonk yeah, you right, i edited my post three times and i din't fix everything. – Simon Dorociak Jun 14 '12 at 19:49
1

Tie an onclick listener to all three buttons. In the listener, retrieve the ID of the button. Make a variable of type Class. Depending on the value of the button ID, initialize it to the class of the activity to invoke. Then construct an Intent for that class, and call startActivity().

EDIT for hawaii.five-0: here's how I'd do it:

@Override
public void onClick(View view) {
   Class c = null;
   switch (view.getId()) {
      case R.id.serviceBtn:
         c = ServiceActivity.class;
         break;
      case R.id.searchBtn:
         c = SearchActivity.class;
         break;
      case R.id.mapBtn:
         c = MapActivity.class;
         break;
   } 
   Intent i = new Intent(YourActivity.this, c);
   startActivity(i);
}

EDIT2:

class CurrentActivity extends Activity
    implements OnClickListener  
{
    void onCreate(Bundle b)
    {
        //Other initialization goes here...

        ((Button)findViewById(R.id.MyButton1)).setOnClickListener(this);
        ((Button)findViewById(R.id.MyButton2)).setOnClickListener(this);
        ((Button)findViewById(R.id.MyButton3)).setOnClickListener(this);
    }

}
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • If a click comes from an unknown ID, then yes. But why would it? – Seva Alekseyev Jun 14 '12 at 18:35
  • 1
    for what that meant Singh your approach is bad. – Simon Dorociak Jun 14 '12 at 18:41
  • Yeah, but a listener has to be explicitly tied to them in order to receive clicks. While doing that, might as well add handling of those IDs. – Seva Alekseyev Jun 14 '12 at 18:43
  • 1
    Good answer. Simply check whether c is null before firing off the Intent. – Knossos Jun 14 '12 at 18:46
  • How do you 'tie an onClick() listener to the buttons? Is this done in the XML file or what? – cnfw Jun 14 '12 at 19:16
  • Something like this: ((Button)findViewById(R.id.MyButtonID)).setOnClickListener(new MyListener()); Where MyListener is defined elsewhere like this: class MyListener implements View.OnClickListener { @Override public void onClick(View v) { /* Do something */}} – Seva Alekseyev Jun 14 '12 at 19:17
  • ((Button)findViewById(R.id.MyButtonID)).setOnClickListener(new MyListener()); See the My listener at the end? is that the name of the current class the code is in? – cnfw Jun 14 '12 at 19:44
  • That's one of the options. Or you can make a dedicated listener class inside the activity class; your choice. You can also make an anonymous class that implements OnClickListener. As long as the argument to setOnClickListener() implements the interface OnClickListener. – Seva Alekseyev Jun 14 '12 at 19:46
  • Sorry, I don't understand what you mean. When I put the current class into it, eclipse suggests to correct it to this: ((Button)findViewById(R.id.goservices)).setOnClickListener((OnClickListener) new CurrentActivity()); Will this work? – cnfw Jun 14 '12 at 19:48
0

Its very easy dear. Just put this code on button click event

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
Hardik
  • 1,429
  • 2
  • 19
  • 37
  • Thanks, I used this method and when I went to debug the app. It said, Source not found, in red writing. And below it, a button saying edit source lookup path... – cnfw Jun 14 '12 at 18:43
  • "Source not found, in red writing." with which line (of your code)? – Dheeresh Singh Jun 14 '12 at 18:45
  • When I add this to the onClick event method, it crashes the app when I click on the button. Do I have to add something to the NextActivity.class? – cnfw Jun 19 '12 at 16:22
0

which is very easy to start a new activity when the button is clicked. I have given the example for this.

// R.id.btnAdd -> which is present in your layout page
Button btnStart = (Button) findViewById(R.id.btnAdd); // declare button
// declare listener evernt for button
OnClickListener listener = new OnClickListener() {

   @Override
   public void onClick(View v) {
        // declare the Intent for moving another activity
        Intent view = new Intent(YourCurrentClassName.this,
        anotherClassName.class);
        // startActivity is used to navigating the view
        startActivity(view);
    }
};
// set the listener evernt to button
btnStart.setOnClickListener(listener);
Karthik
  • 747
  • 4
  • 21
  • 48