0
votes

I am trying to make a common layout form my multiple activity . This commone layout is having drawer layout,custom toolbar and framelayout. The framelayout can be used to host any activity layout.

I am getting error while setting my toolbar i.e setSupportActionBar(toolbar)

Error:

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.

I already tried setting following but no luck <"windowActionBar">false <"windowNoTitle">true

drawer_toolbar_layout.xml

   <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:id="@+id/map_layout"
          xmlns:android="http://schemas.android.com/apk/res/android">
          <android.support.v4.widget.DrawerLayout"
            android:id="@+id/drawerlay"
            android:layout_width="match_parent"
            android:layout_height="fill_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <include
                    android:id="@+id/common_toolbar"
                    layout="@layout/toolbar"/>
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/framlay"/>
            </LinearLayout>
            <RelativeLayout
                android:id="@+id/relative_layout"
                android:layout_marginTop="?attr/actionBarSize"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="start" >
                <ListView
                    android:id="@+id/drawer_list"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:background="@drawable/shadow_rectangle"
                    android:choiceMode="singleChoice"
                    android:divider="@color/logoorange"
                    android:dividerHeight="1dp"/>
            </RelativeLayout>
        </android.support.v4.widget.DrawerLayout>
    </RelativeLayout>

activity java code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer_toolbar_layout);
    FrameLayout mframeLayout = (FrameLayout) findViewById(R.id.framlay);

    LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    View activityview = inflater.inflate(R.layout.activity_usr_order_history, null);
    mframeLayout.addView(activityview);
    Toolbar mToolbar = (Toolbar) findViewById(R.id.common_toolbar);
    setSupportActionBar(toolbar);
}

common_toolbar.xml

    <?xml version="1.0" encoding="utf-8"?>

    <android.support.v7.widget.Toolbar   
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:id="@+id/toolbar_id"
      android:layout_height="?attr/actionBarSize"
      android:layout_margin="0dp"
      android:background="#2196f3"
      android:contentInsetLeft="10dp"
      android:contentInsetRight="10dp"
      android:contentInsetStart="10dp"
      android:minHeight="?attr/actionBarSize"
      android:padding="0dp"
      app:contentInsetLeft="10dp"
      app:contentInsetRight="10dp"
      app:contentInsetStart="10dp">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <RelativeLayout
            android:id="@+id/toolbar_rl_incart"
            android:visibility="visible"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <android.support.v7.widget.AppCompatImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:id="@+id/toolbar_iv_cartimg"
                android:gravity="center"
                app:srcCompat="@drawable/ic_add_shopping_cart_black_24dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/toolbar_tv_itemcnt"
                android:layout_marginTop="5dp"
                android:text="0"
                android:textStyle="bold"
                android:layout_centerHorizontal="true"
                android:textColor="@color/white"
                android:gravity="center"
                android:textSize="12sp"/>
        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom|right"
            android:layout_gravity="bottom"
            android:id="@+id/toolbar_tv_orderhistory"
            android:textSize="16sp"
            android:alpha="0.5"
            android:visibility="visible"
            android:textColor="@color/cb_errorRed"
            android:background="@drawable/shadow_button_rectangle"
            android:text="My Orders" />

    </LinearLayout>
</android.support.v7.widget.Toolbar>

<--Styling theme code-->

<style name="AppTheme_NoAction" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item
</style>

menifest.xml

<activity> 
  android:name=".UsrOrderHistory_Activity"
  android:configChanges="keyboardHidden|orientation"
  android:theme="@style/AppTheme_NoAction"
</activity>

style.xml

<style name="AppTheme_NoAction" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

With following lines commented the activity is working fine:

setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);  
getSupportActionBar().setHomeButtonEnabled(false);

At this time i dont know the consequences...

3
you are using a theme that already has action bar that is why you are getting exceptionJAAD
it is possible that you are adding it multiple timesJAAD
can you show your import of Toolbar (get by findViewById)? is it THIS class? I don't see it in any XML...snachmsm
@Raju I didn't any "Toolbar" in your xml that's why you are getting this error !!! use <android.support.v7.widget.Toolbar> as your action bardex
@snachmsm ... import android.support.v7.widget.Toolbar; is the import of toolbar.Raju Jindal

3 Answers

0
votes

Do you have this?

"values/styles.xml, values-v11/styles.xml, and values-v14/styles.xml"

then you change value-v11/styles.xml and vlues-v14/styles.xml.

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

0
votes

You just need to add this line in your values/styles.xml file in main style tag:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
....
</style>
0
votes

Go to your styles res/values/styles and change AppTheme's parent to <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> and then design you toolbar like this......

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

EDIT Make sure that setContentView(R.layout.drawer_toolbar_layout) points to the layout which contains common_toolbar. Because in your layout, toolbar id is toolbar_id. Clean all your styling and just edit a parent theme.