0

I've two groups of Buttons,each has three buttons and three Activities such as Activity A,Activity B and Activity C in my android Application.

Group A

Button 1

Button 2

Button 3

Group B

Button 5 

Button 6

Button 7

If I click Button 1 and then Button 5 ,Activity A must be opened

If I click Button 2 and then Button 6 ,Activity B must be opened

If I click Button 3 and then Button 7 ,Activity C must be opened .

How to achieve this ?

Rakesh L
  • 3
  • 2
  • 2
    what u tried? Can u post some code – User Mar 25 '13 at 10:17
  • hi Rakesh. Has my answer worked for you? If it has, I would really appreciate if you would accept the answer – Egis Mar 28 '13 at 08:22
  • sorry! This is not what I'm looking for ... I need to go to an Intent after i clicked two buttons one-by-one :( for an example If I click only Button 1,the action must not be done.If i click button 1 and after that if I click button 5 .Then only the action must be done ... – Rakesh L Mar 28 '13 at 10:27

1 Answers1

1

here is an example:

<LinearLayout 
        android:id="@+id/group_A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button 
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startActivityA"/>
        <Button 
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startActivityB"/>
        <Button 
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startActivityC"/>
    </LinearLayout>

    <LinearLayout 
        android:id="@+id/group_B"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button 
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startActivityA"/>
        <Button 
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startActivityB"/>
        <Button 
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startActivityC"/>
    </LinearLayout>

in the Activity, which you want to start your Activities A, B & C in, you need to define your methods, e.g.

public void startActivityA(View view) {
    Intent i = new Intent(this, ActivityA.class);
    startActivity(i);       
}
Egis
  • 879
  • 2
  • 9
  • 13
  • I recommend to not use the onClick attribute. Mixing code and layout is always a bad idea... but beside that, the answer is correct – WarrenFaith Mar 25 '13 at 17:12
  • Hi Warren, I am curious why it would be a bad idea. In this post onClick used in xml was was voted as the best example of best practices in onClick implementation: http://stackoverflow.com/questions/6372104/best-practice-for-defining-button-events-in-android – Egis Mar 25 '13 at 17:18
  • Just because there are the most votes on this approach doesn't mean that it is the best. As I already said: mixing layout and logic is a bad idea in my opinion. – WarrenFaith Mar 25 '13 at 18:06
  • not trying to defend this approach. I am genuinely want to know what problems may arise with it since I use it a lot in the app I am building now. – Egis Mar 25 '13 at 18:08
  • Possible problems: You want to rename the method (have to change it on at least two places), someone else needs to change the layout and might be too lazy: Removes the button in the visual designer and creates a new one on a new place. Code analyzing tools like sonar might consider your method never called anywhere. Beside the method you can't see (in your Fragment/Activity) if there is somewhere a click-listener attached. Your onClick method must be in the activity, so your activity is even closer bound to the layout (while a fragment should handle the layout...) The last one is the worst. – WarrenFaith Mar 25 '13 at 18:27
  • To make it short: You might save the typing of the OnClickListener but the disadvantages shouldn't be ignored. You blow up/mess up the code. Just to take the last example to the next step: You have Activity A with Fragment B, and now you want Fragment B to be attached to Activity C, you would need to implement the method again while a dynamically set listener would just be attached to the button in the fragment itself -> way easier to maintain. – WarrenFaith Mar 25 '13 at 18:30
  • Just to make sure: There is no golden way and depending on the project size the onClick attribute is suitable for smaller ones, but for bigger projects with more developers, I recommend to drop it and use the setter for the listener. – WarrenFaith Mar 25 '13 at 18:40
  • In the question, he say Activity A opens `If I click Button 1 and then Button 5` but your answer seems to open Activity A `if you click either Button 1 or Button 2` – kbluue Jun 06 '17 at 10:22