1

I am making an app. There is an ActionBar with settings option. In it there is an EditTextPreference. How can I get the Text which the user writes there? I tried a lot of code, but don't work. Could somebody help me?

Here is my MainActivity:

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener, LocationListener {

public static FragmentManager fragmentManager;
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
boolean intervall=false;

boolean started;

static String intervalloption = "INTERVALLOPTION";
Intent intervalloptionintent = new Intent(intervalloption);
Bundle intervalloptionbundle = new Bundle();

Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewpager = (ViewPager) findViewById(R.id.pager);
    ft = new FragmentPageAdapter(getSupportFragmentManager());

    fragmentManager = getSupportFragmentManager();

    actionbar = getActionBar();
    viewpager.setAdapter(ft);
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar
            .addTab(actionbar.newTab().setText("Run").setTabListener(this));
    actionbar
            .addTab(actionbar.newTab().setText("Statistics").setTabListener(this));

    viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {


    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();




    if (id == R.id.share) {
        Toast.makeText(getApplicationContext(), "share", Toast.LENGTH_LONG).show();
        return true;
    }
    if (id == R.id.action_settings) {
        Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
        startActivity(intent);
        Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();


        return true;
    }
    if (id == R.id.exit) {
        Toast.makeText(getApplicationContext(), "exit", Toast.LENGTH_LONG).show();
        finish();
        System.exit(0);

        return true;
    }

    return super.onOptionsItemSelected(item);
}


}

And this is SettingsActivity:

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{

static String intervalloption = "INTERVALLOPTION";
Intent intervalloptionintent = new Intent(intervalloption);
Bundle intervalloptionbundle = new Bundle();



@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //addPreferencesFromResource(R.layout.preferences);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    settings.registerOnSharedPreferenceChangeListener(this);
    Toast.makeText(getApplicationContext(), "Preference", Toast.LENGTH_LONG).show();
}



public static class MyPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener
{
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.preferences);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        if (key == "walk") {
            String walktime = sharedPreferences.getString(key, "walk");
            Toast.makeText(getActivity(), String.valueOf(walktime), Toast.LENGTH_LONG).show();
            // do stuff
        }


    }
}



public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    if (key == "walk") {
        String walktime = sharedPreferences.getString(key, "walk");
        Toast.makeText(getApplicationContext(), String.valueOf(walktime), Toast.LENGTH_LONG).show();
        // do stuff
    }


}


}
Lal
  • 14,726
  • 4
  • 45
  • 70
kalmi
  • 13
  • 6
  • 1
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – njzk2 Jan 21 '15 at 18:14

1 Answers1

3

Maybe its just

key.equals("walk")

instead of

key == "walk"

You should never compare strings with == but with .equals()

Also I think your preferences file should be in res/xml and not in res/layout, thus making your call to addPreferencesFromResource(R.xml.preferences)

You could try to do (in onCreate of your Fragment)

EditTextPreference pref = (EditTextPreference)findPreference(key of your EditTextPreference);
pref.setOnPreferenceChangeListener(this)

And implement the interface and then

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String walktime = newValue.toString();
 Toast.makeText(getActivity(), String.valueOf(walktime), Toast.LENGTH_LONG).show();
    return true;
}    
degill
  • 1,285
  • 2
  • 13
  • 19