Android - Handling a grid - android

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
}
}

Related

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.

OnClickListener for many views

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.

Why is there no getOnClickListener in the Button class? (Android)

Why is there no getOnClickListener in the Button class? I think this is really strange considering there is a getOnFocusChangeListener function. Why make it for the FocusChangeListener and not for the ClickListener?
Added comment:
For those below that are wondering why I need this: We are developing a large application with a lot of viewgroups on the screen. I want to add some code to a button on the screen but not replace the complete OnClickListener. I want to implement a new OnClickListener that will run some code and call the old OnClickListener. But for that I need to retrieve the old one.
I don't know why there is not, but you can do what you want to do by extending the button class:
public class Button extends android.widget.Button implements OnClickListener {
public void onClick(View v) {
/* Your code here...*/
super().onClick(v);
}
}
I think it's a question to Google :D
Why do you need to get a onClickListener back? If you are so desperate, store it in a tag (Views.setTag(...));

Which is better in implementing click listener?

Which is a good practice in implementing click listener and why? Or is there a better way other than the two? Thanks.
First :
sampleButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// do something
}
});
Second : implement OnClickListener then override onClick method?
The third option is to set the listener directly in your XML layout:
android:onClick="myClickHandler"
and then implement it in your Activity:
public void myClickHandler(View v){
// do something
}
You're technically doing the 2nd thing with the 1st one. The 1st case uses whats called an anonymous class which implements OnClickListener, but since is anonymous, doesn't have a class name and isn't editable from external classes. Explicitably implementing OnClickListener is useful if you expect to use the same onClick functionality in multiple different locations, or if the click code is long
The first approach is used when you want to perform the action only for a particular case, if many click events require the same action then use the second one.

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