Setting a Window.FEATURE_CUSTOM_TITLE creates AndroidRuntimeException - android

I can't seem to get past this error:
android.util.AndroidRuntimeException: You cannot combine custom titles
with other title features
I am running API > 14.
Manifest is as follows:
<activity
android:name=".activity.ActivityWelcome"
android:label="#string/app_label_name"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:theme="#style/My.Theme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
My activity is doing the following:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.welcome);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title_main);
...
Where R.layout.window_title_main is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_title"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dp" >
<TextView
android:id="#+id/textview_title"
android:drawableLeft="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="-1"/>
</LinearLayout>
Why is this not working when it seems to work for others?

This is the kind of error that will drive you to insanity and beyond.
So I happen to be using Theme.Holo as my parent theme. (The theme that my own theme extends)
The documentation says:
...the action bar is included in all activities that use the Theme.Holo
theme (or one of its descendants), which is the default theme when
either the targetSdkVersion or minSdkVersion attribute is set to "11"
or greater.
http://developer.android.com/guide/topics/ui/actionbar.html#Adding (first paragraph)
Ok, so when I try to go through the process above (my question) I get the error:
You cannot combine custom titles with other title features
Well, that's because, by default action bar is setup already and it won't allow you to use a custom title bar as well.
Documentation reveals that "each activity includes the action bar":
This way, when the application runs on Android 3.0 or greater, the
system applies the holographic theme to each activity, and thus, each
activity includes the action bar.
So, I will now focus my time on altering the action bar and not the title bar!

FEATURE_CUSTOM_TITLE
and
<item name="android:windowNoTitle">true</item>
Are mutially exclusive. Remove
<item name="android:windowNoTitle">true</item>
and it will work again.

As a beginner most of the answers didn't help me for my case. So here is my answer.
Go to res/values folder in your android project and check for strings.xml (this file may vary in your case, something like themes.xml)
Inside the file under resource tag check whether you have style tags. If you don't find it, add the code below as mentioned below as a child to resources tag
something like below
<resources>
<style name="SomeNameHere">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
if you already have style tag, just add the code below to your style tag
<item name="android:windowNoTitle">true</item>

Related

android: back button disappear from toolbar after the app is killed

I have an activity that contains a toolbar with a back button like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
style="#style/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />
</LinearLayout>
and here is the toolbar style used
<style name="toolbar">
<item name="navigationIcon">?attr/homeAsUpIndicator</item>
</style>
The back button is working as expected, but I am facing a weird error where the back button disappears only when I kill the app from recent apps or the app crashes and I reopen the app.
like the following picture
but if I click on the place where the button is supposed to be is looks like the following picture
Was it ever a darker color?
Because now I see that you may have set a toolbar back button drawable to the same color of your toolbar. You can check if you have the same problem when you change the background of the toolbar view.
One solution is to change the android:homeAsUpIndicator icon drawable in your app theme:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:homeAsUpIndicator">#drawable/dark_back_button_drawable</item>
</style>
When you apply a theme to a view you have to specify the parent theme if you want the normal behavior with a specific part modified.
<style name="AppTheme.BackNavigationToolbar" parent="Widget.AppCompat.Toolbar">
<item name="navigationIcon">?attr/homeAsUpIndicator</item>
</style>
If you decide to use the Toolbar which you get from a activity and not declare it in xml. You can just use the setDisplayHomeAsUpEnabled(true). If you want to read more on this check this page out: https://developer.android.com/training/appbar/up-action
supportActionBar?.setDisplayHomeAsUpEnabled(true)
If it is two activities you are navigating between then you just have to specifiy parent activity in the AndroidManifest.xml
Exampel
<activity android:name=".SecondActivity"
android:label = "Second Activity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
More resources:
https://developer.android.com/training/appbar/setting-up

How to change the style of a single view within a fragment

