ActionBar Header Does Not Display

I implemented an action bar, but its title is not displayed. My application also has two fragments, the name should change in accordance with the displayed fragment.

public class MainActivity extends FragmentActivity { /** Called when the activity is first created. */ AppSectionsPagerAdapter mAppSectionsPagerAdapter; ViewPager mViewPager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setupActionBar(); mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager()); // user swipes between sections. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mAppSectionsPagerAdapter); mViewPager.setOnPageChangeListener(new OnPageChangeListener() { public void onPageSelected(int position) { } public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } public void onPageScrollStateChanged(int state) { if (state == ViewPager.SCROLL_STATE_DRAGGING) { } if (state == ViewPager.SCROLL_STATE_IDLE) { } } }); } public static class AppSectionsPagerAdapter extends FragmentPagerAdapter { public AppSectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: return new AllPatient(); case 1: //Intent intent = new Intent(MainActivity.this,abc.class); return new LaunchpadSectionFragment(); } return null; } @Override public int getCount() { return 2; } } private void setupActionBar() { ActionBar actionBar = getActionBar(); //actionBar.setDisplayShowHomeEnabled(false); actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM); ViewGroup v = (ViewGroup)LayoutInflater.from(this) .inflate(R.layout.header, null); actionBar.setCustomView(v, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.RIGHT)); actionBar.setDisplayShowTitleEnabled(true); actionBar.setTitle(getTitle()); } } 

Also tell me how to add separators between the icons in the action bar. Thanks in advance.

+4
source share
3 answers

Try changing this line:

 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM); 

Under this:

 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME, ActionBar.DISPLAY_SHOW_CUSTOM); 
+2
source

I think the problem is that you are adding a custom view with a MATCH_PARENT width, and Android in this case removes the title of the action bar (but not the home icon). This is not very logical, but this is the behavior that I see in my application. Setting the width to WRAP_CONTENT poses the same problem. The solution is to provide a custom view with a fixed width, or if you want the width to vary from one activity to another, calculate the width programmatically and set it to a new fixed width for each activity.

+1
source

Add a show title flag so

 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM); 
+1
source

Source: https://habr.com/ru/post/1484614/


All Articles