I have a very simple <style> in styles.xml:
<style name="roundActionButton">
<item name="background">#drawable/action_button_background</item>
</style>
I'm applying this style to a FrameLayout in one of my activities:
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="#id/okButton"
app:layout_constraintBottom_toBottomOf="#id/okButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintDimensionRatio="1"
style="#style/roundActionButton"
android:padding="7dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp">
The FrameLayout is visible, but the style is not applied in the Android Studio design preview, no matter how much I rebuild the project or force refresh the layout. However, if I set the background directly in the FrameLayout, it is displayed as expected in the design preview as well:
<FrameLayout
...
android:background="#drawable/action_button_background">
The theme in the design preview is set to AppTheme (I've tried others too but no change). Shouldn't the style be visible in the design preview?
You are missing android: in style...
<style name="roundActionButton">
<item name="android:background">#drawable/action_button_background</item>
</style>
Related
I am having issues when using android:fontFamily when set via TextAppearance styles.
Setting global font through android:fontFamily in theme works
Setting android:fontFamily directly on TextViews works (I am using AppCompat and it correctly inflates the AppCompatTextView during inflation from xml, so no issues here)
When setting the font via styles, it just does not work. Any help will be much appreciated. I am thinking that this has to do with how AppCompatTextView processes the style attributes, but have not had much luck in finding the exact root cause.
<style name="TextAppearance.Header">
<item name="android:fontFamily">#font/headerfont</item>
</style>
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.Header" />
Try this...hope it will work.
<style name="TextAppearance.Header">
<item name="android:fontFamily">#font/headerfont</item>
</style>
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/TextAppearance.Header" />
I just recently updated my Android Studio and ever since doing that the ThemeOverlay property is not showing on my buttons in the preview/design editor which it did prior to the update. However when I run the App on the Device the ThemeOverlay works. Below is some test code for the button. I have set the background attribute of the Button to Blue and the text of the button to Test Me
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Me"
android:background="#0000FF"
android:layout_marginTop="20dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark"/>
</LinearLayout>
The theme attribute is set to TheOverLay.AppCompat.Dark so that the text on the button is light but it's not showing in the design editor/preview in Android Studio. The text still shows as dark/Black. When I run this on a connected device it works fine.
Is this because of something that has changed with the current update?
You can set this inside your base theme:
<item name="android:colorButtonNormal">#color/some_color</item> which is global basically, all the buttons will have the color you defined inside your theme file.
On the other hand, I believe you cannot get the button theme from ThemeOverlay. Instead, create a new style and set the parent to Button.
<style name="CustomButton" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#color/Red</item>
<item name="android:colorButtonNormal">#color/Red</item>
<item name="android:textColor">#color/White</item>
</style>
And you can apply the style as:
android:theme="#style/CustomButton"
I am trying to get some buttons to be use the Material Design style (in particular accentColor tinting) for Android 4.4 devices using the AppCompat library. I have had success with the following:
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/continue_button"
android:id="#+id/continue_button"
android:layout_gravity="center_horizontal|bottom"
style="#style/CompatButton"
android:theme="#style/ThemeOverlay.AppCompat.Dark"/>
where "#style/CompatButton" has "Widget.AppCompat.Button.Colored" for a parent. However some of my buttons are the same but instead of declaring the style in the element, I attach the style as the default "buttonStyle" in the theme being used:
<style name="AppTheme">
...
<item name="android:buttonStyle">#style/CompatButton</item>
<item name="colorAccent">#color/flow_accent</item>
...
</style>
and
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/continue_button"
android:id="#+id/continue_button"
android:layout_gravity="center_horizontal|bottom"
android:theme="#style/ThemeOverlay.AppCompat.Dark"/>
These buttons are showing up with the default, non-Material stylings. This also seems to happen with ProgressBar. Can anyone see what's wrong with this and if there's a workaround without having to explicitly define the button style?
Oops, that was dumb. Obviously ThemeOverlay wipes out the previous theme, including the buttonStyle definition. For it to work we have to add the buttonStyle back in:
<style name="FlowOverlay" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:buttonStyle">#style/CompatButton</item>
</style>
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/continue_button"
android:id="#+id/continue_button"
android:layout_gravity="center_horizontal|bottom"
android:theme="#style/FlowOverlay"/>
though that loses much of the convenience we wanted from default button styles in the first place.
I'm Software Engineering student that just started learning android development a couple months ago during my spare time.
at the moment im making my first app while learning in the process and i ran into a problem.
I'm using a DialogFragment and for some reason the Accent color i use in my theme is overridden only in pre-lollipop devices (both emulator and physical).
you can notice that only the floating hint is tinted in lollipop.
my DialogFragment layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_add_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/input_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="#string/dialog_add_title_hint"
android:imeOptions="actionNext"
android:inputType="text"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/input_password_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="#string/dialog_add_password_hint"
android:imeOptions="actionGo"
android:inputType="text"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/input_cancel"
style="?attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="#+id/input_confirm"
style="?attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_weight="1"
android:text="Add" />
</LinearLayout>
my values/styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlHighlight">#color/colorHighlight</item>
<!-- Context Action Mode will Overlay Toolbar -->
<item name="windowActionModeOverlay">true</item>
</style>
i already tried several solutions to no avail:
overriding colorControlActivated and colorControlNormal - only changes floating hint tint
changing the tint programmatically, but that only changes the underline while not focused:
Drawable background = InputTextLayout.getEditText().getBackground();
DrawableCompat.setTint(background, yourColor);
InputTextLayout.getEditText().setBackground(background);
using EditText alone, not wrapped in TextInputLayout, doesn't help either.
any ideas?
Thanks in Advance
after a thorough search through the internet i stumbled upon a question here in stackoverflow regarding animating a DialogFragment - here
the answer in that link referred to a post by a Google Engineer saying:
...DialogFragment is just a wrapper around a Dialog to help manage its lifecycle. Dialogs are top-level windows so their animations are window animations, just like when you use Dialog in all other situations. You thus control the animations of dialogs through the theme or explicit window animation style you have set in its WindowManager.LayoutParams.
so i decided to check how to theme the DialogFragment
& found this guide by CodePath - here
basically, in your App Theme, in order to override the Dialog Themes you have to add the following:
<style name="AppTheme" parent...>
....
<!-- this will override DialogFragment theme -->
<item name="android:dialogTheme">#style/MyDialogFragmentTheme</item>
<!-- this will override AlertDialog theme -->
<item name="android:alertDialogTheme">#style/MyAlertDialogTheme</item>
</style>
<style name="CustomAlertDialogTheme.Animation">
...
</style>
<style name="MyDialogFragmentTheme" parent="Theme.AppCompat.Light.Dialog">
...
</style>
in each of the theme you can override attributes like colorPrimary, colorAccent etc.
if using this method makes your DialogFragment appear without a title (it happend to me), then add the following to its style:
<item name="android:windowNoTitle">false</item>
Bonus - adding animations
in order to add animations to either the AlertDialog or DialogFragment, write the following in its style:
<style name="MyDialogFragmentTheme" parent="Theme.AppCompat.Light.Dialog">
...
<item name="android:windowAnimationStyle">#style/MyDialogFragmentTheme.Animation</item>
</style>
then you need to create a style for the animation, for example:
<style name="MyDialogFragmentTheme.Animation">
<item name="android:windowEnterAnimation">#anim/dialog_slide_in_up</item>
<item name="android:windowExitAnimation">#anim/dialog_slide_out_down</item>
<item name="android:interpolator">#android:interpolator/anticipate</item>
</style>
there's more info in the CodePath guide linked above.
note that this all takes place in values/styles.xml
hope this helps other people.
I am testing out my app for API21 on Android! Unfortunately, my buttons seem weird - there is some kind of background color added. The background images did not change - they are completely transparent except for the border. (The different text and size is due to the screenshot).
Here you see the buttons before and since API 21: http://imgur.com/9EEcl0o,yVEnJkI#0
I already tried android:elevation="0dp" and android:background="#android:color/transparent". Anybody knows why my buttons change? Thank you very much!
layout.xml:
<Button android:id="#+id/sm_achievements_btn"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.1"
android:background="#drawable/menu_btn_selector"
android:onClick="showAchievements"
android:text="#string/sm_achievements_btn"
android:padding="#dimen/padding_std"
android:layout_marginTop="#dimen/margin_small"
android:textColor="#color/button_text"
android:textSize="#dimen/text_xlarge"
style="#style/lbm_button"/>
menu_btn_selector.xml:
<item android:drawable="#drawable/menu_button"
android:state_pressed="false"/>
style.xml:
<style name="lbm_button" parent="android:Theme.Holo.Light">
<item name="android:textAllCaps">false</item>
</style>
Material buttons have a default stateListAnimator that provides state-based elevation (e.g. 0dp when disabled, 1dp when enabled). You can clear it by setting android:stateListAnimator="#null" in your style or directly on the Button.
Here is what that would look like on your button XML:
<Button android:id="#+id/sm_achievements_btn"
...
android:textAllCaps="false"
android:stateListAnimator="#null" />
Also, you're using the wrong parent for your button style. You should never set a theme as the parent for a widget style. Regardless, here is what that should look like if you prefer that route:
<Button android:id="#+id/sm_achievements_btn"
...
style="#style/MyButtonStyle" />
<style name="MyButtonStyle" parent="android:Widget.Material.Light.Button">
<item name="android:textAllCaps">false</item>
<item name="android:stateListAnimator">#null</item>
</style>