Goal: I am attempting to apply two different themes in one Fragment. One for the fragment's overall xml and the other for a particular view within that fragment's xml.
Reason/Problem: The reason for this is that it doesn't seem possible to change the entire background of a Floating Action Button to a vector using MaterialComponents, but it does work with appCompat.
Initially, I tried to change the theme in the xml using style="..." as shown below, but it appears to be reverting back to the theme declared in the manifest which is AppTheme1. I know this because I tested it by switching the theme declared there. That is, when I switched it to AppTheme2, the vector loaded properly in the FAB, but it crashed elsewhere since I have MaterialComponents throughout the app.
Solution: I think the obvious solution for this would be to change the theme for just the Floating Action Button in question, but I don't know how to do that. Any help much appreciated. Thank you!
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/nationality"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/flag_united_states_of_america"
app:borderWidth="0dp"
style="#style/AppTheme2"
app:maxImageSize="56dp" />
Themes:
<style name="AppTheme1" parent="Theme.MaterialComponents.Light.NoActionBar" >
&&
<style name="AppTheme2" parent="Theme.AppCompat.Light.NoActionBar" >
With MaterialComponents:
With AppCompat:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fabtest">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So when the theme is changed from AppCompat to Material Components, the image resource no longer applied properly to the Floating Action Button. So I just want to apply AppCompat for Floating Action Button but keep the Material Components as the main style.
In your case you can use:
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:tint="#null"
app:srcCompat="#drawable/flag_united_states_of_america"
..>
The FloatingActionButton in the Material Components uses the colorOnSecondary attribute defined in your app theme to tint its icon (and the default value is #000000). If you don't want to tint the icon you have to use app:tint="#null".
In general if you want to use a custom style in your FAB you can use:
<com.google.android.material.floatingactionbutton.FloatingActionButton
style="#style/MyCustomFab"
..>
with:
<style name="MyCustomFab" parent="#style/Widget.MaterialComponents.FloatingActionButton>
.....
<item name="backgroundTint">#color/....</item>
<item name="tint">#color/....</item>
</style>
If you want to override the attribute defined in your app theme you can use:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:theme="#style/fab_themeoverlay"
..>
with:
<style name="fab_themeoverlay" parent="#style/Widget.MaterialComponents.FloatingActionButton>
<item name="colorPrimary">#color/...</item>
</style>
If you want to change the theme of the button, which has implications for how it is inflated, you should use android:theme instead of android:style.
Both Ben P. and Gabrielle were semi-correct. The answer required both a change in the theme via:
android:theme="#style/SecondTheme"
and setting the tint to null:
app:tint="#null"
Both were required in this case
All together, given two themes, this is the correct XML for a FloatingActionButton if you want to change it's entire background to a vector while your primary theme is MaterialComponents as referenced in your app's manifest:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:borderWidth="0dp"
app:srcCompat="#drawable/your_vector"
app:tint="#null"
android:theme="#style/SecondTheme"
app:maxImageSize="56dp" />
Thank you Gabrielle and Ben for your help!

AppCompat v7 and DatePicker render issue

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

Application's icon not displayed in ActionBar

I'm new in the Android programming world, and I don't understand why the system is not displaying the application icon on the left side of the 'ActionBar' of my first Android application (HelloWorld).
In the 'ActionBar' developer guide it's stated that:
"By default, the system uses your application icon in the action bar, as specified by the icon attribute in the "application" or "activity" element. However, if you also specify the logo attribute, then the action bar uses the logo image instead of the icon."
(Action bar)
My Android-manifest file isn't defining a 'logo' attribute, but it defines just an 'icon' attribute. Therefore, the icon defined by the 'icon' attribute should be displayed on the left side of the 'ActionBar' as the application icon. However, no icon is being displayed.
My Android-manifest file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.holamundo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppBaseTheme">
<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>
</application>
</manifest>
And my style files are the following:
/res/values/styles.xml:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
/res/values-v11/styles.xml:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
</style>
</resources>
/res/values-v14/styles.xml:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
</resources>
Can anyone explain me why my application icon is not being displayed in the ActionBar and what should I modify in order to such an icon be displayed? Thanks.
Your activity must extend ActionBarActivity.
For example:
public class MainActivity extends ActionBarActivity
{
...
}
Or try this in your activity:
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.ic_launcher);
You will have to use getSupportActionBar() if using the support library.
I've also been having the problem where a menu item icon is not being displayed. I have a toolbar and I set it up to be the support action bar. The manifest declares a NoActionBar theme. Originally I thought that none of the icons for action items would display, however, I've discovered that some drawable icons would display. For, example "#android:drawable/ic_menu_add" would work, but "drawable/ic_launcher_background" would not work. Here's the menu structure that I inflate during the onCreateOptionsMenu. The item action_logo uses a png file and that one works too. The 'ic_launcher_background" is a drawable that is automagically added when the project was created.
<item
android:id="#+id/action_help"
android:orderInCategory="50"
android:title="#string/action_help"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="always" />
<item
android:id="#+id/action_more"
android:orderInCategory="60"
android:title="#string/action_help"
android:icon="#drawable/ic_launcher_background"
app:showAsAction="always" />
<item
android:id="#+id/action_logo"
android:orderInCategory="70"
android:title="#string/action_help"
android:icon="#drawable/tabellae_logo"
app:showAsAction="always" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
What I've been trying to do was to generate a BitmapDrawable on the fly an add it to the menu during a onPrepareOptionsMenu call, but that kind of drawable won't display either.
I have different test application that has it's manifest set to an ActionBarTheme. In this configuration, you don't need to call setSupportActionBar. (It's error if you try to do so) Now this test application displays all the menu icons correctly. It is an AppCompatActivity application. My guess is that this test application is probably using the old style ActionBar rather then the newly recommended ToolBar approach and that's why it works.
My preference is to use the ToolBar approach, but I don't have an explanation why some icon drawables work and other do not. In my research on this problem, I've not seen anyone mention these observations. Perhaps someone would have more insight into why certain drawables don't work for menu item icons.

How can i be generic my title bar?

i have a title bar one of my project and i want to use my all screen.I think that i can do be a general( means i want a something and i will call everywhere like a style tag or theme tag) i have no idea.What can i use for?
My title bar is here:
RelativeLayout
android:layout_height="44dip"
android:layout_width="320dip"
android:background="#drawable/merhaba_header_320_44dip">
<ImageView
android:layout_height="32dip"
android:layout_width="121dip"
android:background="#drawable/merhaba_logo_121_32dip"
android:layout_centerInParent="true">
</ImageView>
<ImageView
android:layout_height="30dip"
android:layout_width="64dip"
android:background="#drawable/merhaba_btn_nedir_64_30dip"
android:layout_margin="5dip"
android:layout_alignParentRight="true">
</ImageView>
</RelativeLayout>
Hi DuyguK are you asking for a way to have a title bar that you can apply as a theme or something to all the activities in your app?
If that's so, I would recommend you to do the following.
First define a style in your res/values/styles.xml
<resources>
<style name="MyTheme" parent="android:style/Theme.Light">
<item name="android:windowTitleSize">44dip</item>
//whatever item you want/need to edit for your custom title bar
</style>
</resources>
This allows you to have a theme that is applyable to your whole app. To do this you need to go to your AndroidManifest.xml file an inside the application tag add the following:
<application android:label="#string/app_name"
android:theme="#style/MyTheme">
The android:theme tells the app to use the theme named "MyTheme" that can be found in the res/values/styles.xml file in your project.
That way you can apply correctly your custom title bar to all your activities.
Hope this answers your question if not, please specify!
An additional recommendation is to take a look at the ActionBarCompat project that comes with the samples in Android SDKs. The Android Developers page shows the project:
http://developer.android.com/resources/samples/ActionBarCompat/index.html
It has the advantage of being compatible with pre-API11 devices, is applied as a theme to your app and it is being used by lots of apps available in the store.
If you have any questions/trouble with it please tell me.
create your own style which can ofcourse extend from existing from an existing theme.
change the windowNoTitle to true.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="noTitleBarStyle" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#color/darkGrey</item>
<item name="android:colorForeground">#ff3333</item>
</style></resources>

Categories

Resources