Skip to content Skip to sidebar Skip to footer

Why Android Is Truncating My ActionBar Title?

In my app, I change the title in the ActionBar from each fragment displayed. When I first start my apps, I got a list of requests, so my title is 'My requests (20)'. Then, when you

Solution 1:

I know this question was posted a long time ago, but I recently ran into this issue and it caused a hell of a headache. I'm working on a Galaxy Note 10.1 and my title was "BidItems" and I only had the action overflow item visible, yet sometimes, "BidItems" became "BidIt..." which is just ridiculous. After much testing, I found that the reason for this truncating behavior in my application is that I was calling a method using ((MyActivity) getApplication()).setTitle(); method from one of my fragments. The setTitle() method in my Activity calls getActionBar().setTitle(). As soon as I called this method, my title was truncated for no reason. But simply calling setTitle() from my activity worked just fine.

I really hope this saves people the headache it caused me.


Solution 2:

I guess this problem is solved by refreshing action bar UI.

So I had solved with below codes.

ActionBar actionBar = getActivity().getActionBar();
actionBar.setTitle(title);

// for refreshing UI
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);

Solution 3:

In my particular case, I'm developing a hybrid app with a complex native menu structure. I'd see this issue intermittently when calling a deeplink from the hybrid content that would update the selected menu option and set the title.

I tried several of the suggested fixes with not luck. But setting a custom view produced a strange result that gave me the feeling that I was dealing with a race condition.

This proved true. I found that simply overriding the activity's setTitle function and wrapping the call to super in a postDelay runnable fixed this for me:

@Override
public void setTitle(final CharSequence title) {
    toolBar.postDelayed(new Runnable() {
        @Override
        public void run() {
            MainActivity.super.setTitle(title);
        }
    }, 200);
}

I'm using toolbar's postDelayed as a convenience method. But you can certainly use a handler here. Hope this helps.


Solution 4:

put setTitle() in onCreateOptionsMenu helps me to solve this problem.

In your fragment add setHasOptionsMenu(true); in onCreateView(){}

Then override onCreateOptionsMenu().

Example:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    // put getActivity in onCreateOptionsMenu will not return null
    if (getActivity() != null) {
        getActivity().setTitle(getResources().getString(R.string.Studies));
    }
}

Reference: How to display android actionbar title without truncation occurring


Solution 5:

I solved this problem using a custom title as described in this post.

This is the code I use to change the title when a tab changes

((TextView) actionBar.getCustomView().findViewById(R.id.title)).setText(someTitle); 

Note that this solution places the title to the right of the tabs in landscape mode when using actionbar tabs.


Post a Comment for "Why Android Is Truncating My ActionBar Title?"