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!
Related
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
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.
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.
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>
I have three buttons to change themes. On clicking each button my app theme must change dynamically. How to do it programmatically.
To set the theme dynamically at runtime, call setTheme() in your activity's onCreate() method, before calling setContentView(). To change the theme, you simply need to restart your activity.
Here is a nice tutorial on how dynamically apply themes.
and this one too.
Please Visit this link for it.
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="#string/hello" />
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
By defining an xml theme file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
You can also apply styles to all activities of an application:
<application android:theme="#style/CustomTheme">
Or just one activity:
<activity android:theme="#android:style/Theme.Dialog">
Sorry but you can't change styles programmatically.
how to set the Style Attribute Programmatically in Android?
There are certainly other methods to achieve this desired behavior however. You could set onclick listeners for each button, and programmatically change text size, color, background etc of your various view elements
you can use a particular theme for a given xml file.
in graphical layout you can CHANGE the theme of the layout using editing config.
use onclick event to go to next layout and here your theme will be different from the first one.
Vimalatha, to change your background when you click a button just add this code to the onClick function of your button.
myLinearLayout.setBackgroundColor(Color.BLUE);
Assuming that myLinearLayout is your LinearLayout name...