Usman Khan
Usman Khan

Reputation: 3973

How to set the text on action bar programmatically

I am working on android application in which i am using sherlock action bar. I want to change the text on my action bar programmatically when user clicked it. I have done it in Edit Text as:editText.setText("Save"); when user clicked textView. I want to do it on run time when i clicked on action bar text it should be changed to "save" My code for action bar is given below:

enter image description here

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/editTxt"
        android:showAsAction="always|collapseActionView"
        android:title="Edit"/>

</menu>

@Override
        public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
            MenuInflater inflater = getSupportMenuInflater();
            inflater.inflate(R.menu.main, menu);
            return true;
        }


        @Override
        public boolean onOptionsItemSelected(
                com.actionbarsherlock.view.MenuItem item) {
            switch (item.getItemId()) {

            case R.id.editTxt:
                return true;

            default:
                finish();
            return super.onOptionsItemSelected(item);
        }
        }

Upvotes: 2

Views: 5990

Answers (1)

Rogue
Rogue

Reputation: 779

Changing ActionBar title

If you're running API >= 11, then :

getActionBar().setTitle("Hello");

Or, with API < 11 :

getSupportActionBar().setTitle("Hello");

To change the ActionBar's text.

EDIT: Changing MenuItem text on click

@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
    switch (item.getItemId()) {
        case R.id.editTxt:
            item.setTitle("Hello");
            return true;

  ......

Upvotes: 5

Related Questions