0

I'm trying to change text by variable (language), but even when value of language variable is "albanian" , text of buttons are not changing!

P.S variable value is passed from another activity.

String language="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_level_activity_layout);

    btnBegginer=(Button)findViewById(R.id.btnBegginer);
    btnMedium=(Button)findViewById(R.id.btnMedium);
    btnHard=(Button)findViewById(R.id.btnHard);

    Intent objIntent=getIntent();
    language=objIntent.getStringExtra("language");

    //Toast.makeText(getApplicationContext(),language,Toast.LENGTH_LONG).show();

    if (language=="albanian")
    {
        btnBegginer.setText("FILLESTAR");
        btnMedium.setText("MESATARE");
        btnHard.setText("VESHTIRE");
    }
    else
    {
        btnBegginer.setText("BEGGINER");
        btnMedium.setText("MEDIUM");
        btnHard.setText("HARD");
    }
  • You should not hardcode strings, especially if it's subject to localization. You should put the strings in your `string.xml` file and provide alternative files depending on system settings. See [this](https://developer.android.com/guide/topics/resources/localization.html) for more details – Sunshinator Jun 29 '17 at 15:22

2 Answers2

0

When comparing Strings in Java use the equals() method. ex: stringValue.equals("Test") return true/false.

Yossi Segev
  • 607
  • 5
  • 12
0

Don't ever use == for comparing strings use .equals()

if (language.equals("albanian"))
{
    btnBegginer.setText("FILLESTAR");
    btnMedium.setText("MESATARE");
    btnHard.setText("VESHTIRE");
}
else
{
    btnBegginer.setText("BEGGINER");
    btnMedium.setText("MEDIUM");
    btnHard.setText("HARD");
}
Sony
  • 7,136
  • 5
  • 45
  • 68