Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow hard coded strings and stop asking about using strings.xml

I'm working currently on an android project and I have no plans in mind whatsoever to translate it to other languages, so I'm not saving string literals in strings.xml. However Android Studio keeps complaining everytime I hard code a string literal especially when setting the text value for a TextView.

enter image description here

enter image description here

enter image description here

Is there a way to disable these warnings?

like image 874
razz Avatar asked Jan 02 '16 01:01

razz


4 Answers

you can edit it in the following location Settings->Editor->Inspections->Android Lint->TextView Internationalization:

Editor

and for the xml Settings->Editor->Inspections->Android Lint->Hardcoded Text:

XML

like image 107
DZDomi Avatar answered Nov 10 '22 07:11

DZDomi


The best way to do this in my opinion is to use a gradle file, this will allow you to suppress these globally without having to do it in Android Studio, so your settings can go into source control as well, and then you don't have to individually decorate each method you want to apply the warning to. To do this, disable SetTextI18n in your gradle files lint options as follows:

android {
    lintOptions{
        disable 'SetTextI18n'
    }
}

Gradle sync and voila, warnings gone.

like image 21
Trevor Hart Avatar answered Nov 10 '22 07:11

Trevor Hart


Add

@SuppressLint("SetTextI18n")

on top of your function(s). Example:

@SuppressLint("SetTextI18n")
private void updateEZWBMode2ConnectionStatus(){
    switch (IsConnected.checkMode2(mContext)){
        case IsConnected.USB_TETHERING:
            M2StatusTV.setText("Status: Connected");
            M2StatusTV.setTextColor(Color.argb(255,0,255,0));
            M2ReceiveTV.setVisibility(View.VISIBLE);
            startTestReceiverSafe(M2ReceiveTV);
            break;
        case IsConnected.USB_CONNECTED:
            M2StatusTV.setText("Status: No Tethering");
            M2StatusTV.setTextColor(Color.argb(255,255,51,51));
            M1ReceiveTV.setVisibility(View.GONE);
            M2ReceiveTV.setVisibility(View.GONE);
            stopTestReceiverSafe();
            break;
        case IsConnected.USB_NOTHING:
            M2StatusTV.setText("Status: No USB Connection");
            M2StatusTV.setTextColor(Color.argb(255,255,51,51));
            M1ReceiveTV.setVisibility(View.GONE);
            M2ReceiveTV.setVisibility(View.GONE);
            stopTestReceiverSafe();
            break;
    }
}
like image 5
Constantin Geier Avatar answered Nov 10 '22 07:11

Constantin Geier


Old question, but anyway the accepted answer is misleading.

There is no need to translate string literal resources. In fact, they can be marked as non-translatable in the resources file. This way, you'll still adhere to best practices while not being annoyed by lint and translations.

<string name="invite_sent" translatable="false">Invite sent</string>

While disabling Lint my be useful to stop being annoyed, there is a further reason you want to (and should) use string literal resources: repetition. Following the DRY (Don't Repeat Yourself) principle can avoid a miriad of problems down the line, from complex refactors to unexpected behaviour, as well as inconsistency in the user(s) experience while using the app. Just imagine that an "Ok" button is present in 10+ screens. Having a single reference and source simplifies and centralizes maintenance of the project.

like image 1
necavit Avatar answered Nov 10 '22 06:11

necavit