I am trying to create this theme for one, and just one of my activities:
Since I am learning how this styles work, and I am now beginning to be aware that we have more different & incompatible ways of styling in android than atoms in the whole universe. So I've tried to learn from this site: http://jgilfelt.github.io/android-actionbarstylegenerator
However when I try to apply its theme in my project that is using AppCompat I get the
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
error type.
I am extending from ActionBarActivity in my tabbed activity and I don't feel safe going back now (someone suggested extending Activity to solve this issue), since in previous posts I was suggested to use AppCompat, and ActionBarActivity seems somehow to be AppCompat related.
I've spent all my week googling for solutions and I've came across these problems:
The official Android styling documentation is deprecated.
The code generated by Android Studio is also deprecated in method such as this.*
http://jgilfelt.github.io/android-actionbarstylegenerator is deprecated.
BUT, to the point now, this is what I have at the moment:
So, the only thing I am not being able to do is
putting the logo in the top left corner of the screen (in the action bar), in a way that only affects the action bar of this particular activity, (Done)
make the orange bar showing which tab is selected.
How can I achieve this in the most simple way?
This is my values/style.xml file:
<resources>
<!-- Base application theme. -->
<style name="SmartCampusTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/actionbar_bg_color</item>
</style>
<style name="GenericProgressIndicator" parent="#android:style/Widget.ProgressBar.Small">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:indeterminate">true</item>
</style>
</resources>
My AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="greensmartcampus.eu.smartcampususerfeedbackapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:logo="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/SmartCampusTheme">
<!-- Splash screen while connecting to the web-server -->
<activity
android:name=".HomeScreen"
android:label="#string/title_activity_home_screen"
android:parentActivityName=".AbstractPortraitActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="greensmartcampus.eu.smartcampususerfeedbackapp.AbstractPortraitActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- The activity where all actions take place -->
<activity
android:name=".ControlActivity"
android:label="#string/title_activity_home_screen"
android:parentActivityName=".AbstractPortraitActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="greensmartcampus.eu.smartcampususerfeedbackapp.AbstractPortraitActivity" />
</activity>
</application>
</manifest>
*Ps. it's funny how the most up-to-date Android Studio generates deprecated code for tabbed activities and then complains about it being deprecated as if it is your fault...
Important Note: I am a huge fan of MVC and a modularity priest (OSGi practitioner). So I would love if all these styling solutions could be all contained in the style and drawing XML files (as they should be if I correctly recall), the layout information is all contained in my layout files and my java files only contain code about behavior. For example I could just show the action bar icon with java through getSupportActionBar().setDisplayShowHomeEnabled(true), but inserting layout related stuff in java disrupts the MVC pattern. Also, the website that I mention above can create the layout with the icon in the actionbar without using a single java line. I just need to know how can that be done with AppCompat, which apparently is what I am using because other solutions are deprecated.
Related
I've searched for the articles that are discussing about the same topic,
but it accidentally seems like there aren't any.
I actually had the problem when setting the layout & edit the style xml file like below:
<!-- Base application theme. -->
<style name="NoTitleBarFulScn" parent="#android:style/Theme.NoTitleBar.Fullscreen">
Customize your theme here.
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPreimaryDark">#color/colorPrimaryDark</item>
<item name="ceolorAccent">#color/colorAccent</item>
</style>
And then indicate the manifest file to use the NoTitleBarFulScn style:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/NoTitleBarFulScn"
android:screenOrientation="landscape" >
<activity android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Problem solved till here,
but the "Missing styles. Is the correct theme chosen for this layout?" error shows again when I was simply adding a new textview component...(It also shows an error about textview attributes missing below the missing style one.)
I tried also the sdk version setting & "invalidate caches / restart" option, but still doesn't work.
Also it seems like the new version of android studio has been set not to show the detail of the missing attributes it's saying, I still don't know where to find it.
Please give me some advice if possible,
thank you so much!
I've found the solution that I think is the most likely cause in the very beginning,
just simply add
<item name="android:textEditSuggestionItemLayout"></item>
<item name="android:textEditSuggestionContainerLayout"></item>
<item name="android:textEditSuggestionHighlightStyle"></item>
into the style definition of the XML file,
however it seems like the new version of android studio has been set not to show the detail of the missing attributes it's saying, I still don't know where to find it.
I am just beginning to learn Android Studio (version 2.0, running on Windows 7). When I create a new project (Empty Activity template) Android Studio places a header at the top of the project. How can I remove the header?
Here's a picture of the problem. I want to get rid of the 'My Greeting Card' header.
You have to simply navigate like below....
app -> res -> values -> styles.xml
Find <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> line.
Theme.AppCompat.Light.DarkActionBar line shows, how your activity will look like.
There are lots of Themes available. Just remove DarkActionBar it and press Cntrl+Space to explore another themes. In your case, you do not want Header which is called ActionBar in Android Terminology.
So, Just change theme to Theme.AppCompat.Light.NoActionBar and you are done.
To get more deeply about how the theming work and other courses of Android, Just try Pluralsight. Go to below link and get 6 months free subscription. It is very good website to learn any technology.
Pluralsight 6 Months Subscription
Well the answer is already given, but still i would like to add if you don't want any kind of actionbar on top of the activity simply do this.
go to your res->values->styles.xml and add these xml code
<resources>
<!-- Base application theme. -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#3F51B5</item>
<!-- Light Indigo -->
<item name="colorPrimaryDark">#3949AB</item>
<!-- Dark Indigo -->
<item name="colorAccent">#00B0FF</item>
<!-- Blue -->
</style>
<style name="AppTheme" parent="AppTheme.Base"></style>
</resources>
and reference the theme in the style on any activity you want, like below in the manifest using android:theme=#style/AppTheme.
<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=".DetailActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
you can hide Toolbar using
getActionBar().hide();
or
getSupportActionBar().hide();
For Newer Version
app -> res -> values -> themes -> themes.xml
In that it also depends whether the phone is in night mode or not.
Then You have to modify both the files:
themes.xml
themes.xml (night)
with
parent="Theme.MaterialComponents.DayNight.NoActionBar"
just Remove AppCompatActivity and extend it with only Activity
Recently I have read a chapter about dialogs in The Big Nerd Ranch Guide (2nd edition) and I have done everything by the book. However there are some issues, which are not mensioned in the book. Preview area of Intellij Idea doesn't work correctly with AppCompat themes.
This is my current theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
And this is what I have in preview area:
In the Internet I have read that using Base.Theme.AppCompat.Light.DarkActionBar may help. It removes the message, but ActionBar dissapeares also.
Oh, and DatePicker doesn't work with both and any Material Design:
This is just a render issue, because both themes work fine on devices. Can someone try to preview DatePicker in IDEA and Android Studio with AppCompat theme? May be this is just an IDEA issue.
P.S. dialog_date.xml
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_date_date_picker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="false"
/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.criminalintent" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".CrimeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CrimePagerActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
Whole project: https://onedrive.live.com/redir?resid=4653F2D263EA4131!21585&authkey=!AESze90_ZZ0p9WY&ithint=folder%2cproperties
Switching Themes
If you look at the declaration for Theme.AppCompat.Light.DarkActionBar, it aliases directly to Base.Theme.AppCompat.Light.DarkActionBar in AppCompat's values/themes.xml file:
<!-- Platform-independent theme providing an action bar in a dark-themed activity. -->
<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar" />
source
This isn't aliased in any of the other device configurations (e.g. values-v23/themes.xml, values-v18/themes.xml, etc). Have a look at the resouces for AppCompat here. This means that for every device, Theme.AppCompat.Light.DarkActionBar will always and only alias to Base.Theme.AppCompat.Light.DarkActionBar.
The docs for the Base theme make this even more clear:
Themes in the "Base.Theme" family vary based on the current
platform version to provide the correct basis on each device. You
probably don't want to use them directly in your apps.
Themes in the "Theme.AppCompat" family are meant to be extended or
used directly by apps.
source
So your switching from Theme.AppCompat.Light.DarkActionBar to Base.Theme.Light.DarkActionBar will never affect how things are displayed on actual devices and really isn't a change. The fact that Android Studio behaved differently is a problem with Android Studio (which I was unable to reproduce using your code on AS 2.0 preview 9).
Problem with Android Studio
I'll say that the Design tab has always been a bit flaky. It's getting better but hasn't always played nice, especially with support library stuff. To change your app's theme (even if what I said above wasn't true) just to make the Design tab look pretty is probably a bad idea. Think about your users. If you really want to know what your screen looks like, then deploy it to a real device or emulator.
Dialogs
I was also unable to reproduce your Dialog problem in AS 1.4 (stable) or AS 2.0 preview 9 (canary, ATM). But since your entire layout file is a DatePicker, I don't think you will get much benefit. This is even less beneficial when you consider that your DatePicker will be displayed in a dialog (which the Design tab is unable to simulate). I don't mean to sound harsh but you might just have to bite the bullet and use an emulator or physical device.
Make your style.xml look like this, toolbar will show up:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And your DatePicker needs to loo like this:
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:id="#+id/datePicker" />
If you need CalendarView in your xml add this insted of DatePicker:
<CalendarView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/calendarView1" />
Shitty Android Studio 1.5.0 should be updated to 1.5.1. So easy
I'm uncertain of the expected behavior when using ActionBarCompat and supportRequestWindowFeature(Window.FEATURE_NO_TITLE). I see two different behaviors depending on Android version:
On a Android ICS (possibly also from Android 3.0?) device using ActionBarCompat a call to supportRequestWindowFeature(Window.FEATURE_NO_TITLE) would hide the combined titlebar/actionbar
On a Android 2.3 device the actionbar is still visible even though supportRequestWindowFeature(Window.FEATURE_NO_TITLE) is called.
Some additional information: My activities extend android.support.v7.app.ActionBarActivity and my app uses the #style/Theme.AppCompat.Light theme. supportRequestWindowFeature(Window.FEATURE_NO_TITLE) is called before setContentView(). I never make any call to either getSupportActionBar().show() or hide().
So, what's the expected behavior? When I used ABS a call to requestWindowFeature(Window.FEATURE_NO_TITLE) would hide the titlebar/actionbar regardless of Android version.
How would I go about having an android app where some activies use an actionbar while others have none? Should they not extend the same base class?
So I've found a solution to this but it uses styles applied via the AndroidManifest (and therefore needs no code). Keep in mind that this will almost definitely only work if you're using the AppCompat library.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="..."
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<application
android:allowBackup="false"
android:icon="#drawable/launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar">
<activity
android:label="#string/log_in"
android:name=".activities.LogInActivity"
android:parentActivityName=".activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.MainActivity" />
</activity>
<activity
android:label="#string/app_name"
android:name=".activities.MainActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/sign_up"
android:name=".activities.SignUpActivity"
android:parentActivityName=".activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.MainActivity" />
</activity>
</application>
</manifest>
Note how I've got a global theme applied directory to the <application> tag, and then also a theme that's individually applied to my MainActivity class through its own <activity> tag. That is the only Activity where I want the Action Bar to be hidden; everywhere else I want it to be visible (which is why I don't have individual themes applied to any of my other Activities as I instead let them inherit from the global application theme).
But you'll quickly find that Theme.AppCompat.Light.NoActionBar doesn't actually exist. This is because I had to create that style myself, which requires two different styles.xml files:
values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.AppCompat.Light.NoActionBar" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
values-v11/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.AppCompat.Light.NoActionBar" parent="Theme.AppCompat.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
You must use two different styles.xml files for this as the android:windowActionBar flag didn't exist before Android API version 11.
I want to override a application theme on a particular activity, but its not working for me
This is code on my Manifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.Light" >
<activity
android:theme="#style/MyTheme"
android:name="com.ssdevnet.Splash"
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="com.ssdevnet.Home"
android:label="#string/app_name" />
</application>
And this is what i used in style:
<style name="MyTheme" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
</style>
and its still not working on Android 2.3.6 or below but working on 3 and above.
Also i tried to use this on the oncreate method on class file for that particular page:
requestWindowFeature(Window.FEATURE_NO_TITLE);// Removes title bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
but its not working on my phone running Android 2.3.6 but its working on Emulator running Jellybean and i need something that works on all the versions.
And I need to know some things as i am a newbie to android:
Can we define a Global theme, and override that theme on some activities? if yes, what method can we use?
Or we have to define theme for every activity if we are targeting activities instead of app?
I also want to learn about deploying the custom themes in the app.. if there are some good tutorials and references please share.
Thanks in Advance!
Try using setTheme(..) before calling setContentView(...) and super.oncreate() and it should work fine in the Activity.
Check out the Android developer's site for more information.