Unable to display action bar on main activity - java

style.xml
here i purposely disable action bar because i dont want it to appear on my splash screen
<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/colorAccent</item>
</style>
MainActivity.java
After that I want action bar exist in Main activity by using set supportactionbar, but the action bar doesnt work after I run the code, and whole process is without error, can someone tell me what's wrong with my code.
Toolbar tb;
tb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tb);

You have two options.
1: Manually add a Toolbar in your XML. Example:
<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:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
</LinearLayout>
See https://guides.codepath.com/android/Using-the-App-Toolbar for more toolbar information.
OR
2: Create a new theme for your splash screen and set it in your manifest. Example:
<activity
android:name=".view.activity.SplashActivity"
android:theme="#style/Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
And then in your app's styles.xml:
<style name="Splash" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowFullscreen">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:windowBackground">#drawable/splash_screen</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Either one of these are fine, although personally I always keep a separate style for my splash screen just to keep things clean and simple.

Related

Activity showing different color than that set as background color

I have a simple activity that contains a RelativeLayout with background as white color. When launched sometimes the activity is white in color but other times it picks up a blue color which I have not declared in my theme. Where can I look?
I have noticed that the white background remain consistent only if I add a white backgroundtint color. Strange.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
</RelativeLayout>
Manifest
<application
android:theme="#style/AppTheme"
android:name=".Dagger.MyApplication"
android:allowBackup="false"
android:icon="#drawable/app_launch"
android:label="#string/app_name"
android:largeHeap="true"
android:roundIcon="#drawable/app_launch"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
tools:replace="android:allowBackup">
Themes
<!-- 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/black</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Colors
<color name="colorPrimary">#FFFFFF</color>
<color name="colorPrimaryDark">#000</color>
<color name="colorAccent">#FFFFFF</color>
<color name="themeColor">#FFFFFF</color>
<color name="primaryTextColor">#11CFC5</color>

The 'style' attribute is not declared

The code blocks in my activity_main.xml with the errors are as follows:
<EditText
style="#style/editText_style"
android:inputType="textEmailAddress"
android:id="#+id/editText1" />
<EditText
style="#style/editText_style"
android:inputType="textPassword"
android:id="#+id/editText2" />
The code in my styles are as follows:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="editText_style">
<item name="android:gravity">center</item>
<item name="android:background">#drawable/editText_background</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:ems">10</item>
<item name="android:layout_margin">10dp</item>
</style>
</resources>
I'm getting the error stating:
The 'style' attribute is not declared.
I dont understand what is wrong. Any help appreciated. Thank you.

Change background colour and action bar colour separately

I am trying to change my background colour for my app and the actionBar to different colours:
<!-- Base application theme. -->
<style name="actionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/veniceblue</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:background">#drawable/background_template</item>
<item name="actionBarTheme">#color/veniceblue</item>
</style>
But in my manifest file i add :
android:theme="#style/actionBar">
But I'm not sure how to add the background at the same time?
I do want it to be set for the whole application??
Excuse me if i didn't understand your question (i'm just a beginner trying to learn and help )
you can use android.support.v7.widget.Toolbar
layout.xml
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay"
android:id="#+id/appBarLayout">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
and style.xml
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AppCompaTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>

These two icons(drawerlayout and menu icon) in toolbar is black, How to turn it into white?

I just added drawerlayout in my app an changed the background color of toolbar,but drawerlayout button and menu icon are black , it should be white.
style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="actionMenuTextColor">#color/font</item>
<item name="android:actionMenuTextColor">#color/font</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
AppBarLayout:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll" >
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="30.0dip"
android:layout_height="match_parent"
/>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/tab_normal"
app:tabSelectedTextColor="#color/tab_se"
app:tabTextColor="#color/tab_normal"
/>
</android.support.design.widget.AppBarLayout>
And that is what app looks like:
Add a new style to change the color of hamburger icon and overflow menu icon.
To change Hamburger Icon color
<style name="DrawerArrowStyle" parent="#style/Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
To change Overflow Menu Color
Add this item to your current style.
<item name="android:textColorSecondary">#android:color/white</item>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">#android:color/black</item>
</style>
<style name="MyMaterialTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
<item name="android:windowAllowEnterTransitionOverlap" tools:targetApi="lollipop">true
</item>
<item name="android:windowAllowReturnTransitionOverlap" tools:targetApi="lollipop">true
</item>
<item name="android:windowSharedElementEnterTransition" tools:targetApi="lollipop">
#android:transition/move
</item>
<item name="android:windowSharedElementExitTransition" tools:targetApi="lollipop">
#android:transition/move
</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:statusBarColor">#color/white</item>
<item name="android:windowLightStatusBar">true</item>
</style>

Change the text color of the toolbar overview

I cannot find how to change the text color of the overview toolbar on Android.. :
I tried this one :
android:theme="#style/AppTheme"
<style name="AppBlankTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme" parent="AppBlankTheme">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">true</item>
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
I want to change the color of the title ("Sharezone") and the close button of the overview toolbar to white.
Thanks.
------------------------- EDIT -------------------------------------------
After a lot of failed solutions, here's my entire code :
style.xml
<style name="AppBlankTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme" parent="AppBlankTheme">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:textColorTertiary">#color/grey_300</item>
</style>
AndroidManifest.xml
<application
...
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
...
android:theme="#style/AppTheme">
</activity>
</application>
And here's the toolbar I use in Activity :
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<TextView
android:id="#+id/toolbar_app_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/app_name"
android:textSize="30sp"
android:gravity="center_horizontal|center_vertical"
android:textColor="#color/grey_300"/>
</android.support.v7.widget.Toolbar>
This is because of Theme.which you have set in your style
style name="AppBlankTheme" parent="Theme.AppCompat.Light.DarkActionBar"
please use this Theme
style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"
Now your App's dark theme will be removed
You can do that by XML, if your activity is called MainActivity go to the res/layout/activity_main, go to design and press/select the action bar, then to properties on the right and select titleTextColor and change tot he required colour.
Choose colour here:
#. Seems your themes are OK. In AndroidManifest.xml file, apply AppTheme to your desired Activity.
AndroidManifest.xml
<activity
android:name=".YourActivity"
android:theme="#style/AppTheme" />
#. If you are using Toolbar in your layout XML, then you can change its title color programmatically:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar)
toolbar.setTitleTextColor(Color.WHITE);
UPDATE:
As per your updated question, seems you are using custom Toolbar. To change title "Sharezone" color, you have to change the background color of TextView (toolbar_app_name).
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<TextView
android:id="#+id/toolbar_app_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/app_name"
android:textSize="30sp"
android:gravity="center_horizontal|center_vertical"
android:textColor="#FFFFFF"/>
</android.support.v7.widget.Toolbar>
Hope this will help~

Categories