As seen above when the switch toggle in iOS is disabled and toggled has color, but in the android doesnt.
Is possible to do the same effect in android?
If you just want to change the thumb color to match the iOS UISwitch track green color (#41D150), you can change the colorSwitchThumbNormal in the Resources/values/style.xml file (in your Xamarin.Android project):
Add:
<item name="colorSwitchThumbNormal">#41D150</item>
Forms running on API 19 - API 26:
when the switch is disabled and is toggled, I want less opacity
If you want to change the track color, you can use a very simple renderer that changes all the Forms' Switch on Android:
[assembly: ExportRenderer(typeof(Switch), typeof(StyleBasedSwitchRenderer))]
namespace Forms_PCL_XAML.Droid
{
public class StyleBasedSwitchRenderer : SwitchRenderer
{
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Switch> e)
{
base.OnElementChanged(e);
Control?.SetTrackResource(Resource.Drawable.form_switch_track_mtrl_alpha);
}
}
}
The Resource.Drawable.form_switch_track_mtrl_alpha is a 9Patch image. the normal theme uses abc_switch_track_mtrl_alpha.9.png in the AppCompat library.
So if you add a similar 9Patch but change the color:
You end up with:
Or use a drawable-based selector and two 9Patch images to maintain the same default colors of the thumbs:
Renderer becomes:
Control?.SetTrackResource(Resource.Drawable.form_switch_selector);
Selector (form_switch_selector.xml) in the Resource.Drawable folder:
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/form_switch_track_selected" />
<item android:drawable="#drawable/form_switch_track_mtrl_alpha" />
</selector>
The two 9Patch files:
Results in:
If you want to change the default colours for the Switch control in Xamarin forms, you could use our Effect that we created in the Forms Community Toolkit...
The actual code can be use too if you want, it's available here
https://github.com/FormsCommunityToolkit/FormsCommunityToolkit/blob/dev/src/Effects/Effects.Android/Effects/Switch/SwitchChangeColor.cs
But best is to use it as done in our Samples app https://github.com/FormsCommunityToolkit/FormsCommunityToolkit/blob/dev/Samples/Samples/Samples/Views/EffectsSwitchChangeColorPage.xaml#L16
So what it does under the covers is hook into the actual Android native control and change the colours of it with the ones you provide in the XAML.
Native code for setting the colour is like this:
((SwitchCompat)Control).ThumbDrawable.SetColorFilter(...
Related
I'm currently trying to figure out how to change the colour of the bottom border of a text input from the default blue. I've tried using the border-color, color and background-color properties but none seem to have an effect on the input. This is the XML code I am using for the input <TextField text="{{ username }}" cssClass="username" android:row="1"/>.
To piggyback on #Bradley-Gore answer. Here is the Android documentation which highlights what colors need to be set Here is another good link from the Android documentation on compatibility with styles/themes
The important piece you are looking for is:
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:colorAccent">#color/accent</item>
With NativeScript version 1.6+ you need to create the files in App_Resources/Android/, pre NativeScript pre 1.6 you had to make these changes in platforms/android/ the reason for the change was to persist these types of changes when you need to remove the platform and add it back to fix any build cache/gradle issues, etc.
So to simply set the primary, primaryDark, and accent color create the values and values-v21 folders located in App_Resources/Android
App_Resources/Android/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ns_primary">#3489db</color>
<color name="ns_primaryDark">#336699</color>
<color name="ns_accent">#ff4081</color>
</resources>
App_Resources/Android/values-v21/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ns_accent">#ff4081</color>
</resources>
If you actually just create a NativeScript project and build the android app, navigate to platforms/android you'll see that the CLI generated these files, it's how NativeScript styles the apps by default and that's why it uses the prefix ns_ before the primary, primaryDark, accent values.
Something else that might help you as you begin to learn Android and NativeScript is the attached .gif. This is from the Android documentation, most of the styling can be done programmatically or by setting styles via the .xml files #bradley-gore mentioned and what I have listed here for colors, but it's best to keep items separate :) In the .gif I just highlight the android.widget.TextView class and scroll down to find the XML attributes that you can set if you were building a native Android UI via XML. These are what you can set via the styles, and you'll see the code methods next to the xml attributes if there is a matching method. Hope this explains everything and provides a good learning experience for those getting into NativeScript. I might blog this :) good question.
For android, if you want to use the TextInputLayout (fancy floating label, error message, etc...) instead, I have a plugin for that: https://www.npmjs.com/package/nativescript-textinputlayout
Even if you do not wish to use that plugin, or cannot due to my not having gotten iOS support in there yet :p, then the same style mechanisms will help. In the plugin's demo app I'm using an App_Resource for android that defines some style rules for things that I don't think can be defined in NativeScript:
<style name="DemoAppTheme" parent="AppTheme">
<item name="colorControlNormal">#color/darkBlue</item>
<item name="colorControlActivated">#color/medBlue</item>
<item name="colorControlHighlight">#color/lightBlue</item>
<item name="android:textColorPrimary">#color/darkBlue</item>
<item name="android:textColorHint">#color/medBlue</item>
</style>
Specifically those first two items are going to help you, as it covers the standard colors for controls in normal and activated states. I'm not 100% on the difference between hightlight and activated states at the moment. Take a look at how the demo app for my plugin wires these up and it should get you there. You want to look at app/App_Resources/Android/values/(appColors | appStyles).xml and app/App_Resources/Android/AndroidManifest.xml
My understanding is that 4.4 changed some of the highlight colors for buttons to be grey or more neutral rather than blue. In my app I have some buttons which are a custom drawable - just a shape with rounded corners and then a selector for all the states. Now in my app so far I have just hard coded actual colors in this selector which are the same as from the default Holo theme on 4.0-4.3. However, with this recent change I want the pressed state of these buttons to be default (grey) when ran on 4.4+ devices. This way they will match the action bar highlights etc.
So far I have tried to use attribute 'colorPressedHighlight' in my selector but this doesn't work (I don't fully understand attributes/styles to be honest). The app wont compile, seemingly you can't refer to an attribute in place of a color.
Is there a way to do this? I'm now thinking maybe a different selector for each API version and just hard code the values in each? From a brief look I think this is possible, seems very inelegant though. This really shouldn't be as hard as it seems, right?
Any help much appreciated.
res / values-v19
Create value folder for API level 19 i.e for android 4.4 and above.
res/values-v19/colors.xml
define custom color value for button selection color
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color
name="button_selection"
>hex_color(Gray)</color>
</resources>
Create value folder for API level below 19 i.e for below android 4.4
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color
name="button_selection"
>hex_color(blue)</color>
</resources>
Then use this value in your custom drawable.
Is there any method to set the default style for group of control of android like if i set color for checkboxes and also for edittext then it applies like in css
In my app i'm enabling and disabling some control conditionally so i want to change their color can i create a style that change color for all control without applying to each and specific.
I am not aware of such thing in Android but you can create a custom background like this:
<?xml version="1.0" encoding="utf-8"?>
<gradient android:angle="1dp"
android:centerColor="#color/sky_blue"
android:endColor="#color/white"
android:startColor="#color/white" />
Declare this file as say backgrnd.xml in drawable folder and when you want to give background to any view just use: android:background="#drawable/backgrnd"
Each view will then have same background like this.
Have a look at the class ColorStateList
I've seen there are many different "themes" for the Android, depending on the device. For example on my HTC WildFire the highlight color is a "lime green", and that of the emulator is orange.
Is there anyway to know what are the main interface colors of the device in which my app is running?
So i can set (for example) TextViews background colors to match the device theme.
EDIT: You told me this is not possible so...
Is there any way to draw a simple rectangle with the highlight color? Maybe a void button?
Thanks!!
It's not actually a specified color, the drawables themselves are actually replaced on these modified Android skins that HTC, Samsung, etc. put out. So programmatically, there's no direct way to know what the color scheme will be. Your best bet would be to simply define your own drawables for your widgets with your own color scheme, or even reuse the defaults from stock Android, but copy them to your app's drawable folder, and set them into a StateListDrawable, and apply these to your widgets. This will ensure that you get the same color style on all platforms, with the disadvantage being that your app will not match the scheme of the rest of the skin. Depending on your app's layout, that will likely not be a problem, though.
I don't think that there is a way for you to get what the default highlight color is but you can certainly set your own by using the "textColorHighlight=#aarrggbb" attribute on your TextView within the layout.
As far as I know, there isn't a way (I don't see how there could be really). I guess you could have a few different "themes" in your own app and let the user pick one, to easily atleast somewhat match the rest of their setup.
You can use the #android:drawable/list_selector_background (or code form: android.R.drawable.list_selector_background) on your views. It has each of the default selector states. You can also create a selector yourself in the drawable folder like this (named something like default_highlight.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:drawable/list_selector_background" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#android:drawable/list_selector_background" /> <!-- focused -->
<item android:drawable="#android:drawable/list_selector_background" /> <!-- default -->
</selector>
And change any of those that you might want to customize. Then you use it like any drawable by setting the background of a View to that (default_highlight or whatever you call it).
I'm trying to detect the focus/pressed color for button and other elements.
This is needed because I'm developing new components and it's important that those look as part of platform.
Those colors are ORANGE on android sdk and GREEN on HTC SenseUI.
If I could detect that color my component will look as part of platform on both version.
Does anyone knows how to do this?
It's possible to create "selector" which uses custom image for default state and platform default for focus/selection.
To do this follow the steps:
1) create xml file with selector in "res/drawable" (e.g. "red_button.xml"):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:drawable/btn_default" >
</item>
<item android:state_focused="true"
android:drawable="#android:drawable/btn_default" >
</item>
<item
android:drawable="#drawable/btn_default_red" >
</item>
</selector>
2) from folder ".../android-sdk-mac/platforms/android-1.5/data/res/drawable/" take picture "btn_default_pressed.9.png" and change color as you like (I needed to change it to red and for this GIMP is enough).
3) place altered picture in "res/drawable" (e.g. with name "btn_default_red.9.png")
4) define button:
<Button
android:id="#+id/info_button"
android:layout_width="wrap_content"
android:layout_height="37dip"
android:layout_marginTop="1dip"
android:background="#drawable/red_button"
android:text="[Info]" />
That's all.
This is result:
alt text http://img200.imageshack.us/img200/1349/custombutton.png
I had this problem too. As already stated, the problem is that the backgrounds aren't simple colors, they're Drawables that could take on all kinds of appearances. However, I found a work-around that may help. If your custom component looks something like an existing one, e.g. a Button or ListView entry, you can just steal their background/selector and set that as the background for your custom component. E.g., in your custom component constructor:
setBackgroundDrawable(new Button(context).getBackground());
or for a background more suitable for list-like components:
setBackgroundDrawable(new ListView(context).getSelector());
You may want to optimise that code somewhat, but you get the idea.
Those aren't colors. They are a few nine-patch images out of a StateListDrawable. I am skeptical that there will be a reliable way for you to determine what the color is, one that will work across all devices and all versions of Android.
This is pretty much a duplicate of: Android ListView Selector Color
Also, why do you need to detect the colours? Just do nothing and your widgets will fit in to the platform's existing look & feel.
Or if you're writing a custom theme, just make yours inherit from android:Theme.