Change the color of a switch - nativescript

I want to change the color of a toggled on switch on Android. I noticed that the switch has the color of the ns_accent and this color can't be changed because of other elements in my app.
Style.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="ns_accent">#00CCBB</color>
</resources>
In iOS i already solved the issue with this code:
switch.ios.tintColor = UIColor.colorWithRedGreenBlueAlpha(1.0, 1.0, 1.0, 0.5);
switch.ios.onTintColor = UIColor.colorWithRedGreenBlueAlpha(1.0, 1.0, 1.0, 0.5);
Also i noticed that i could parse an Android color with this code:
android.graphics.Color.parseColor("#B3FFFFFF");
Question: How can i change the switch-on color in Android?

You can do it programmatically like this:
var Color = colorModule.Color
var switchColor = new Color("#somecolor");
switch.android.getThumbDrawable().setColorFilter(switchColor.android, android.graphics.PorterDuff.Mode.MULTIPLY);
switch.android.getTrackDrawable().setColorFilter(switchColor.android, android.graphics.PorterDuff.Mode.MULTIPLY);
And yeah, you can replace switchColor.android by the code you have found android.graphics.Color.parseColor("#B3FFFFFF");

Related

How to Change Checkbox/Radibutton Color in Xamarin?

I have implemented Radio button and Checkbox control in my application using custom rendering. I have rendered the control by referring the below link. The controls default color is white. Since my application background color is white controls are not visible. I tried to change the color of the controls. But it is not working. Text color only changing. Can anyone suggest how to change the color of Checkbox/Radio button controls?
https://github.com/XLabs/Xamarin-Forms-Labs
Thanks in Advance.
You can set Xamarin.Forms control (like Switch) color from renderer with using below line code in OnElementChanged method:
Control.OnTintColor = Color.Blue.ToUIColor();
for your XLab control, you need to set background color in CheckBoxRenderer and change color in OnElementChanged method. or you can change from control also.
BackgroundColor = Element.BackgroundColor.ToUIColor();
For Android you can manage by material design like:
Create a new style.xml file with this (Change the color in colorAccent to whatever you like) and put it inside the Resources folder.
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- colorAccent is used as the default value for colorControlActivated which is used to tint widgets -->
<item name="colorAccent">#0000FF</item>
<!-- You can also set colorControlNormal, colorControlActivated colorControlHighlight and colorSwitchThumbNormal. -->
</style>
</resources>
Then add an attribute to your MainActivity.cs to load that theme.
[Activity(Label = "MyApp", Icon = "#drawable/icon", Theme = "#style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, WindowSoftInputMode = SoftInput.AdjustResize)]
public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity

iOS Statusbar text color when there is no Actionbar

I want to color my Statusbar text white. This is no problem when i have an Actionbar, but when i set the actionbar to hidden, the color is black again.
This is the code i used:
<page actionBarHidden="true" backgroundSpanUnderStatusBar="true"></page>
And this is the javascript code:
var navigationBar = frameModule.topmost().ios.controller.navigationBar;
navigationBar.barStyle = 1;
Question: How to color the statusbar when there is no actionbar?
You could find solution of this behavior in this GitHub issue:here

Centering Layout With Padding - rounding error

Nested layouts in xamarin seem to be biased to the left of the screen. I'm assuming this is a dp to pixel rounding bias or something?
Can anyone confirm, is there a work around or solution?
Although my example uses Absolute layout the problem seems to be on all layouts.
using System;
using Xamarin.Forms;
namespace XamarinTest
{
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
AbsoluteLayout child = layout;
AbsoluteLayout.SetLayoutFlags(child, AbsoluteLayoutFlags.All);
AbsoluteLayout.SetLayoutBounds(child, new Rectangle(0, 0, 1, 1));
Random rand = new Random();
for (int i =0;i<100; i++)
{
child = addLayout(child, rand) ;
}
AbsoluteLayout abs = new AbsoluteLayout();
AbsoluteLayout.SetLayoutFlags(abs, AbsoluteLayoutFlags.All);
AbsoluteLayout.SetLayoutBounds(abs, new Rectangle(0, 0, 0.5, 0.5));
abs.BackgroundColor = Color.Black;
layout.Children.Add(abs);
}
private AbsoluteLayout addLayout(AbsoluteLayout parent, Random rand)
{
AbsoluteLayout abs = new AbsoluteLayout();
AbsoluteLayout.SetLayoutFlags(abs, AbsoluteLayoutFlags.All);
AbsoluteLayout.SetLayoutBounds(abs, new Rectangle(0,0,1,1));
abs.Padding = new Thickness(2.0);
abs.BackgroundColor = new Color(rand.NextDouble(), rand.NextDouble(), rand.NextDouble());
parent.Children.Add(abs);
return abs;
}
}
}
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinTest.Page1">
<AbsoluteLayout BackgroundColor="White" x:Name="layout">
</AbsoluteLayout>
</ContentPage>
Screen shot (if it uploads):
I think you are seeing differences in the emulator's rendering vs. an issue with the Forms code running on Android as the DPI of the simulate screen can throw offsets into the mix. ​
iOS Simulator does a great job at rendering at a 1:1 based upon the actual devices' DPI and then down-sampling the display based upon your view settings and your code will always render those StackLayout at the requested position.
If you look at the your code running on two (OS-X-based) emulators using a DPI that divides "cleanly" to the pixels on the physical screen of the emulator:
Versus one that does not:
If you have the paid-version of GenyMotion's emulator, there is a one-2-one scaling option that provides a similar experience as the iOS simulator.
Moral of the story, use physical devices for final checks of your layouts. A great way is to use Xamarin's Test Cloud and add a screenshot capture to each of your app's Form pages

