0

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 ;

    }
}
ERJAN
  • 23,696
  • 23
  • 72
  • 146
  • 3
    There will be no noticeable performance difference either way, especially for a method like this that will be called very rarely. – JesusFreke Oct 02 '15 at 05:13

2 Answers2

1

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

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
1

The android:onClick with function binding in XML Layout is a binding between onClick and the function that it will call. The function have to have one argument (the View) in order for onClick to function.

https://developer.android.com/reference/android/widget/Button.html

SO COURTESY

Registering android:onClick is a better way.

How exactly does the android:onClick XML attribute differ from setOnClickListener?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198