Search code examples
androidbuttontextsettext

Text of buttons are not changing


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");
    }

Solution

  • 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");
    }