Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, June 9, 2016

Button array onclicklistener

Button array onclicklistener


I have set up 10 buttons in xml and added to main activity. I show only 2. All button just change a variable and button colors. Is there a method to group all the button to a single onclicklistener and use switch to check which button was pressed. using separate onclicklistener seems like allot of waisted space.

@Override  protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);        f1 =(Button) findViewById(R.id.f1);      f2 =(Button) findViewById(R.id.f2);        f1.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {              clearbutton();              f1.setBackgroundColor(Color.RED);              intbtnSelect=0;           }      });        f2.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {              clearbutton();              f2.setBackgroundColor(Color.RED);              intbtnSelect=1;          }      });  

Answer by Blackbelt for Button array onclicklistener


You can implement OnClickListener into your Activity. For instance

public class MyActivity extends Activity implements View.OnClickListener {              @Override       protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            f1 =(Button) findViewById(R.id.f1);          f1.setOnClickListener(this);            f2 =(Button) findViewById(R.id.f2);          f2.setOnClickListener(this);       }  }  

Them you have to implement the onClick method and switch upon view.getId():

@Override  public void onClick(View v) {      switch (v.getId()) {      case R.id.f1:          // your code here          break;      case R.id.f2:          // your code here           break;      }  }  

Answer by Waza_Be for Button array onclicklistener


in your xml, on all your buttons:

android:onclick="myClick"  

in your Activity:

public void onClick (View v){     Button clickedButton = (Button) v;     //Do wathever you want with your Button     clickedButton.setBackgroundColor(Color.RED);     // and you could also switch(v.getId()) if really needed.  }  

You could also implements View.OnClickListener as suggested by blackbelt, it just depend on your preferences.

I personally love to keep the cleaner java code possible, but that's up to you!

There is nothing good/wrong with these 2 methods, I think.

Answer by midhunhk for Button array onclicklistener


Let your Activity implement View.OnClickListener and then you have to add a method onClick(View view).

While creating the buttons, you just add this line

button.setOnClickListener(this), so that whenever a button is clicked, it will call the onClick() method in your Activity.

Once inside the onClick() method, you can use the view.getId() to see what button was clicked.

Code :

// onCreate() - Either dynamically create your buttons, or find them using findViewById() and assign the on click listener

for(int i = 0; i < MAX_BUTTONS; i++){      // Create a button here      button.setOnClickListener(this);  }  

// onClick()

switch(view.getId(){      case R.id.btn_one:         // do something         break;      ...  }  

Answer by Ishant Sagar for Button array onclicklistener


    ****just implement View.OnClickListener and override the onClick(View v) as      public class YourActivity implements Activity implements View.OnClickListener      {       super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          f1 =(Button) findViewById(R.id.f1);          f2 =(Button) findViewById(R.id.f2);          f1.setOnClickListener(this);          f2.setOnClickListener(this);              @Override          public void onClick(View v) {          // TODO Auto-generated method stub                switch(v.getId())              {               case R.id.f1: //your implementation               break;               case R.id.f2: //your implementation               break;              }          }      }  

Answer by KEYSAN for Button array onclicklistener


define a global veriable like this in your activity.

private OnClickLister listener = new View.OnClickListener() {            @Override          public void onClick(View v) {              // TODO Auto-generated method stub              clearbutton();              ((Button)v).setBackgroundColor(Color.RED);              intbtnSelect=v.getTag();              }      }  

setTag() data every button in layout like 1-2-3------ whatever

and set to all button this listener.

f1 =(Button) findViewById(R.id.f1);  f1.setTag(1);  f2 =(Button) findViewById(R.id.f2);  f2.setTag(2);  f1.setOnClickListener(listener);  f2.setOnClickListener(listener);  

So, when you pressed the button, intbtnSelect will get the button's tag. You don't need implements View.OnClickListener from your activity.

Answer by Saurav for Button array onclicklistener


Considering the Android official documentation: http://developer.android.com/guide/topics/ui/controls/button.html

The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:

Be public

Return void

Define a View as its only parameter (this will be the View that was clicked)

So, in XML mention method name

android:onclick="onButtonClicked"  

and in Activity:

public void onButtonClicked(View v) {      switch (v.getId()) {      case R.id.Button1:        break;      case R.id.Button2:        break;      .      .      .      . and so on       case R.id.Button10:        break;      }  }  

Answer by NBK.Nacer for Button array onclicklistener


I have the same problem but I found the best solution ( at least for me ) first you need to create a list of Button, then int Array of id

    Button[] listBtn = new Button[3];      IntId[] idList = {R.id.btn1, R.id.btn2, R.id.btn3};        for(int i = 0 ; i < listBtn.length ; i++)          listBtn[i] = (Button) findViewById (idList[i]);        for(Button btn:listBtn)          btn.setOnClickListener(btnListener);        .......        private View.OnClickListener btnListener = new View.OnClickListener() {          @Override          public void onClick(View v) {                for(int i = 0 ; i < listBtn.length ; i++){                    if(v.getId() == idArray[i]){                        //do something                      listBtn[i].setText("It works fine !");                      Toast.makeText(MainActivity.this, listBtn[i].getText().toString(), Toast.LENGTH_SHORT).show();                  }              }            }      };  

`


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.