Is there a better way to do this:
    int drawable;
    switch(mSignalStrength) {
        case 0:
            drawable = R.drawable.signal_0;
            break;
        case 1:
            drawable = R.drawable.signal_1;
            break;
        case 2:
            drawable = R.drawable.signal_2;
            break;
        case 3:
            drawable = R.drawable.signal_3;
            break;
        case 4:
            drawable = R.drawable.signal_4;
            break;
        case 5:
            drawable = R.drawable.signal_5;
            break;
        default:
            drawable = -1;
    }
I'm trying to replace the switch statement with some int getDrawableIdByString("signal_" + mSignalStrength) function.
As someone mentioned in the comments, you can use the
getIdentifier(String name, String defType, String defPackage) 
For you case, like this
int resId = getResources().getIdentifier("signal_" + mSignalStrength, "drawable", getPackageName());
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With