Update a text field in BB OS 6 when a listener method is invoked - blackberry

For the GUI portion of my app, how could I update the RichTextField when my batteryStatusChange method is invoked that changes on the spot?
I was thinking of calling a set method and then having RichTextField get that new number, but it will make a long list of lines unless I delete the textfield before I add a new one.
Something like the battery percentage number under Device Information or the signal strength that changes on the spot.
Edit: Figured it out using setText
public void batteryStatusChange(int status)
{
// TODO Auto-generated method stub
if ((status & DeviceInfo.BSTAT_LEVEL_CHANGED) != 0)
{
batteryStatusField.setText(getBatteryLevel());
}
}

batteryStatusField.setText(getBatteryLevel());
public String getBatteryLevel() {
return Integer.toString(DeviceInfo.getBatteryLevel()) + " %";
}
Got it to work with this code above by putting it inside my batteryStatusChange listener function. Later I will add in more parameters after the function getBatteryLevel() to keep my default formatting.
My battery app in progress

Related

TextView adding a variable string and appending another

I am using IntentExtra to pass three variables from an entry in a RecyclerView from one Activity into a TextView on another Activity (ActivityTwo) using Get Extras. That all works fine and the variables are joined and displayed in the TextView.
TextView mTitle = (TextView) findViewById(R.id.textViewOrderList);
mTitle.append(number + title + (Double) price);
I then navigate back to ActivityOne, select a different item in the RecyclerView and the new variables are sent to ActivityTwo.
However, despite using append, it either a/ overwrites the existing text, OR b/the first set of text is not retained. Am not sure which
I did consider saving the text to a local file and then appending to it each time I enter ActivityTwo. Then loading it into the TextView But this feels like using a steamroller to crack a nut!!
Any solutions much appreciated.
In the end I decided to write to a local file. As I wanted to Append info rather than overwrite I used the MODE_APPEND rather than MODE_PRIVATE.
try {
FileOutputStream fOut = openFileOutput(fileTitle,MODE_APPEND);
fOut.write(dataTitle.getBytes());
fOut.write('\n');
fOut.close();
Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

ListGrid put focus in the FilterEditor

I have a ListGrid defined like this:
ListGrid lgrid = new ListGrid();
ListGridField first = new ListGridField("first",first");
ListGridField second = new ListGridField("second ",second ");
lgrid.setFields(first, second);
lgrid.setShowFilterEditor(true);
¿How can i put the keyboard focus in the first filter editor field after i call show() in the layout?
Thxs in advance.
Depending on what your use case is (which would be useful to provide a more focused answer), the solution you posted might not be what you really need, because if you scroll on your ListGrid, it could trigger a new data fetch (if there are more records to show), and move the cursor to the filter editor as a result (if your user is editing some records at that point, the cursor moving to the filter row is not what she would want to happen!!).
In such a case, you probably just want to call grid.focusInFilterEditor("fieldToFocus") after the listGrid.show() statement or in the ClickHandler of some button you use to fetch the data, etc.
Anyway, you don't need the Timer either. This works:
listGrid.addDataArrivedHandler(new DataArrivedHandler() {
#Override
public void onDataArrived(DataArrivedEvent event) {
grid.focusInFilterEditor("fieldToFocus");
}
});
I got the solution, its focusInFilterEditor, this is an example to set the focus after the data arrived to the grid:
// Put the focus on the first listGrid field when is loaded
listGrid.addDataArrivedHandler(new DataArrivedHandler() {
#Override
public void onDataArrived(DataArrivedEvent event) {
Timer t = new Timer() {
public void run() {
if(listGrid.getFilterEditorCriteria() == null){
listGrid.focusInFilterEditor("fieldToFocus");
}
}
};
t.schedule(600);
}
});

How do I prevent one specific character to be entered into a UITextView (in Xamarin)?

I need to prevent users from entering a caret ("^") into a notes field that is implemented in a UITextView. I found this question: prevent lower case in UITextView, but it's not clear to me when/how often the shouldChangeTextInRange method will be called. Is it called for each keystroke? Is it named this way because it will be called once for a paste? Instead of preventing the entire paste operation, I'd rather strip out the offending carets, which it doesn't look like that method can do.
Our main application (written in C++Builder with VCL components) can filter keystrokes, so that if ^ is pressed, it beeps and the character is not added to the text field. I would like to replicate that behavior here.
Is there any way to do that sanely in Xamarin? I'm doing iOS first, and might be asking about Android later.
Thanks for your help!
Are you using Xamarin.Forms to build your UI? If you're going to be targeting Android, I highly recommend doing so.
If that is the case, then you can easily do this with a custom Entry subclass:
public class FilteredEntry : Entry
{
private string FilterRegex { get; set; }
public FilteredEntry (string filterRegex)
{
// if we received some regex, apply it
if (!String.IsNullOrEmpty (filterRegex)) {
base.TextChanged += EntryTextChanged;
FilterRegex = filterRegex;
}
}
void EntryTextChanged (object sender, TextChangedEventArgs e)
{
string newText = e.NewTextValue;
(sender as Entry).Text = Regex.Replace (newText, FilterRegex, String.Empty);
}
}
Usage:
// The root page of your application
MainPage = new ContentPage {
Content = new StackLayout {
VerticalOptions = LayoutOptions.Center,
Children = {
new FilteredEntry(#"\^")
}
}
};
A typed ^ will be stripped out of the Entry's Text.

How to push new screen from global screen in blackberry?

Here I am display push notification in globalscreen in blackberry, I need to push screen by clicking OK button of the dialog. I want to start app by clicking the ok button.
Please help me.
Thanks in advance!
I'm not 100% sure I understand what you want, but if this doesn't work, just add a comment and I'll try to give you a better answer.
First, read this on pushing global screens
and this on performing actions after receiving global alerts
Your code, if I'm understanding correctly, should be similar to the second link's example.
Then, if you implement the DialogClosedListener, like in the second link, you might have something like this:
called from the background when you get notified:
Dialog myDialog = new Dialog(Dialog.D_OK_CANCEL, "Hello", Dialog.OK, null, 0);
myDialog.setDialogClosedListener(new MyListener());
UiApplication.getUiApplication().pushGlobalScreen(myDialog, 1, true);
implementation of your dialog listener:
private class MyListener implements DialogClosedListener {
public void dialogClosed(Dialog dialog, int choice) {
switch (choice) {
case Dialog.OK:
// ok clicked
UiApplication.getUiApplication().requestForeground();
break;
case Dialog.CANCEL:
// cancel clicked. or escape pressed
break;
default:
break;
}
}
}
And, then in your UiApplication class, you can respond to activation, which will happen if the user selects Ok from the Dialog:
public class MyApp extends UiApplication {
private boolean _nextScreenShowing = false;
public void activate() {
super.activate();
if (!_nextScreenShowing) {
pushScreen(new NextScreen());
_nextScreenShowing = true;
}
}
}
I show the _nextScreenShowing variable, just to make sure you think about whether pushing the next screen is appropriate. It probably won't be every time activate is called. You may need to keep track of that boolean flag by responding to the Application.deactivate() method, or maybe Screen.onExposed() or Screen.onObscured(). All that depends on how your app works.

Vaadin addStyleName problem

I created a TextField with TextChangeListener. When user types in certain values (in this case 'admin') then addStyleName is invoked on that field and font color becomes red. But afterwards, the value is blank and each entered character is being cleared.
Here is the code of the application. Why after adding new style to TextField its value changes?
public class VaadintestApplication extends Application {
#Override
public void init() {
Window mainWindow = new Window("Vaadintest Application");
setTheme("test");
TextField textField = new TextField("username");
textField.setEnabled(true);
textField.setTextChangeEventMode(TextChangeEventMode.EAGER);
textField.addListener(new TextChangeListener() {
public void textChange(TextChangeEvent event) {
if ("admin".equals(event.getText())) {
((TextField) event.getComponent()).addStyleName("text-error");
} else {
((TextField) event.getComponent()).removeStyleName("text-error");
}
}
});
mainWindow.addComponent(textField);
setMainWindow(mainWindow);
}
}
I would guess that the following happens:
The style name change triggers a repaint on the server, causing the TextField component to be serialized again to the client
The client receives the serialization (the whole bloody thing, not just the changed parts, because that's how things work with Vaadin), and hence it changes the contents of the textfield, while ignoring any changes that are pending from the text change listener
Solutions:
Update the value of the TextField at the same time you add/remove the style name: ((TextField) event.getComponent()).setValue(event.getText())
Create a custom client side widget which extends VTextField and add the functionality there

Resources