schuetzi
schuetzi

Reputation: 35

Android Button: Shorten OnClick method

Is it possible to have multiple buttons calling the same method? I mean the parameters would be the ID of the button. My problem is that I got a really, really long switch case function and every case has the same methods. Here is my code snippet (I have shortened it):

public class HerkunftRind extends Activity implements   View.OnClickListener, Animator.AnimatorListener {

private static final String TAG = "HerkunftRind";
ViewFlipper viewFlipper;
ImageButton myButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.herkunft_rind);

    viewFlipper = (ViewFlipper) findViewById(R.id.herkunft_rinder_view_flipper);

    ImageButton imageButton = null;
    imageButton = (ImageButton) findViewById(R.id.button_1);
    imageButton.setOnClickListener(this);
    imageButton = (ImageButton) findViewById(R.id.button_2);
    imageButton.setOnClickListener(this);
    imageButton = (ImageButton) findViewById(R.id.button_3);
    imageButton.setOnClickListener(this);
    imageButton = (ImageButton) findViewById(R.id.button_4);
    imageButton.setOnClickListener(this);
    }
public void setAnimationFade(int id) {
    myButton = (ImageButton) findViewById(id);
    ObjectAnimator animator = ObjectAnimator.ofFloat(myButton, View.ALPHA, 0.5f, 1f);
    animator.setDuration(300); //ms
    animator.start();
    animator.addListener(this);
}
@Override
public void onAnimationStart(Animator animation) {
    myButton.setAlpha(1f);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button_1: {
            setAnimationFade(R.id.button_1);
            text = (TextView) findViewById(R.id.button_1_text_1);
            text.setVisibility(View.VISIBLE);
            text = (TextView) findViewById(R.id.button_1_text_2);
            text.setVisibility(View.VISIBLE);
            break;
        }
        case R.id.button_2: {
            setAnimationFade(R.id.button_2);
            text = (TextView) findViewById(R.id.button_2_text_1);
            text.setVisibility(View.VISIBLE);
            text = (TextView) findViewById(R.id.button_2_text_2);
            text.setVisibility(View.VISIBLE);
            break;
        }
        case R.id.button_3: {
            setAnimationFade(R.id.button_3);
            text = (TextView) findViewById(R.id.button_3_text_1);
            text.setVisibility(View.VISIBLE);
            text = (TextView) findViewById(R.id.button_3_text_2);
            text.setVisibility(View.VISIBLE);
            break;
        }
        case R.id.button_4: {
            setAnimationFade(R.id.button_4);
            text = (TextView) findViewById(R.id.button_4_text_1);
            text.setVisibility(View.VISIBLE);
            text = (TextView) findViewById(R.id.button_4_text_2);
            text.setVisibility(View.VISIBLE);
            break;
        }

What I don't want is to define it directly in the XML file. Can I make this shorter?

Upvotes: 1

Views: 262

Answers (3)

Ruchira Randana
Ruchira Randana

Reputation: 4179

If you don't want a long onClick method, then you could write an onClick listener to each button.

E.g:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });


anotherButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

Upvotes: 1

this here is a little bit redundant:

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button_1: {
            setAnimationFade(R.id.button_1);
            text = (TextView) findViewById(R.id.button_1_text_1);
            text.setVisibility(View.VISIBLE);
            text = (TextView) findViewById(R.id.button_1_text_2);
            text.setVisibility(View.VISIBLE);
            break;
        }
        case R.id.herkunft_rinder_bauer2_button: {
            setAnimationFade(R.id.button_2);
            text = (TextView) findViewById(R.id.button_2_text_1);
            text.setVisibility(View.VISIBLE);
            text = (TextView) findViewById(R.id.button_2_text_2);
            text.setVisibility(View.VISIBLE);
            break;
        }
...

why you just dont call setAnimationFade before the hole switch case..??

 public void onClick(View v) {
        setAnimationFade(v.getId());
        switch (v.getId()) {
            case R.id.button_1: {
                setAnimationFade(R.id.button_1

Upvotes: 1

Phantômaxx
Phantômaxx

Reputation: 38098

Yes, you can, I personally do that very often.
You can call the method in the xml layout.

i.e.:

<Button
     android:onClick="myMethod()"
/>

Mind that myMethod() must have a signature like

public void myMethod(View v)

Inside myMethod you may then want to discern which is the View you clicked (which can also be heterogeneous) and act consequentially.

Just add a switch() where you use v.getId() to determine which is the View which fired the event.

Something like this:

switch(v.getId())
{
    case R.id.txtPhone:
    {
        // Do something
        // ...
        break;
    }
    case R.id.txtMenu:
    {
        // Show options menu
        // ...
        break;
    }
    // ...
    default:
    {
        break;
    }
}

Upvotes: 2

Related Questions