OnClickListener for many views - android

I have done some onclick listeners before in earlier projects but i have have never done like 100-200 onclicks. The idea is a horizontal scroll that AddImagViews to it if a variable is == somethin and if you click the imageview the imageview will setText to an TextView.
I have done the set imageview part but is there any easier way then creating like 100 different onclick for each imageview. Sorry if this was bad explained and difficult to understand.

Sure, have your Activity implemenent OnClickListener something like:
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.splash_startScan:
// do something
break;
case R.id.splash_startReview:
// do something
break;
}
}
then for each button do
button.setOnClickListener(this);

You can create a generic onClick event that reacts to the view that is passed to it. You can use a decision statement (like an if statement) to determine the button, but if all you need is the text from the button you can get that generically by casting the view back into a button and getting its text.
Button button = (Button)v;
button.getText().toString();

Might be best to create a class that implements onClickListener and has a constructor that passes in what you need and then just set the listener to a new instance of that object with the right parameters.

Related

How can a widely used custom button track which class its onclick is called from?

In my application, I have a custom button that is used in many different custom views.
The behavior differs very slightly depending on which view the button was clicked in, I want to have one #OnClick implementation in my custom button class that takes care of every case in order to avoid having similar code in every custom view class.
Is there a way I can determine in the #OnClick block where the button was clicked from?
It looks something like
public class customButton extends appcompactimagebutton {
//stuff
#OnClick
public void onbuttonclick(){
//handles general behavior
//if (clicked from customViewA) {
//do A stuff
//}
}
}
and then I have customViewA that has a customButton within it, I'm not sure what to put in the if statement, if that's even a proper way to handle something like this
You can use the tag attribute. In your XML layouts, add a line
android:tag="viewname"
to the XML for the button (in code you can do .setTag("viewname"). You can then just modify your onClick to check the tag using this.getTag() and use that to identify where the click came from.

best implementation of onclick() [duplicate]

This question already has answers here:
Best practice for defining button events in android
(10 answers)
Closed 7 years ago.
i just started developing in android a couple of weeks ago. i am working a simple app that has 2 activities but one activity has 15 buttons. right now i am using
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {/* Some Code */ });
for each button inside my onCreate. this approach seems excessive to me and maybe not the best way.
I know about the 5 different forms of using onClick and implementing onClickListeners but I am asking which way would be the proper way of doing it. Best practices.
Thank You.
In that case, I might add implements View.OnClickListener to the activity class, and then make a single onClick that looks like:
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.button1:
// handle button1 click
break;
case R.id.button2:
// handle button3 click
break;
case R.id.button3:
// handle button3 click
break;
// etc...
}
}
This only really makes sense if the actions of all those buttons are somehow related, and if they are then it can significantly reduce code duplication. You would still have to do view.setOnClickListener(this) for each one, too.
Alternatively, if you want you can remove the implements View.OnClickListener and the #Override and simply register your method in the XML, like Kevin said. That would allow you to have multiple of these "grouped" click listeners.
I think that the best way to do it when you have so much buttons is to implement the View.OnClickListener to your activity and then to the following:
For every single button just one line:
button.setOnClickListener(this);
override the onClick() method and check the id of the selected button and do something with the currently selected button like this for example:
// onClick is called when a view has been clicked.
#Override
// Parameter v stands for the view that was clicked.
public void onClick(View v) {
// getId() returns the id of the clicked view.
if(v.getId() == R.id.button1){
//you clicked the first button
}else if(v.getId() == R.id.button2){
//you clicked the second button
} else if ... // and so on for all of your buttons
}
You can use the xml attribute onClick:
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myFancyMethod" />
<!-- even more layout elements -->
More Information

Android - Handling a grid

I am trying to make a 2x2 grid of buttons and handle them. Right now I have a relative view activity with four buttons...but my question is: is the best way to do this? Than give each button a listener? Or is there anyway to add the buttons to the GridView and handle them all in one method?
Ex.:
Instead of using something like
if(button1x1)...
if(button1x2)...
if(button2x2)...
if(button2x1)...
and write a method for all of them, is there a way for me to just have one method and it will automatically detect which button is being pushed? Sorry if this is a confusing question, I can think it perfectly but translating to words is a bit difficult. Thanks for any help!
First of you you can do
public class YourActivity extends Activity implements OnClickListener {...
and then implement the onClick method as
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.A_UI_Element:
//do what you need for this element
break:
case R.id.A_Different_UI_Element:
//do what you need for this element
break;
//continue with cases for each element you want to be clickable
}
}

android: xml attribute android:onClick, why the method needs View parameter

When I create an android:onClick attribute in the activity's xml, the method defined by onClick needs to have the View parameter, why View?
i.e.
onClick method in my activity----------> public void sayHello(View v){...}
consider the next code, I'm not using the view variable, but I still need to pass it in method, how come?:
public void onClick(View view){
TextView t= new TextView(this);
t=(TextView)this.findViewById(R.id.textView2);
t.setText("new text");
}
So you know which View is calling the method.
It's like implementing the OnClickListener for you activity, the method created is onClick(View v) (or arg0 depending on your Eclipse), defining it from xml is just specifying a sort of listener for the View, and the method from the listener as that argument.
Once you're in the method, you can do a switch for the id of the button, to perform different actions:
public void myOnClickMethod(View v){
switch(v.getId()){
case R.id.button1:
//Do something for button 1
break;
case R.id.button2:
//Do something for button 2
break;
}
}
In short. Android just implements the OnClickListener for you when you define the android:onClick="myOnClickMethod" attribute.
Before answering the question I would like to mention what a view is...
Android app contains activities which are like screens which further contains GUI elements(such as buttons). In simple words, those GUI elements are called views.
Answer for your question...
Just imagine a situation in which your activity has more than one button (Let's consider it to be 5) and on getting clicked those buttons make a call to the same function (namely onClick()).So, how would the program know which button was clicked. The answer is simple just pass a view parameter to the onClick() function which will allow access to the information regarding the button that was clicked.

Android, Bunch of imagebuttons

I am a learning via a book so please forgive this newbie question.
I have a bunch of imageButtons in my xml, here is how one of them looks:
<ImageButton android:src="#drawable/level1" android:layout_width="wrap_content"
android:id="#+id/imageButton1" android:layout_height="wrap_content"
android:onClick="button_clicked1"></ImageButton>
and processing code:
public void button_clicked1(View v) {
text1.setText("clicked");
}
rather than have each button have its separate onClick code, is there anyway I can pass which button was clicked? for example button_clicked(1) and then button_clicked(2) instead of button_clicked1 like it is now (in the above example xml code)
or i have no choice but have to do it separately?
Kind of - what I like to do is make my View or Activity implement View.OnClickListener.
public class MyView extends ImageButton implements OnClickListener
Then during onCreate, I do something like:
((ImageButton)findViewById(R.id.imageButton1)).setOnClickListener(this);
then, in my onclick:
public void onClick(View view){
switch(view.getId()){
case R.id.imageButton1:
// do something.
break;
case R.id.imageButton2:
// do somethign else.
break;
}
Of course, you can definitely get creative and toss the switch statement if any of your buttons should trigger the same event behavior. Also, I'm not in a place where I can easily view my droid references so there may be an OnClickListener specific to ImageButton - if so, implement that on your containing View or Activity to consolidate the handlers...
Hope that makes sense - happy coding!
B

Categories

Resources