Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This Activity already has an action bar supplied by the window decor?

I am making an Android app using Toolbar with Custom style but when i'm running the app, i'm getting the following error:

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

Styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:textColor">@color/textColorPrimary</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="android:textColorSecondary">@color/textColorPrimary</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

<style name="MyMaterialTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>

</style>


<style name="MyEditTextTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Used for the bottom line when not selected / focused -->
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:colorBackground">@color/colorBackground</item>
    <item name="android:textStyle">bold</item>
    <item name="android:endColor">@color/colorPrimary</item>




    <!-- colorControlActivated & colorControlHighlight use the colorAccent color by default -->
</style>

<style name="TabTextAppearance" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:colorBackground">@color/colorBackground</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:textStyle">bold</item>
</style>

MainActivity:

public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_information_category1);
    Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar_information);
    setSupportActionBar(toolbar);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);


}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new TodayInformationFragment(), "Today");
    adapter.addFragment(new ThisWeekInformationFragment(), "This Week");
    adapter.addFragment(new ThisMonthInformationFragment(), "This Month");
    adapter.addFragment(new AllyearInformationFragment(), "All");
    viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}

like image 698
Bharat Sindhav Avatar asked Jan 17 '16 17:01

Bharat Sindhav


People also ask

How do I get rid of action bar?

If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme. AppCompat.

Which view can be used to customize action bar?

Custom Action Bar Layout The view layout consists of two ImageButtons that represent forward and back image buttons and a TextView in the center.

What is windowActionBar?

android:windowActionBar denotes property for lollipop and above only. Where as windowActionBar denotes for all versions and is retrieved from support library. Follow this answer to receive notifications.


1 Answers

Just go to your Manifest file and in the specific activity add

android:theme="@style/AppTheme.NoActionBar"/>
like image 69
Martin Kinuthia Avatar answered Sep 28 '22 08:09

Martin Kinuthia