In my main activity I have an edit text field where whatever is entered changes the actionbar text.

I need to store that value and add it to all my other activities so when the user enters the text it changes all of the action bars.

This is the code that changes sets the actionbar text but I need to take the value stored in getSupportActionBar().setTitle(editTextHouse.getText().toString()); and add it to all other activities.

        buttonSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            getSupportActionBar().setTitle(editTextHouse.getText().toString());
            }
        });

1 Answers

1
user8194042 On Best Solutions

Try this:

        private static final String NAME = "my_pref";
        private static final String ACTION_BAR_TITLE = "action_bar_title";
        private SharedPreferences sharedPreferences;
        private SharedPreferences.Editor editor;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);


          }
...
buttonSet.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                String title = editTextHouse.getText().toString();
                getSupportActionBar().setTitle(title);
                editor = sharedPreferences.edit();
                editor.putString(ACTION_BAR_TITLE, title);
                editor.apply();
                }
            });

...

Then get a title in another activity like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
                        String title = sharedPreferences.getString(ACTION_BAR_TITLE, "Default Title"); // your title
                        getSupportActionBar().setTitle(title);

                  }