android - handle toolbar appearance individually on different Activities -
i have 2 activities activity1 , activity2
and in both activities added toolbar
codes
activity1:
<android.support.v7.widget.toolbar android:layout_height="wrap_content" android:layout_width="match_parent" android:minheight="?attr/actionbarsize" android:id="@+id/toolbartrendingactivity" android:elevation="5dp" android:textalignment="center" android:background="@color/colorprimary" app:theme="@style/toolbarstyle"> <textview android:layout_width="wrap_content" android:layout_height="match_parent" android:textalignment="center" android:layout_gravity="center" android:id="@+id/toolbar_title" android:textsize="20sp" android:textstyle="bold" android:background="#00000000" android:textcolor="#ffffff" android:text="title" /> </android.support.v7.widget.toolbar>
activity2
<android.support.v7.widget.toolbar android:layout_height="wrap_content" android:layout_width="match_parent" android:minheight="?attr/actionbarsize" android:id="@+id/toolbartagdetailactivity" android:elevation="5dp" android:textalignment="center" android:background="@color/colorprimary" app:theme="@style/toolbarstyle"> <textview android:layout_width="wrap_content" android:layout_height="match_parent" android:textalignment="center" android:text="toolbar2" android:layout_gravity="center" android:id="@+id/textview_toolbartagdetailactivity_title" android:textsize="20sp" android:textstyle="bold" android:background="#00000000" android:textcolor="#ffffff" /> </android.support.v7.widget.toolbar>
as can see both toolbars have unique id
i'm making activity2's
toolbar transparent working fine when i'm switching activity2
activity1
activity1's
toolbar transparent
for getting i'm doing on activity1:
@override protected void oncreate() { toolbarbackground = toolbar.getbackground(); } @override protected void onresume() { super.onresume(); int sdk = android.os.build.version.sdk_int; if(sdk < android.os.build.version_codes.jelly_bean) { toolbar.setbackgrounddrawable(toolbarbackground); } else { toolbar.setbackground(toolbarbackground); } }
but above code not doing tried set background color manually
but not returning correct color , primarycolor blue toolbar's background purple color
anyone can guide me how can fix ??
edit
toolbarstyle :
<style name="toolbarstyle" parent="themeoverlay.appcompat.dark.actionbar"> <item name="android:textcolorprimary">@android:color/white</item> <item name="android:textcolorsecondary">@android:color/white</item> <item name="actionmenutextcolor">@color/coloraccent</item> <item name="android:background">@color/colorprimary</item> <item name="android:windowactionbaroverlay">true</item> </style>
default toolbar (desired toolbar)
current toolbar (after switching activity2)
you did not need use this,
toolbarbackground = toolbar.getbackground();
and no need in onresume().
simply use set
toolbar.setbackgroundresource(r.color.price_color);
in activity after block of code in oncreate()
toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar);
and if getting error in
toolbar.setbackgroundcolor(r.color.price_color);
use contextcompat.getcolor method that
toolbar.setbackgroundcolor(contextcompat.getcolor(this, r.color.primary_color));
Comments
Post a Comment