Search code examples
androidperformanceonclickdalvikddms

What is better - one long onClick method for all buttons, or many short methods for each button[android]


I have 12 buttons divided in 2 groups, each has 6 buttons, all buttons respond to one long onClick method goToCategory().

I can refactor it into many small independent onclick methods.

My app takes too much time to render images after click/touch happens - about 2-3 seconds. I launched ddms to see whats happening, did tracing and my app stumbles upon goToCategory() - at least i think it is the root problem causing render long delay. I may want to rewrite long onclick method.

What is better from performance viewpoint?

public void goToCategory(View v){
    switch (v.getId()){
        case R.id.scientists:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.galim1);
            hero2.setBackgroundResource(R.drawable.galim2);
            hero3.setBackgroundResource(R.drawable.galim3);
            upper_category_index = "science";
            break ;
        case R.id.scientists2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.galim1);
            hero5.setBackgroundResource(R.drawable.galim2);
            hero6.setBackgroundResource(R.drawable.galim3);
            lower_category_index = "science";
            break ;
        case R.id.politics:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.pol1);
            hero2.setBackgroundResource(R.drawable.pol2);
            hero3.setBackgroundResource(R.drawable.pol3);
            upper_category_index = "politics";
            break ;
        case R.id.politics2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.pol1);
            hero5.setBackgroundResource(R.drawable.pol2);
            hero6.setBackgroundResource(R.drawable.pol3);
            lower_category_index = "politics";
            break ;
        case R.id.akins:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.akin1);
            hero2.setBackgroundResource(R.drawable.akin2);
            hero3.setBackgroundResource(R.drawable.akin3);
            upper_category_index = "akin";
            break ;
        case R.id.akins2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.akin1);
            hero5.setBackgroundResource(R.drawable.akin2);
            hero6.setBackgroundResource(R.drawable.akin3);
            lower_category_index = "akin";
            break ;
    case R.id.folk_heroes:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.folk1);
            hero2.setBackgroundResource(R.drawable.folk2);
            hero3.setBackgroundResource(R.drawable.folk3);
            upper_category_index = "folk";
        break ;
        case R.id.folk_heroes2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.folk1);
            hero5.setBackgroundResource(R.drawable.folk2);
            hero6.setBackgroundResource(R.drawable.folk3);
            lower_category_index = "folk";
            break ;
        case R.id.hans:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.han1);
            hero2.setBackgroundResource(R.drawable.han2);
            hero3.setBackgroundResource(R.drawable.han3);
            upper_category_index = "hans";
            break ;
        case R.id.hans2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.han1);
            hero5.setBackgroundResource(R.drawable.han2);
            hero6.setBackgroundResource(R.drawable.han3);
            lower_category_index = "hans";
            break ;
        case R.id.batirs:
            categories.setVisibility(View.GONE);
            hero1.setBackgroundResource(R.drawable.kabanbai);
            hero2.setBackgroundResource(R.drawable.bogenbai);
            hero3.setBackgroundResource(R.drawable.karasai);
            upper_category_index = "batirs";
            break ;
        case R.id.batirs2:
            categories2.setVisibility(View.GONE);
            hero4.setBackgroundResource(R.drawable.kabanbai);
            hero5.setBackgroundResource(R.drawable.bogenbai);
            hero6.setBackgroundResource(R.drawable.karasai);
            lower_category_index = "batirs";
            break ;

    }
}

Solution

  • I think registering onClick in xml (layout) is better approach.

    Found related threads :

    1) Best practice for defining button events in android

    2) best practices for handling UI events