mardi 31 octobre 2017

If statement gives condition is always true

When I use If statement it gives me that the condition x is always true although I use the same code in another app and its work ,but in this case I use it in onOptionsItemSelected method for my menu ,So can someone help ??

public class Wellcome extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wellcome);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

        }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.chang_language, menu);
    return true;
}
 Boolean x=true;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
   int id = item.getItemId();
   if (id == R.id.arabic) {
       if (x =true) {
           setLocale("en");
           x = false;
       } else  if (x =false) {
           setLocale("ar");
           x = true;
       }
     return true;
   }
    return super.onOptionsItemSelected(item);
}
public void setLocale(String lang) {
    java.util.Locale myLocale = new Locale(lang);
    DisplayMetrics dm = getResources().getDisplayMetrics();
    Configuration conf = getResources().getConfiguration();
    conf.locale = myLocale;
    getResources().updateConfiguration(conf, dm);
    Intent refresh = new Intent(this, Wellcome.class);
    startActivity(refresh);
       }
     }

Aucun commentaire:

Enregistrer un commentaire