I'm trying to make some Seekbar for my app as a distance value. I had liked it to looks something like this (image was taken from - link):
Can someone please show a simple example of how to make something like this?
I know that I can use "already made" Seekbars from Github but I'm trying to understand the concept so I can further design it.
Thank you
With the Material Components Library version 1.2.0 provided by Google you can use the Slider component.
Just add in your layout:
<LinearLayout
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
...>
<com.google.android.material.slider.Slider
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:labelBehavior="withinBounds"
android:value="7"
android:valueFrom="0"
android:valueTo="10"
.../>
</LinearLayout>
You can customize the colors using these attrs:
<com.google.android.material.slider.Slider
app:activeTrackColor="#color/primaryDarkColor"
app:inactiveTrackColor="#color/primaryLightColor"
app:thumbColor="#color/primaryDarkColor"
.../>
or you can override the default colors using a custom style with the materialThemeOverlay attribute:
<com.google.android.material.slider.Slider
style="#style/Custom_Slider_Style"
with:
<style name="Custom_Slider_Style" parent="Widget.MaterialComponents.Slider">
<item name="materialThemeOverlay">#style/slider_overlay</item>
</style>
<style name="slider_overlay">
<item name="colorPrimary">#color/...</item>
<item name="colorOnPrimary">#color/...</item>
</style>
or use the android:theme attr in the layout:
<com.google.android.material.slider.Slider
android:theme="#style/slider_overlay"
../>
Example:
If you want to customize the tooltip shape you can use the labelStyle attribute:
<com.google.android.material.slider.Slider
app:labelStyle="#style/tooltip"
with:
<style name="tooltip" parent="Widget.MaterialComponents.Tooltip">
<item name="shapeAppearanceOverlay">#style/tooltipShOverylay</item>
<item name="backgroundTint">#color/....</item>
</style>
<style name="tooltipShOverylay">
<item name="cornerSize">50%</item>
</style>
Related
Here is the button :
<Button
android:id="#+id/btn_choose_photo"
style="#style/Widget.MaterialComponents.Button.TextButton.Dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="#drawable/ic_camera"
android:drawablePadding="8dp"
android:text="#string/image_picker_dialog_choose_image"
android:textAlignment="textStart" />
I am using material themes, so this will be inflated into a material button
drawableStart has no effect at all, however drawableEnd, bottom and top work just fine
When I make the button tag a text view, drawableStart works
It seems like a bug or maybe I am missing something ?
Edit: My app theme is as follows :
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!---colors-->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryVariant">#color/colorPrimaryVariant</item>
<item name="colorPrimaryDark">#color/colorPrimaryVariant</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="colorSecondaryVariant">#color/colorSecondaryVariant</item>
<item name="colorAccent">#color/colorSecondary</item>
<!--
<item name="android:colorBackground">#color/colorBackground</item>
-->
<!--components-->
<item name="textInputStyle">#style/text_input_layout_style</item>
<item name="bottomSheetDialogTheme">#style/bottom_sheet_dialog_theme</item>
<item name="spinnerStyle">#style/spinner_style</item>
<item name="android:spinnerStyle">#style/spinner_style</item>
<item name="android:toolbarStyle">#style/toolbar_style</item>
<item name="toolbarStyle">#style/toolbar_style</item>
<item name="actionOverflowButtonStyle">#style/overflow_button_style</item>
<item name="android:actionOverflowButtonStyle">#style/overflow_button_style</item>
</style>
You should use app:icon like this:
In the layout:
<Button
...
app:icon="#drawable/ic_camera"
style="#style/Widget.MaterialComponents.Button.TextButton"
/>
It is displayed at the start, before the text label. You can change icon gravity, tint or size.
For more information
Using
android:drawableLeft
will solve the problem, but will not give you RTL(Right to left) support. If your end user uses a different language which follow RTL, then your Button will not support it.
Are you managing the Button's property in the Activity or Fragment, as technically speaking,
android:drawableStart
should work.
You can change to "App compat button"
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/continuar_anonimo_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
app:backgroundTint="#color/greenPrimary"
android:background="#drawable/corner_boton_outline"
android:text="Continuar anonimamente"
android:paddingLeft="15dp"
android:drawableStart="#drawable/ic_google_color"
style="?android:textAppearanceSmall"/>
Use android:drawableLeft
android:drawableLeft="#drawable/ic_phone"
android:backgroundTint="#android:color/white"
I'm creating an Android app and I'm using the AndroidX libraries and Material design theme. My app theme on styles.xml is:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
I have the following FAB from a custom library:
<com.leinardi.android.speeddial.SpeedDialView
android:id="#+id/work_log_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:layout_behavior="#string/speeddial_scrolling_view_snackbar_behavior"
app:sdMainFabClosedSrc="#drawable/ic_add_white_24dp"
app:sdOverlayLayout="#id/overlay" />
And also tried the default FAB:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_add_white_24dp"
android:layout_margin="16dp" />
No mater the color of the icon (a vector drawable), the icon inside the FAB (from the library and from the default) is always black. I have narrowed down the problem to the material design theme, since using the old Theme.AppCompat.Light.DarkActionBar instead of the new Theme.MaterialComponents.Light.DarkActionBar the icon inside the FAB gets the color of the original vector drawable.
Does anyone know why this is happening and how to solve it?
I solved this by using:
app:tint="#color/COLOR_OF_ICON"
and NOT:
android:tint="#color/COLOR_OF_ICON"
Reference: https://github.com/material-components/material-components-android/blob/master/docs/components/FloatingActionButton.md
For MaterialComponents Theme you can define the following colors.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primaryDark</item>
<item name="colorPrimaryVariant">#color/primaryVariant</item>
<item name="colorOnPrimary">#color/onPrimary</item>
<item name="colorSecondary">#color/secondary</item>
<item name="colorSecondaryVariant">#color/secondaryVariant</item>
<item name="colorOnSecondary">#color/onSecondary</item>
<item name="colorAccent">#color/accent</item>
<item name="colorSurface">#color/surface</item>
<item name="colorOnSurface">#color/onSurface</item>
<item name="colorError">#color/error</item>
<item name="colorOnError">#color/onError</item>
<item name="colorButtonNormal">#color/buttonNormal</item>
<item name="colorControlNormal">#color/controlNormal</item>
<item name="colorControlActivated">#color/controlActivated</item>
<item name="colorControlHighlight">#color/controlHighlight</item>
<item name="colorBackgroundFloating">#color/backgroundFloating</item>
</style>
colorSecondary is the responsible color for FloatingActionButton.
and
colorOnSecondary is the responsible color for icon color of FloatingActionButton.
In your AppTheme you have not defied the colorSecondary. So, it took the default black color from parent Theme.MaterialComponents.Light.DarkActionBar.
Reference : Android dev summit, 2018 - The Components of Material Design
As said in this answer, if your icon has multiple colors or if you want to keep your icon original color(s), then assign #null as the tint:
app:tint="#null"
According to the GitHub documentation page for the Material Components library's FloatingActionButton, the only attributes that affect the icon are
app:srcCompat
app:tint
app:maxImageSize
In this case, since your color is defined as a constant (#FFF), the only one that seems to make sense is app:tint. Perhaps something in your theme has set this to black?
You ought to be able to override it by setting app:tint="#FFF" on your FAB.
Is Your Icon is multicolor
Add Only
app:tint="#null"
but if you only want to change color
#dimen/fab_margin -> 16dp
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
style="#style/Widget.MaterialComponents.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:backgroundTint="#color/colorAccent"
app:srcCompat="#drawable/ic_add"
app:tint="#color/colorWhite" />
Style :
<style name="AppTheme" parent="Theme.MaterialComponents.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>
Programmatically in kotlin :
fab.setImageDrawable(ContextCompat.getDrawable(context!!, R.drawable.your_drawable))
In my case, I just follow this
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/bottom_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_home"
android:scaleType="centerCrop"
android:src="#drawable/ic_add_sign"
app:backgroundTint="#color/app_dark_green"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal"
app:layout_anchor="#id/nav_view"
app:tint="#FFF" />
And my result is
use app:tint="#color/white"
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
app:tint="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="#drawable/ic_done"/>
I was looking for it and the following code worked.
android:backgroundTint="#color/primarycolor"
android:textColor="#color/white"
app:iconTint="#color/white"
Using vector asset as icon for FAB . Here is what worked for me .
app:tint is changing vector icon color and app:backgroundTint is changing background of FAB .
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/idFABAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:gravity="bottom"
android:layout_gravity="bottom|right"
android:layout_marginBottom="70dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="18dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="18dp"
app:srcCompat="#drawable/ic_baseline_home_24"
app:tint="#color/white"
app:backgroundTint="#color/purple_500"
android:contentDescription="TODO" />
I'm also using Heinardi speed dial library. According to the document about customizing items, you can define a theme for the component:
<style name="Theme.FAB">
<item name="colorPrimary">#color/Primary</item>
<item name="colorAccent">#color/Accent</item>
<item name="colorOnSecondary">#color/white</item>
</style>
Note that colorPrimary defines the color of extended buttons,
colorAccent the color of main button,
colorOnSecondary the color of icon.
Then you can add the theme to SpeedDialView:
<com.leinardi.android.speeddial.SpeedDialView
android:id="#+id/speedDial"
android:theme="#style/Theme.FAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:sdMainFabClosedSrc="#drawable/ic_add_white"
app:sdOverlayLayout="#id/speedDialOverlay" />
app:backgroundTint="#color/white"
app:fabSize="mini"
app:srcCompat="#drawable/ic_"
app:tint="#null"
Color of icons are overrided by colorOnSecondary (see themes.xml).
To put the icon colour you want you must use:
app:tint="#colour/icon_colour"
And for the background:
app:backgroundTint="#colour/icon_colour"
You can change these colours automatically by the theme using https://stackoverflow.com/a/24043959/1200914
Just want to add my 2 cents. There are a few options how to fix your color issue for Material Components. The majority of them are already covered in the answers.
However, there's another and, probably, more general approach in this case. You can set a default "floatingActionButtonStyle" to your theme which will be used for all FloatingActionButtons.
<style name="Theme.Design" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="floatingActionButtonStyle">#style/Theme.Design.FloatingActionButton</item>
</style>
<style name="Theme.Design.FloatingActionButton" parent="Widget.MaterialComponents.FloatingActionButton">
<item name="tint">#null</item>
</style>
More details about styling of FloatingActionButtons is available there Theming Fabs
UPDATED
Shortly:
#gab-ledesma comment did the trick:
https://stackoverflow.com/a/53843325/5451349
implementation 'com.google.android.material:material:1.2.0-alpha05'
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
...
app:iconTint="#color/white"
app:icon="#drawable/ic_add_white"
...
Finnaly app:iconTint changed an icon color.
I've found it in the source code:
https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/floatingactionbutton/res/values/styles.xml#L81
Of cource, you need to check the instruction before:
https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md
In order to use Chip and ChipGroup, I set Application style extends Theme.MaterialComponents.Light.NoActionBar int manifests.xml, then I set Button "android:background" attr, but it does not effect! why? and what can I do?
This is my style:
<style name="AppBaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/primary_material_light</item>
<item name="colorPrimaryDark">#color/header_color</item>
<item name="actionBarSize">48dp</item>
<item name="android:screenOrientation">portrait</item>
<!--<item name="buttonStyle">#style/AntButton</item>-->
<!--<item name="materialButtonStyle">#style/AntButton</item>-->
<!--<item name="android:button"></item>-->
<!--<item name="android:progressTint">#color/ffc000</item>-->
<item name="colorAccent">#color/ffc000</item>
</style>
<style name="AntButton" parent="android:Widget">
<item name="android:background">#drawable/abc_btn_default_mtrl_shape</item>
<item name="android:textAppearance">?android:attr/textAppearanceButton</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">88dip</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
<!--<item name="android:colorButtonNormal">#color/white</item>-->
</style>
I have tried to change buttonStyle and materialButtonStyle, but not effect too!
this is my layout XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/ll_popup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/white"
android:divider="#drawable/shape_divider_05dp"
android:orientation="vertical"
android:showDividers="beginning|middle">
<!--only can use backgroundTint to change background color-->
<Button
android:id="#+id/item_popupwindows_Photo"
android:layout_width="match_parent"
android:layout_height="55dp"
android:backgroundTint="#color/white"
android:text="Pictrue"
android:textColor="#color/c_666666"
android:textSize="16sp" />
<!--background not effect !!-->
<Button
android:id="#+id/item_popupwindows_cancel"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#color/white"
android:text="cancel"
android:textColor="#color/c_666666"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
this is result :
Because I have used Chip and ChipGroup in APP, I have to user theme extends MaterialComponents! do you know how to resolve it ?please tell me, thanks!
in this page Can't use android:background with button from the new material components, the author wants to change the default padding, but I want to change the backgroundDrawable, so, it does not work for me.
If you want a true Button, but one that you can modify like the framework Button (instead of the MaterialButton), then you can explicitly specify the framework version in your layout file. Replace this:
<Button
android:id="#+id/item_popupwindows_cancel"
... />
with this:
<android.widget.Button
android:id="#+id/item_popupwindows_cancel"
... />
This will give you what it says on the tin: an android.widget.Button, which should respond to styling the way you expect.
Similarly, you could use a <androidx.appcompat.widget.AppCompatButton> if you want support library features but not material components features.
In this particular case, the LinearLayout holding your second Button seems to have a white background color. That means you don't need to explicitly specify a white background for your Button; you can just let the LinearLayout's background show through.
This can be accomplished by using the "Text Button" style of MaterialButton:
style="#style/Widget.MaterialComponents.Button.TextButton"
Use this code for change the color
app:backgroundTint="#color/primaryColor"
You can also use ImageButton instead of Button from material components. android:background is working on ImageButton.
<ImageButton
android:id="#+id/item_popupwindows_cancel"
... />
I am trying to create a button whose text color is the colorAccent and whose background color is the default background color.
It looks like this:
I have tried default styles, but non of them have worked. Currently I have something like this but doesn't work:
styles.xml :
<style name="AppTheme.ButtonRaised" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#android:color/transparent</item>
<item name="android:textColor">#color/colorAccent</item>
</style>
And layout:
<Button
app:theme="#style/AppTheme.ButtonRaised"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="date" />
And I am getting something like this:
I am not getting any effect of style or theme here. I have also tried with style tag, but it doesn't work.
apply theme with a style like this
<style name="ThemeButtonBlue" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">#color/colorPrimary</item>
<item name="colorButtonNormal">#android:color/transparent</item>
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/colorPrimaryDark</item>
<item name="colorControlHighlight">#color/colorPrimary</item>
</style>
I am also struggling to do this effect. I could achieve something similar, but I don't know if its "material design approved".
I set the style to their button borderless colored and the elevation to 2dp because on material design they say that "Raised buttons have a default elevation of 2dp". But they don't give you xml layout examples which is annoying -_-
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
style="#style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:elevation="2dp"
android:text="Button"/>
Keep in mind that the elevation tag does not show a shadow pre Lollipop devices.
I have created several styles for font in my app.
How can I add these styles to views? - 1) Using style attribute 2) Using textAppearance.
To use style is not an option because views may have other attributes (margins, paddings, min width, etc - I cant specify 2 styles for a view), so I need to specify text-related attributes separately, but android:textColor doesnt work here:
styles.xml:
<style name="TextAppearance.baseText" parent="#android:style/TextAppearance">
<item name="android:textAppearance">?android:textAppearanceSmall</item>
<item name="android:typeface">sans</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textSize">15sp</item>
</style>
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="65dp"
android:orientation="horizontal" >
<Button
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sometext"
android:textAppearance="#style/TextAppearance.baseText"/>
</RelativeLayout>
Why doesnt it work? How can I specify textcolor in textappearance?
As Oderik has mentioned in the comments, the Button view has a default value for it's textColor attribute. This textColor value overrides whatever value is set through the textAppearance attribute. Therefore you have two options:
Set the textColor to #null in the style:
<style name="Application.Button">
<item name="android:textAppearance">#style/Application.Text.Medium.White</item>
<item name="android:textColor">#null</item>
</style>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Application.Button" />
Set the textColor to #null in the Button XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Application.Button"
android:textColor="#null" />
It works. Your text color is white - the same as default color. Put there any other color like <item name="android:textColor">#AA6699</item> and you will see difference.
Second thing is that you are trying to set text appearance in text appearance - doen's make sense. If you want to set small letters do this using parent parent="#android:style/TextAppearance.Small and delete line <item name="android:textAppearance">?android:textAppearanceSmall</item>
Third thing is that you want to have small letters and put additional textSize - doesn't make sense.
Try this, works for me:
<style name="BaseText" parent="#android:style/TextAppearance.Small">
<item name="android:typeface">sans</item>
<item name="android:textColor">#AA7755</item>
</style>
If you want to set everything in style:
<style name="BaseText" parent="#android:style/TextAppearance.Large">
<item name="android:typeface">sans</item>
<item name="android:textColor">#AA7755</item>
</style>
<style name="BaseText.Margins">
<item name="android:layout_margin">10dip</item>
</style>
Where BaseText.Margins inherits from BaseText.
Then you can just use:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
style="#style/BaseText.Margins" />
If you want to use android:TextAppearance instead of style: to set your android:textColor, you need to set android:textColor=#null on your TextView.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:textAppearance="#style/TextAppearance.AppCompat.Medium.MySubheader"
android:textColor="#null"
tools:text="Section" />
Then it will inherit correctly from your android:TextAppearance
<style name="TextAppearance.AppCompat.Medium.MySubheader">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/black_54</item>
</style>