0

Lets say my main screen has a lot of Buttons.

I want to make a class in which all the Button listeners are inside and call the onClick from my main method each time I press a button.

public class Buttons extends Activity implements OnClickListener {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

public void onClick(View v) {

   Button btn1 = (Button) findViewById(R.id.button1);
   Button btn2 = (Button) findViewById(R.id.button3);


    int viewId = v.getId() ;

    if(viewId == R.id.button1){
           Intent ScrollViewTest = new Intent(this, ScrollViewTest.class);
           startActivity(ScrollViewTest);}
      if(viewId == R.id.button2){
           Intent ScrollViewTest = new Intent(this, CameraTest.class);
           startActivity(CameraTest);}      
    }
    }
}

Is there a way to call the onClick method like:

Buttons allButtons = new Buttons();
allButtons.onClick(btn1);

or is my logic wrong ?

........................................

Already checked

calling a method from an onClick listener

How to call onClick(View v) method explicitly in an Android? Is it possible?

Android onClick method

Is there a method to set methods for multiple objects in one go?

Community
  • 1
  • 1

1 Answers1

1
public class Buttons extends Activity {

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   Button btn1 = (Button) findViewById(R.id.button1);
   Button btn2 = (Button) findViewById(R.id.button3);  // did you mean R.id.button2?

   // Create the onClickListener for btn1
   btn1.setOnClickListener(new View.OnClickListener() {

       public void onClick(View v) {
           Intent ScrollViewTest = new Intent(this, ScrollViewTest.class);
           startActivity(ScrollViewTest);
       }
   });

   // Create the onClickListener for btn2
   btn2.setOnClickListener(new View.OnClickListener() {

       public void onClick(View v) {
           Intent ScrollViewTest = new Intent(this, CameraTest.class);
           startActivity(CameraTest);
       }
   });
}
Philip Liberato
  • 331
  • 8
  • 22
  • Thanks, but I have to call the Button class from my Main Activity otherwise they don't respond. There is no setContentView(R.layout.main); in this class so no Buttons here. Just a method I want to call each time they are pressed. Hard to explain. Anyway I think Java doesn't work as I think. Have to study more. – please delete me Mar 27 '14 at 03:18
  • If you explain it in a little more detail, I may be able to help you. Why are you trying to do things with buttons in an activity that doesn't have buttons in the view? – Philip Liberato Mar 27 '14 at 03:27
  • Well You are right about that. What I should write is: public class Buttons extends mainScreen implements OnClickListener{... So I have the Button class that contains all my Buttons. Now I want to call it from the "public class mainScreen extends Activity" Hope I got a little bit more clear. – please delete me Mar 27 '14 at 18:12