How do I change the colours of the Switch component? - codenameone

I want to change the colour of the thumb of the Switch component to green instead of the standard black.
According to the documentation "You can customize the look and feel of a switch using styles, either directly in the theme.res file or in CSS." and "The thumb will be rendered using Switch's Style.getFgColor(). It will use the selected style, when in the "on" position, the unselected style when in the "off" position,"
https://www.codenameone.com/javadoc/com/codename1/components/Switch.html
I have tried adding a "Switch" style to the theme.res file. In the theme preview I see my changes. When I run the app in the simulator, there is no change.
I also tried changing the style in code:
swPrdp.setUIID("Switch");
I still get the standard black thumb.
I also tried:
swPrdp.getAllStyles().setBgColor(0xcccccc, true);
swPrdp.getSelectedStyle().setFgColor( 0x59925A, true);
with and without the second argument (true). Still no change.
I am trying to keep the app size to a minimum, so I don't want to go the route of specifying images.
Where am I going wrong?

This should work in the theme too and works for me in the style object (which is easier to write here:
Form hi = new Form("Switch", BoxLayout.y());
Switch s = new Switch();
s.getAllStyles().setFgColor(0xff0000);
s.getAllStyles().setBgColor(0xff);
hi.add(s);
hi.show();

Related

How to use the dark mode and selection modifier together in TailwindCSS?

I'm attempting to set the text highlight colors in my project that uses TailwindCSS. This can be done fairly easily using the "selection" modifier that Tailwind provides. This works fine except when I try to set different colors for dark mode. I can't figure out what I'm doing wrong but I can't find anything online regarding how to correctly use the "dark" and "selection" modifiers in conjunction.
I tried the following syntax in attempt to get it to work correctly:
<html className="selection:bg-blue selection:text-purple dark:selection:bg-pink dark:selection:text-blue">
However, this only picks up the selection:bg-blue selection:text-purple styles and not the dark mode selection styles (dark:selection:bg-pink dark:selection:text-blue) when dark mode is activated.
Any thoughts on what I'm doing wrong? Thanks in advance.
Edit:
The colors do exist in my tailwind config:
I figured this out. I wasn't thinking and realized what I was doing wrong was putting the selection classes on the HTML tag. The HTML tag is where the dark mode class gets applied and due to this, the selection class wasn't a descendant of the .dark mode class. To fix it, I moved the selection classes to the body tag.

Getting Tab Component To Consume UIID

I have tried everything possible to style my Tab component but its not getting reflected. I want my selected Tab to have a white background and red foreground. Even though I have edited Style using Theme designer. Its still doesnt work. I read the docs and I am aware Tab component is now like a toggle. I did the suggestion to setTabUIID(null) but still cant get it to work. How can this be done.
You don't need the setTabUIID(null); just make sure to style both selected and unselected styles. Also override the Border to Empty if you don't define a border.

Trouble changing background color of TextField in Codename One

I just started building my first App with Codename One and I'm having quite some trouble. :-)
I cant change the background color of a TextField, I changed everything in the Theme and named it "TextField". At the Color tab I changed the background color and set Transparency to 255. Since the Component is named TextField, shouldnt the style be applied automatically when I make a new TextField?
I also tried to set the UIID manually to "TextField", which didn't help either.
Am I missing something?
Kind regards,
Max
The way backgrounds work in Codename One is this:
Border is applied first
Then image
Then gradient
Then color
TextField and other components such as Button etc. often have a default border derived from the theme.
Switch to the Border tab, click derive then click the ... button and select empty.

Scrollbars recently appeared

I'm revisiting some older projects and a new build with the latest library has added an ugly scroll bar to my scrolling containers and lists.
I put in theme entries for 'Scroll' and "ScrollThumb" with transparency 0 and border empty. It removed some but not all, ticking or unticking scroll visible in the Designer doesn't seem to have much effect.
This happened sometime in the last few weeks. I can't find a pattern of what's causing it. (I think it may be new in v3.5).
Here is my trick for removing scrollbar everywhere:
UIManager.getInstance().setLookAndFeel(new DefaultLookAndFeel(UIManager.getInstance()) {
#Override
public void bind(Component cmp) {
if (cmp instanceof Container) {
cmp.setScrollVisible(false);
}
}
});
I placed that piece of code in the init method of my main class.
Edit (May 09 2019):
You may experience some weird UI behavior using the code above.
My experience so far is RadioButton and Checkbox components not rendering properly when I change their images with the code below:
((DefaultLookAndFeel) UIManager.getInstance().getLookAndFeel()).setCheckBoxImages(checkedImage, unCheckedImage);
((DefaultLookAndFeel) UIManager.getInstance().getLookAndFeel()).setCheckBoxFocusImages(checkedImage, unCheckedImage, checkedImage, unCheckedImage);
Solution (May 09 2019):
Add the following to the init method of your main class:
UIManager.getInstance().getLookAndFeel().setFocusScrolling(false);
UIManager.getInstance().getLookAndFeel().setFadeScrollBar(false);
...and set a theme constant scrollVisibleBool to false. (I think CN1 defaults this to false but I still set it anyway).
In Codename one scrollbar coming back Shai indicates that the preferred method for removing scrollbars is to define the theme constant scrollVisibleBool=false.
The Theme editor would not let me add this constant to the theme as it was not in the drop-down selector. In order to add it I had to do the following:
Put the theme editor in XML Team Mode, save the theme, close the editor.
Add the following line to the theme.xml file <val key="#tabPlacementInt" value="0" />
Then open the theme editor and save it again.
After this, the scrollbars are no longer visible.

Slider in codenameone

Can any one please tell me how i can add slider and moving form in codename one (with sample lines of code) and also want to know are these features supported by all types of devices?
Regards,
Megha
I think you mean how to animate forms changes
Form.setTransitionInAnimator(CommonTransitions.somthing)
Form.setTransitionOutAnimator(CommonTransitions.somthing)
Next, you should handle some "finger slide" event.
To add a slider you can use the following code
Slider jSlider = new Slider();
jSlider.setMaxValue(255);
jSlider.setMinValue(0);
jSlider.setProgress(50); // Set the starting value
jSlider.setEditable(true); // To it works as a slider instead of a progress bar
Now you have created a slider which you can add to your component like you would in Swing. You can type 'jSlider.' in eclipse to find out which other methods you can use, or you can go to the API: http://codenameone.googlecode.com/svn/trunk/CodenameOne/javadoc/com/codename1/ui/Slider.html
I think min/maxValue are selfexplenatory though :)
If you want to open a new form, simply create a new class extending form or do it in code like
Form form = new Form();
form.animate(); // To make it "slide in"
form.show();
Also noteworthy, the slider doesn't work with the lumia skin per default, though you can make it work. I actually asked this question on here as well:
Slider doesn't draw (CodeName One) with Windows phone skin

Resources