Getting user input java coding

Hello all! So i started working in Kodular Extension IDE yesterday and have been working non stop to learn as much as possible. Ive also taken 5 courses on the basics to java so I have a little bit of knowledge to get me started. Ive already created a basic extension and it worked fine. Im trying to push my skills a little more but need some advice. My goal is to create a a Dark Theme, Light Theme extension. Heres my code posted below.

~ CODE ~
package io.makeroid.jc1bryan_82242.Confidential;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;

/import java.util.Scanner;/

@DesignerComponent(
version = 1,
description = “Extension description coming soon!”,
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = “https://ide.kodular.io/assets/logo.png”)

@SimpleObject(external = true)
public final class Confidential extends AndroidNonvisibleComponent {

/**

  • Creates a new component
    */
    public Confidential(ComponentContainer container) {
    super(container.$form());
    }

private String DarkColor, LightColor;
DarkColor = “Black”;
LightColor = “White”;

@SimpleFunction(description = “Block for Light Theme”)
public String SetLightTheme(String LightTheme) {
LightColor=LightTheme;
String res = LightColor;
GotValue(res);
}

@SimpleEvent(description="")
public void GotValue(String result){
EventDispatcher.dispatchEvent(this, “LightTheme”, result);
}

@SimpleFunction(description = “Block Description Coming Soon”)
public void DarkTheme() {
EventDispatcher.dispatchEvent(this, “DarkTheme”);
}

@SimpleFunction(description = “Block Description Coming Soon”)
public void Settings() {
EventDispatcher.dispatchEvent(this, “Settings”);
}

@SimpleFunction(description = “Block Description Coming Soon”)
public void Reset() {
EventDispatcher.dispatchEvent(this, “Reset”);
}

/* Where do I put this?? */
If (LightColor == “White”) {
System.out.println(“Hi”)
}
}

Ive gotten pretty far which is good. Basically when the user presses a button they can choose to flip between dark mode and light mode. I cant figure out how to make the input for the themes color only (not text or number). Shouldnt be hard if i take more time. Also If the user chooses light theme the background would change and vise versa for dark theme. Ive gotten pretty far on my own just need a push.

Any help is great! Advice, encouragement, suggestions is greatly appreciated. 1 thing though – Dont give me the whole code. I want to really learn on my own. Again just small pieces im missing. Thanks everyone.

First of all, when dispatching a @SimpleEvent you use EventDispatcher.dispatchEvent(this, "funtionName", params).

You’re also using Event Dispatcher to dispatch a @SimpleFunction, now wonder why you’re probably getting errors.

1 Like
  1. So for the first quote that you highlighted “LightTheme”, I have the EventDispatcher.dispatchEvent(this, "funtionName", params) but are you saying that the function that I used was wrong? If yes, the function name should be changed to “SetLightTheme”. But I believe your saying get rid of this line.

  2. For the second quote you highlighted “DarkTheme”, I should remove the
    EventDispatcher.dispatchEvent(this, “DarkTheme”);.

  3. How about the color question – "Cant figure out how to make the input for the themes color only (not text or number). Do I need a library for this or its something pretty simple? And the If Then Statements are just like how you would code it in java right?

~ CODE ~

import java.util.Scanner;
Scanner keyboardInput = new Scanner(System.in);

System.out.print("Enter username: ");
String username = keyboardInput.nextLine(); // .nextDouble(), .nextInt()
System.out.println("Hello, " + username);

I greatly appreciate your help. Im learning from my mistakes so thank you :pray:

Looking at your code I can see that you are using wrong annotation in this portion.

The annotation should be @SimpleEvent instead of @SimpleFunction. Also you are not calling the event from anywhere in your code which makes it confusing what are you trying to do.

Thanks for the help and information. With that advice I decided to narrow down at what I was looking at and started to understand some of the errors i was having. The code posted below is what Im working on now to figure out.

~ CODE ~

package io.makeroid.jc1bryan_82242.Confidential;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
/import java.util.Scanner;/

@DesignerComponent(
version = 1,
description = “Extension description coming soon!”,
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = “https://ide.kodular.io/assets/logo.png”)

@SimpleObject(external = true)
public final class Confidential extends AndroidNonvisibleComponent {

  /*private String DarkColor, LightColor;*/

private String DarkColor = “Black”;
private String LightColor = “White”;

public Confidential(ComponentContainer container){
super(container.$form());
}
/* Purple Block /
@SimpleFunction(description = “Block for Light Theme”)
public String SetLightTheme(String LightTheme){
LightColor=LightTheme;
String res = LightColor;
GotValue(res);
}
/
Yellow Block */
@SimpleEvent(description = “”)
public void GotValue(String result){
EventDispatcher.dispatchEvent(this, “GotValue”, result);
}
}

So what im trying to achieve here is when the user clicks the button, the background of the screen will change color. For example ----

When Button1.Click
do call ApplicationTheme.Set Light Theme to Grey

In the above example, the port coming out of the purple function block (Set Light Theme) the user can chose a color block under the “Palette” section. So black, white, yellow, green whatever they chose and the screen background would change.

So to answer part of what your saying so you know im understanding what you said, the @SimpleEvent would be the Button1.Click and so based on that click the function (action) would perform which is setting the screen color.

I’m not with my pc right now. I will paste a demo code as soon I go to home.

1 Like

Sir I greatly appreciate your time. Thank you taking the time to help. :pray::pray: