How to make checkbox/slider persistent? - java

Till now I've created apps that stored mainly objects ecc in an SQLite database.
But how would I make a checkbox/slider persistent?
Example:
The user goes in the settings and checks "Use Dark mode"
How would I make it that the checkbox remains checked after closing the app, so the user wouldn't have to check the dark mode everytime he opens my app? SharedPrefernces?

As #AndyRes said, you can store simple data like this in shared preferences. This is a simple key value store, ie you store values against a key and can fetch the stored value by providing the key.
Eg
getSharedPreferences().edit().putBoolean("my_key", checkBox.isChecked()).apply;
Will store the value of the checkbox into a key "my_key". You should change this to something meaningful
You want to put this inside a listener so you save it every time the check state changed either using checkBox.setOnCheckedChangeListener or onClick of a save button.
When you load the activity you want to retrieve the state and update the checkbox to match ie
//the second parameter false is the default value to be used if the key doesn't exist in the store
boolean checked = getSharedPreferences().getBoolean("my_key", false);
checkBox.setChecked(checked)
Note that i'm using the function getSharedPreferences() which is a function of Context and can be used in an activity

Related

Create a hyperlink that will expire after one click

I am creating an hyperlink that will be send to user mail(gmail in my case). I want to expire the link after user once clicked in it(I don't want the user to click more than once).Once user clicked on the link it will trigger subsequent mail to manager. Here I also want similar functionality.
Note: link has some encrypted data too.It should remain intact.
I solved the issue.Instead of relying on session.I am using last update value in DB to check the life of hyperlink.

Codename One: How to display the contents of entire properties page

I have a properties page that looks like this
property1=value1
property2=value2
property3=value3
property4=value4
And my plan is to be able to create a page in my app that will display these values and allow the user to change and save them.
The way that I thought I would do this is to create a method that will iterate through the file and populate a text control with the data.
I am using the 'propertyNames' function explained here https://www.codenameone.com/javadoc/com/codename1/io/Properties.html
Unfortunately when I use this, all that is listed are the properties and not the values
Is there a way to show the entire contents of the properties file?
The other way I was thinking about doing it is to create a list of buttons that correspond to the keys - so one button for each key [with the key name as the button label] - and then when the user clicks on the button it displays the key value in an editable text box, and a save button that writes it back to the file.
Is this possible?
Thanks
propertyNames gives you all the keys, then you can retrieve the value of each get key using getProperty:
for(String key:props.propertyNames()){
String value = props.getProperty(key);
output += key+"="+value+"<br/>"; //however you output your stuff
}
After letting the user set new values, you can set them using setProperty(key, newValue).

How can I allow the user to set the background color?

I have created a simple app that shows the user random quotes. I would like to allow the user to change the background color of the app.
How would I go about doing this? I have created a menu, with a settings activity. What would I need to do to allow them to set the color?
I read about using SharedPreferences, but can't find anywhere that explains it to my level of understanding.
Would it be possible to do something like this:
android:background="selectedColor">
Then get user input and set:
selectedColor = "#color/" + "userInput"
Please ELI5, this is the first time I've tried building an app, so I'm going to try and slowly add different 'features'. It's more of a learning thing, than to actually create a useful app too!
You need to get the View in which you would like change the color by View v = findViewById(R.id.yourViewId) in the Activity, and then call on this View v.setBackgroundColor(int color);
You can cast the View to proper type to get possibility of using other methods.
Store in SharedPreferences a value of the hex color you want. And always use the value associated a certain key as the background, since preferences can have a "default value" when reading them
Somethis along these lines:
//Reading
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.getString("colorbg","#FFFFFF");//#FFFFFF would be the default one
.
//Writing
prefs.edit().putString("colorbg","#FF0000").commit(); //Next read would be #FF0000

Retrieving current user entered value from GWT SuggestBox

I'm new to GWT. I have a simple SuggestBox which is populated using a MultiWordSuggestOracle. Users input their data to this SuggestBox, and if they find any match with the existing Suggestions its well and good. I'm able to retrieve this value in the SelectionHandler code as below.
display.getSuggestBox().addSelectionHandler(new SelectionHandler<Suggestion>() {
public void onSelection(SelectionEvent<Suggestion> event) {
String selectedProperty = ((SuggestBox)event.getSource()).getValue();
// do something with the property value
}
});
But users are allowed to enter values which are not already in the Suggestion oracle, in which case I should read this value and do something with this,may be saving to db as a new data.(The thing which I'm looking for is something like a browsers navigation widget where we show suggestions, users can pick up any suggestion or he can type in his new entry and carry on.) What I needed is a way to retrieve this new text user has entered? Data will be read on a button click. What I tried out is this.
display.getSaveBtn().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String selectedProperty = display.getSuggestBox().getValue();
//String selectedProperty2 = display.getSuggestBox().getText();
// Blank in both cases :(
// tried display.getSuggestBox().getTextBox().getValue(),but blank again
}
});
I tried to employ onChange() event handlers (as shown below)
display.getSuggestBox().addValueChangeHandler(new ValueChangeHandler<String>() {
public void onValueChange(ValueChangeEvent<String> event) {
String selectedProperty = ((SuggestBox)event.getSource()).getValue();
Window.alert("on change -- "+selectedProperty);
}
});
This is working fine except one scenario. Suppose there are two suggestions in the oracle,say 'createTicketWsdl' and 'createTicketTimeout'. When the user types in 'cr', he is opted with these two options, and if he selects 'createTicketWsdl' by pressing keyboard ENTER, then my alert is printing 'createTicketWsdl' which is correct. But if he selects 'createTicketWsdl' using mouse, then my alert is printing 'cr' (I tried to post the screenshot to give a better understanding, but being a new user I'm not allowed).(which I wanted to get as 'createTicketWsdl'since thats what he has selected). Soon after printing my alert, the value in the SuggestBox changes to 'createTicketWsdl'.
Is there a way to retrieve the value of the suggest box? I saw a similiar thread GWT SuggestBox + ListBox Widget, where some source code for a custom widget is available. But I didn't take the pain of trying out that, since what I want is simply get the current value from the SuggestBox and I hope there should be some easy way.
Thanks for all your help!
Your question is not very clear. You need to clarify your language a lil' bit. For example - is the following a question or an assertion? I mean, it sounds like an assertion but it has a question mark.
What I needed is a way to retrieve this new text user has entered?
Also, I do not understand what you mean by "he is opted by". Did you mean to say, "he is presented with the options ..." ?
Therefore, I am guessing your situation.
You have a listbox of existing items.
You have a textbox which allows freeform text entry
Any items whose prefix values matches the current textbox entry, the listbox items would be filtered to be limited to the matching items.
Even if the current textbox entry presents matching prefixes to filtering the listbox, the user can still perform freeform text entry. So, there are two possible cases here
4.1 the user clicks on the list box to select one of the filtered items
4.2 the user press enter key, which triggers selection of the current value of the textbox.
However, you find your widget participating in a race condition, so that when you click on the widget, the ValueChangeHandler gets triggered rather than the SelectionHandler. I do not know the structure of your widget so that is my best guess.
The problem is that you are allowing two separate modes of obtaining an outcome and you probably did not have well-defined state machine to handle choosing the appropriate mode. One mode is by the textbox and the other is by selection on the listbox - and you do not have a well-defined way of which would mode would be effective at any moment.
If my guess is accurate, this is what you need to do:
You must restrict your outcome to coming from only the textbox.
Your listbox selection must not trigger any outcome. Any change in listbox selection must propagate back to the textbox - to allow the user the chance of making further freeform entry based on that value.
only the keyboard enter on the textbox will trigger the final outcome.

Check for entry in text fields and comboboxes to enable a button Java Desktop Application

I am writing a Java application for digitizing a group of documents in the office that I am working in and I am wanting to check if 5 textfields are populated and 4 combobox fields as well before the save button is enabled (I have it checking if i press a button (that happily says "Check"), but i would much rather have it auto-check to see if they are populated or if they are null).
If it makes a difference i am using NetBeans for this project.
Basically I need the fields to have something in them before the document can be saved.
Any and all help will be greatly appreciated as this is the final step in creating this application... :D
Thanks,
Erik
There are two ways (I can think of):
1- Put a listener on each field, this listener will be triggered when the field is populated. Inside the listener increment a counter for example, or set a flag. If all flags are set or if the counter reaches (9 in your example) then enable the button.
2- Enable the Save button, but call a validate() method before doing the Save action. Any unpopulated field will have a red mark beside it (shown by validating) like in web applications.

Categories