Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my (working) code:</p> <pre><code>public class MainActivity extends ActionBarActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener { private ActionBar.Tab mTab; private ActionBar mActionBar; private ViewPager mPager; private MainViewPagerAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initTabs(); } /** * Function creates all tabs with initializations and creates action bar */ private void initTabs(){ // Create an actionbar mActionBar = getSupportActionBar(); mActionBar.setDisplayHomeAsUpEnabled(true); mActionBar.setTitle(R.string.first_tab_title); mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM); mActionBar.setDisplayHomeAsUpEnabled(true); mPager = (ViewPager) findViewById(R.id.fragment_pager); mAdapter = new MainViewPagerAdapter(getSupportFragmentManager()); mPager.setAdapter(mAdapter); mPager.setCurrentItem(0); mPager.setOnPageChangeListener(this); // Create Actionbar Tabs mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Create first Tab mTab = mActionBar.newTab().setTabListener(this); mTab.setText(R.string.first_tab_title); mTab.setIcon(R.drawable.tab_pet_selected); mActionBar.addTab(mTab); // Create Second Tab mTab = mActionBar.newTab().setTabListener(this); mTab.setText(R.string.demo_tab_title); mTab.setIcon(R.drawable.tab_vet_selected); mActionBar.addTab(mTab); // Create Third Tab mTab = mActionBar.newTab().setTabListener(this); mTab.setText(R.string.demo_tab_title); mTab.setIcon(R.drawable.tab_news_selected); mActionBar.addTab(mTab); // Create Third Tab mTab = mActionBar.newTab().setTabListener(this); mTab.setText(R.string.demo_tab_title); mTab.setIcon(R.drawable.tab_emergency_selected); mActionBar.addTab(mTab); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_about: Intent i = new Intent(MainActivity.this, AboutActivity.class); startActivity(i); return true; } return false; } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { mPager.setCurrentItem(tab.getPosition(), true); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onPageScrolled(int i, float v, int i2) { } @Override public void onPageSelected(int i) { mActionBar.getTabAt(i).select(); } @Override public void onPageScrollStateChanged(int i) { } </code></pre> <p>}</p> <p>XML:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout_parent" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"&gt; &lt;android.support.v4.view.ViewPager android:id="@+id/fragment_pager" android:layout_width="match_parent" android:layout_height="match_parent"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>And here is the result: (Please don't worry about graphic ;) ) <img src="https://i.stack.imgur.com/KcYj7.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload