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.
source share