Search code examples
androidandroid-actionbarpagerslidingtabstrip

Actionbar not showing with PagerSlidingTabStrip


I have created a sliding tab by using PagerSlidingTabStrip. it is created correctly but the action bar get lost it is showing like this :

enter image description here

Is any help me on this why action bar is disabled.

import com.astuetz.PagerSlidingTabStrip;


public class SMainTabActivity extends FragmentActivity implements ETabFragment.OnFragmentInteractionListener,
    MTabFragment.OnFragmentInteractionListener{

public String classSelected;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarIndeterminateVisibility(true);
    setProgressBarIndeterminateVisibility(false);
    setContentView(R.layout.activity_subject_main_tab);

    Bundle b = getIntent().getExtras();
    classSelected = b.getString("classId");
  // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new TabFragmentPagerAdapter(getSupportFragmentManager()));

    // Give the PagerSlidingTabStrip the ViewPager
    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    // Attach the view pager to the tab strip
    tabsStrip.setViewPager(viewPager);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main_actionsbar, menu);

    return super.onCreateOptionsMenu(menu);


   // getMenuInflater().inflate(R.menu.activity_main_actionsbar, menu);
    //return true;
}

xml (activity_subject_main_tab):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    app:pstsShouldExpand="true"
    app:pstsTextAllCaps="true"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:textSize="14sp"
    android:textColor="#000000"
    app:pstsDividerColor="@color/color1"
    app:pstsIndicatorColor="@color/color2"
    app:pstsUnderlineColor="@color/color3"
    app:pstsTabPaddingLeftRight="14dp">
</com.astuetz.PagerSlidingTabStrip>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white" />

manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SubjectMainTabActivity"
        android:label="@string/title_activity_subject_main_tab" >
    </activity>
</application>

Expected is :

enter image description here


Solution

  • It could simply be that in your xml file, you made you Actionbar 0dp. Another issue could be that you are not using the right theme for the App. The theme that gets the action bar would be set in your style.xml like so:

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
    
    </style>
    
    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    

    Then to call it in your AndroidManifest.xml, like so:

    android:theme="@style/MyMaterialTheme"
    

    NOTE: You would need to add the colors in the styles.xml to your choosing. Also, If you are completely stuck, use this example for a better more Android supported Material Design Guide http://www.android4devs.com/2014/12/how-to-make-material-design-app.html