How to customize android title bar in fragments?

How to customize android title bar in fragments onCreateView

  • I am trying to change the view of titlebar by typical approach: @Override public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle args) { ... Window window = getActivity().getWindow(); window.requestFeature(Window.FEATURE_CUSTOM_TITLE); window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.brand_update_layout); ProgressBar progressBar = (ProgressBar) inflater.inflate(R.layout.brand_update_layout, group, false) .findViewById(R.id.title_progress); TextView title_Text = (TextView) inflater.inflate(R.layout.brand_update_layout, group, false) .findViewById(R.id.title_text); title_Text.setText(Html.fromHtml("...")); progressBar.setVisibility(View.VISIBLE); ... } But this code does not work on some reason. Whats wrong? UPD. ok, I declared Window window = getWindow(); window.requestFeature(Window.FEATURE_CUSTOM_TITLE); window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.brand_update_layout); in Activity and ProgressBar progressBar = (ProgressBar)getActivity().getWindow().findViewById(R.id.title_progress); TextView title_Text = (TextView)getActivity().getWindow().findViewById(R.id.title_text); title_Text.setText(...); progressBar.setVisibility(View.VISIBLE); in fragment, however I have an error: ERROR/AndroidRuntime(12213): java.lang.NullPointerException at com.quto.ru.CarListFragment.onCreateView(CarListFragment.java:188) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:837) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1359) on string title_Text.setText(...);

  • Answer:

    You are inflating a whole new view and trying to find the progressbar / textview in that newly inflated view by using this code: ProgressBar progressBar = (ProgressBar) inflater.inflate(R.layout.brand_update_layout, group, false).findViewById(R.id.title_progress); TextView title_Text = (TextView) inflater.inflate(R.layout.brand_update_layout, group, false).findViewById(R.id.title_text); Try changing it to this ProgressBar progressBar = (ProgressBar)window.findViewById(R.id.title_progress); TextView title_Text = (TextView)window.findViewById(R.id.title_text);

Roman C at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.