Transparent parts in Skia View

I'm trying to use SkiaSharp in Xamarin.forms to draw some polygons on top of an image.
The image is downloaded from a server and than cached in background. So I would prefer not to manipulate the image itself and instead draw a new canvas and place it in a new view on top of the image, like in the screenshots. (using relative layout)
As you can also see in the screenshots, placing the rectangular is not the problem but the "transparent" part is not really transparent.
Code of the first screenshot:
using (var paint = new SKPaint ()) {
paint.IsAntialias = true;
using (var path = new SKPath ()) {
path.MoveTo (0f, 0f);
path.LineTo (width, height);
path.LineTo (0, width);
path.Close ();
paint.Color = SKColors.Orange;
canvas.DrawPath (path, paint);
}
}
In the second I tried to use
canvas.Clear(SKColors.Transparent);
but it only changes to the black background.
Does anyone know whether it is possible to have completely transparent parts in a skia view?
Your question helped me solve this today:
As well as setting the canvas' colour as transparent, the containing Xamarin Forms element needs to be transparent too!
C#
canvas.Clear(SKColors.Transparent);
XAML
<views:SKCanvasView ... BackgroundColor="Transparent" />
guessing here cause you forgot the screenshots,
what you probably want to do is using clipping
something like :
canvas.Clear();
canvas.ClipPath(path);
canvas.ResetMatrix();
canvas.DrawBitmap(...)

Appcelerator titanium, how can I create a modal window?

I am new to appcelerator titanium and have a question
how can I create a modal window that blurs its parent, or have a semi transparent background?, I managed to create a modal window but the parent went black.
thanks in advance
This is the current way to accomplish this in Titanium as of 3.1.3 on iOS.
First, make a new window.
var myModal = Ti.UI.createWindow({
title : 'My Modal',
backgroundColor : 'transparent'
});
Then create a wrapper view, a background view, and a container view:
var wrapperView = Ti.UI.createView(); // Full screen
var backgroundView = Ti.UI.createView({ // Also full screen
backgroundColor : '#000',
opacity : 0.5
});
var containerView = Ti.UI.createView({ // Set height appropriately
height : 300,
backgroundColor : '#FFF'
});
var someLabel = Ti.UI.createLabel({
title : 'Here is your modal',
top : 40
});
var closeButton = Ti.UI.createButton({
title : 'Close',
bottom : 40
});
closeButton.addEventListener('click', function () {
myModal.close();
});
Now build your UI stack. The order is important to avoid having to set z-index.
containerView.add(someLabel);
containerView.add(closeButton);
wrapperView.add(backgroundView);
wrapperView.add(containerView);
myModal.add(wrapperView);
Now you can open your modal, but to NOT set modal : true
myModal.open({
animate : true
});
Its very simple.Just create the window, and when you're opening it,specify the 'modal' property as true!
var ModalWindow = Ti.UI.createWindow({});
ModalWindow.open({modal:true});
In Titanium Appcelerator (tried in 1.6.2) a modal window is always a full screen window. The parent might seem black because this modal window's background is black.
Try specifying a semi-transparent image as the background of this modal window, you are creating and you might get the effect you want from it.
You could try using the opacity on the window you have that is overlaying the other.
Ti.UI.currentWindow.opacity = 0.4;
If you are on iPhone, probably you cannot make it. on iPhone, if a modal dialog appears, the other window on the render stack will be cleared. That is, only ONE modal dialog in the render stack. That's the reason you got black if other areas of the parent are not covered by your modal window. iPad implemented modal dialog using "sheet" style, so you can make the are areas semi transparent.
I like the solution presented by AlienWebguy, although I think there's a minor bug. When you create your label, I think you meant to set the text property and not the title property:
var someLabel = Ti.UI.createLabel({
text: 'Here is your modal',
/* etc., etc. */
});
When I used title, it (the label) was not appearing in the window.
Another modification I might make is to set the layout property for the container view, e.g.,
var containerView = Ti.UI.createView({
height: 100, /* or whatever is appropriate */
backgroundColor : '#FFF',
layout: 'vertical'
});
In doing this, you can "stack" up the GUI elements in that view and not worry (too much) about setting layout coordinates... At least that's what I do in creating a customized alert box using the techniques outlined here.
i was also looking for such window with semi transparent background for ios 8.4. I tried the way "AlienWebguy" in Alloy XMl but the issue was whole window getting opacity 0.5 and background stacked window content are clearly visible than foreground view content. i have did some changes to the "AlienWebguy" to get the required result:
<Alloy>
<Window backgroundColor="transparent" modal="false">
<View layout="vertical" width="Ti.UI.FILL" height="Ti.UI.FILL" backgroundColor="#000" opacity="0.5">
// View will fill whole window with transparent shade of black color.
</View>
<View class="container" zIndex="100" height="400" width="Ti.UI.FILL" backgroundColor="#fff">
// You can add any content here, which will be look like Modal window.
//View automatically vertically centered on screen.
</View>
</Window>
</Alloy>
Hope this will save the time of developer doing in Alloy. Thanks for "AlienWebguy" for the concept.
why not just give it transparent background?
<Window backgroundColor="#80000000">
<View class="container">
// Your views
</View>
</Window>

Resources