I am having trouble using and IF statement based on a string from my preferences.
Here is the code:
preferences = PreferenceManager.getDefaultSharedPreferences(this);
String themePref = preferences.getString("theme", "null");
Log.i("Theme", "Current theme is " + themePref );
if (themePref == "dark"){
setTheme(android.R.style.Theme_Black);
}else{
setTheme(android.R.style.Theme_Light);
}
I have two options at this point; light and dark. The code successfully sets themePref as light or dark as needed, and I've confirmed via debugging, but for some reason the if statement fails. I have manually set a string to "dark" and it then works correctly.
Am I missing something here? Why would a string coming from the preferences process any differently?
Any help would be greatly appreciated.
Thanks,
Josh
You should use themePref.equals("dark") (or even better "dark".equals(themePref)) to compare strings. == tells you if the string instances are the same, but two instances can have the same value and not be the same instance.
Is PreferenceManager.getDefaultSharedPreferences(this).getString(...) returning a java.lang.String instance or a subclass? I would replace themePref == "dark" with themePref.equals("dark") instead.
Related
private static final String Accept = "Accept & continue";
public void acceptWorkspaceCreation() {
//Wait for Set up a Work Profile Screen for Android 9 Pixel
waitUtilByText(180, Accept);
assertTrue("Couldn't click on Accept & continue.",
findElementByIdAndClick("com.android.managedprovisioning", "next_button"));
}
public boolean waitUtilByText(int seconds, String text){
String textStr = "//*[#text='" + text + "//*[#id='";
return !new WebDriverWait(this.driver, seconds).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By
.xpath(textStr))).isEmpty();
}
For some cases Accept value comes in caps 'ACCEPT & CONTINUE', How to validate both the strings in selenium.
The condition you are waiting for only checks for one thing:
ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(textStr))
So, presumably (as I am not familiar with Selenium), you can use the ExpectedConditions.or method to check a number of conditions, and wait for just one to be true:
ExpectedConditions.or(
ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(textStr)),
ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(textStr2)))
where textStr2 is the alternative text you are looking for.
I'd suggest using native Android UiSelector's textMatches method where You'd write matcher for both upper and lower case texts. If you want to stick to the Xpath anyways - it also has a text matcher. But first of all, investigate why the text changes - that's not a good practice.
So my app is saving the names of Bluetooth devices the user has connected to previously in SharedPreferences which is than compared to all of the names of currently paired devices so on opening the app can instantly connect to the said device. This is done by this piece of code:
sharedPreferences = getApplicationContext().getSharedPreferences("BtNames", MODE_PRIVATE);
keys = sharedPreferences.getAll();
for(BluetoothDevice device : pairedDevices) {
try {
for (Map.Entry<String, ?> entry : keys.entrySet()) {...}
This loops through the paired devices and the entries of SharedPreferences whose value than is accessed by this code:
String device_name = device.getName();
String name = entry.getValue().toString();
Now both of these work well and entry.getValue()... returns the exact names of the previously connected devices. The problem occurs when trying to compare the two Strings by:
device_name.equals(name)
This returns false even though both of the Strings appear to be exact same when logged:
E/FaceTracker: EV3LO
E/FaceTracker: EV3LO
I have already tried to replace all spaces with nothing but that didn't work either. Maybe I overlooked something but at the moment I don't really have a clue what's going wrong.
Thanks in advance for answers.
The problem is a non printable and non ASCII character at the end or the beginning of the string. Please try the following script:
name.replaceAll("\\P{Print}","");
I hope it helpem and good luck if it didn't
I am porting my Android app to its cross-platform version (to have the iOS version) by means of Codename One.
I would like to use string resources like in the Android version.
I created a localization bundle (named "Localization (L10N) 1") only with english words for now.
In the main form I put this:
theme = UIManager.initFirstTheme("/theme");
String lang= L10NManager.getInstance().getLanguage();
UIManager.getInstance().setBundle(theme.getL10N("Localization (L10N) 1",lang));
In another container class I have:
String StringRes(String id)
{
String result;
result=UIManager.getInstance().getBundle().get(id);
return result;
}
when I need a string, for example:
add(new Label(StringRes("title_string")));
I get null pointer error in StringRes method.
I know that it is just an attempt to manage string resources.
What is the right way?
You are over thinking this... Codename One is localizable by default unlike any other framework I'm personally aware of.
Once you set the bundle value all you need to do is use the label with the right key e.g.:
add(new Label("title_string"));
It will "just work". Also you can use:
String value = getUIManager().localize("title_string", "This will show if title_string is missing from the bundle");
So StringRes isn't the right direction.
Im pretty pretty new to Dynamic-Jasper, but due to work i had to add a new feature to our already implemented solution.
My Problem
The Goal is to add a Column to a report that consists only out of a background-color based on some Information. I managed to do that, but while testing I stumbled upon a Problem. While all my Columns in the html and pdf view had the right color, the Excel one only colored the fields in the last Color.
While debugging i noticed, that the same colored Fields had the same templateId, but while all Views run through mostly the same Code the Excel one showed different behavior and had the same ID in all fields.
My Code where I manipulate the template
for(JRPrintElement elemt : jasperPrint.getPages().get(0).getElements()) {
if(elemt instanceof JRTemplatePrintText) {
JRTemplatePrintText text = (JRTemplatePrintText) elemt;
(...)
if (text.getFullText().startsWith("COLOR_IDENTIFIER")) {
String marker = text.getFullText().substring(text.getFullText().indexOf('#') + 1);
text.setText("ID = " + ((JRTemplatePrintText) elemt).getTemplate().getId());
int rgb = TypeConverter.string2int(Integer.parseInt(marker, 16) + "", 0);
((JRTemplatePrintText) elemt).getTemplate().setBackcolor(new Color(rgb));
}
}
}
The html view
The Excel view
Temporary Conclusion
The same styles uses the same Objects in the background and the JR-Excel export messes something up by assigning the same Object to all the Fields that I manipulated there. If anyone knows of a misstake by me or potential Solutions to change something different to result the same thing please let me know.
Something different I tried earlier, was trying to set the field in an evaluate Method that was called by Jasper. In that method we assign the textvalue of each field. It contained a map with JRFillFields, but unfortunatelly the Map-Implementation denied access to them and just retuned the Value of those. The map was provided by dj and couldn't be switched with a different one.
Edit
We are using JasperReports 6.7.1
I found a Solution, where I replaced each template with a new one that was supposed to look exactly alike. That way every Field has its own ID guaranteed and its not up to chance, how JasperReports handles its Data internaly.
JRTemplateElement custom =
new JRTemplateText(((JRTemplatePrintText) elemt).getTemplate().getOrigin(),
((JRTemplatePrintText) elemt).getTemplate().getDefaultStyleProvider());
custom.setBackcolor(new Color(rgb));
custom.setStyle(((JRTemplatePrintText) elemt).getTemplate().getStyle());
((JRTemplatePrintText) elemt).setTemplate(custom);
To speed up my debugging, I color certain messages for instant spotting, like this:
if (isOK)
Log.i(TAG, stringVarContentOfMessage);
else
Log.v(TAG, stringVarContentOfMessage);
It works, but viewing this source code over and over again, where the only justification for occupying 4 precious lines is one different character only (Log.i vs. Log.v) is an eyesore for me.
Any suggestions for avoiding this eyesore without resorting to the following?
isOK ? Log.i(TAG, stringVarContentOfMessage) : Log.v(TAG, stringVarContentOfMessage);
You can use Log.println():
Log.println(isOK ? Log.INFO : Log.VERBOSE, TAG, stringVarContentOfMessage);
Create a helper method:
private void conditionalLog(boolean flag, String tag, String message);
A better way is to color from the other end. Just color selected logcat messages. See: Modifying the Android logcat stream for full-color debugging That is a Python script that you can easily mod to your own pleasure.