I have a dimension resource setup in my res/dimens.xml like so:
<dimen name="cornerRadius">40.0dp</dimen>
However, when I grab it into a float object like this:
float cornerRadius = R.dimen.cornerRadius;
And output the value, it gives me 2.1309686E9 every time. I'll change the value from 40.0dp to 15dp and it still outputs 2.1309686E9.
What am I doing wrong?
R.dimen.cornerRadius is just a reference but not a value. You should use getResources().getDimension(R.dimen.cornerRadius) instead
You are getting the ID not the value so try this :
getResources().getDimension(R.dimen.cornerRadius)
Related
I have following line in dimens.xml;
<dimen name="pagePadding">8dp</dimen>
and following line in my activity class;
int d = R.dimen.pagePadding;
When I put a breakpoint right after this line, I expect d to be a number like 16 or 24 depending on my pixel density but instead it is a huge number, 2131099861 exactly. Why could this be?
R.dimen.pagePadding is a resource identifier. To get the dimension:
Call getResources() on your Activity or other Context
Call getDimension() on the Resources
So, you wind up with:
float d = getResources().getDimension(R.dimen.pagePadding);
I want to use the string resources to save some key values. So, I have something like this:
<string name="key_name">key_name</string>
The resource name and the resource value are the same. There is no need for me to make a difference between them, as it is just to cleanly store one value.
To reduce redundant information,
is there a way to just tell android-studio that the name equals the value? Something like this?
<string name="key_name"/>
I think you can't do it. It looks like you create a variable, you must name it and when you need you can set its value.
You can't create a variable with same values with name at the same time.
I think you have the wrong resource type. A string resource is used for mapping a name to a text. From what you're describing here you only need the name (and then can extrapolate the text from it).
Using strings with matching name and value shouldn't be necessary:
If you're going to be referencing them from Java or Kotlin code, you still need to know the name (i.e. R.string.key_name), so you might as well just use the string value there.
If it's for using inside XML code (where you want to pass the value of a string, e.g. text="#string/key_name") most of the time you can just use the raw string there (i.e. text="key_name").
How can I set textsize from setting preference fragment into textView? The font size is selected from a listView I will provide the code below the image.
Please see my image below:
I know I am doing something wrong. Please show me a proper way to do this.
This is my font size value:
This is my listpreference:
<ListPreference
android:title="Font Size"
android:summary="Select desirable font size"
android:key="FontSizeKey"
android:entries="#array/select_font_size"
android:entryValues="#array/select_font_size_value"
android:defaultValue="24"/>
and also if you guys have any website/tutorial/blogs link for me to refer, it would be good.
The value which TextView accept is float. One more thing is that to get the value from SharedPreference, use KEY.
String setFontSize = getSharedPreferences.getString("FontSizeKey");
setDuaText = setTextSize(Float.valueOf(setFontSize));
convert string to float using this method
secDuaText.setTextSize(Float.parseFloat(setFontSize));
From the hint its quite clear that you the method is expecting a float value. And you are passing it a string value. So either save it as a float using putDouble in your shared prefs and use getDouble and save it in a float variable. Like below
SharedPrefs.putDouble("FontSizeKey", 24.0);
double setFontSize = SharedPrefs.getDouble("FontSizeKey");
Else, just parse the String for a float value like below
secDuaText.setTextSize(Float.parseFloat(setFontSize));
I'm trying to compare whether the background color of one of my views is equal to a color resource I created and haven't found anything that works.
I'm willing to use any method, whether converting to HEX or String or Int, as long as it works. Here's an example of my current method.
I set the background color of a view with:
chosenColor.setBackgroundColor(getResources().getColor(R.color.tag_pink));
When I retrieve it using this method:
ColorDrawable chosenColorBox = (ColorDrawable) chosenColor.getBackground();
int colorId = chosenColorBox.getColor();
colorId == `-611329`
Using Integer.toString on R.color.tag_pink shows the value as 2130968581 which obviously can't be compared in an if statement" with the retrieved value.
What's the best way to do this so I don't have to resort to hard-coding the individual values, which prevents me from adjusting the color resources?
Try to compare the color to Context.getResources().getColor(R.color.tag_pink) instead of the resource's ID (which is not the color!). The returned value represents the color in the form of 0xAARRGGBB.
Just simple Answer:
int viewColor=((ColorDrawable) YOUR_VIEW.getBackground()).getColor();
if(viewColor==Color.parseColor("#ffffffff"){
//Color matched
}
I want to have a parameter in one string in strings.xml and this parameter should be a double value. So I use %1$f. Here - http://developer.android.com/reference/java/util/Formatter.html there are many examples, but what if I want to have have a few double/float parameters and I want only the second one to have 2 digits after .? I tried to use combinations like %2$.2f or %2.2$f. Nor of them worked. %.1f does not work as well.
So, does anybody know how can I "customize" a float/double value inside a strings.xml? Thanks.
Just adding to #David Airam's answer here; the "incorrect" solution he gives is actually correct, but with a bit of tweaking. The XML file should contain:
<string name="resource1">Hello string: %1$s, and hello float: %2$.2f.</string>
Now in the Java code:
String svalue = "test";
float sfloat= 3.1415926;
String sresult = getString(R.string.resource1, svalue, sfloat);
The exception that #David Airam reported is from trying to jam a String into a format specifier with %f, which requires a floating point type. Use float and there is no such exception.
Also, you can use Float.valueOf() to convert a String to a float in case your input data was originally a string (say, from a EditText or something). However, you should always try/catch valueOf() operations and handle the NumberFormatException case, since this exception is unchecked.
%.1f work for me if you like to show only 1 digit after ','
Define is strings.xml file
<string name="price_format">$%,.2f</string>
//For using in databinding where amount is double type
android:text="#{#string/price_format(model.amount)}"
//For using in java runtime where priceOfModifier is double type
amountEt.setText(context.getResources().getString(R.string.price_format, priceOfModifier));
This worked for me.
<string name="market_price">Range ₹%1$.0f - ₹%2$.0f</string>
android:text="#{#string/market_price(viewModel.suggestedPriceRange.max, viewModel.suggestedPriceRange.min)}"
Outputs: Range ₹500 - ₹1000
In ₹%1$.0f, .0f defines how many digits you want after the decimal.
A simpler approach:
<string name="decimalunit">%.2f%n</string>
float sfloat= 3.1475926;
String sresult = getString(R.string.decimalunit, sfloat);
Output: 3.15
If it were me I'd store the values in the resources as simple values, and then use formatter methods to control how they're displayed, roughly like this
public String formatFigureTwoPlaces(float value) {
DecimalFormat myFormatter = new DecimalFormat("##0.00");
return myFormatter.format(value);
}
public String formatFigureOnePlace(float value) {
DecimalFormat myFormatter = new DecimalFormat("##0.0");
return myFormatter.format(value);
}
I now that this reply is arriving too late... but I hope to be able to help other people:
Android sucks with multiple parameters substitutions when you want decimal numbers and format this in common style %a.bf
The best solution I have found (and only for these kind of resources) is put the decimal parameters as strings %n$s and in the code apply my conversion with String.format(...)
Example:
INCORRECT WAY:
// In xml file:
<string name="resource1">You has a desviation of %1$s and that is a %2$.2f%% percentage.</string>
// And in java file
String sresult = getString(R.string.resource1, svalue, spercentage); // <-- exception!
This solution is technically correct but incorrect due to android substitution resources system so the last line will generate an exception.
CORRECT WAY / SOLUTION:
Simply convert the second parameter into a String.
<string name="resource1">You has a desviation of %1$s and that is a %2$s percentage.</string>
And now in the code:
...
// This is the auxiliar line added to solve the problem
String spercentage = String.format("%.2f%%",percentage);
// This is the common code where we use the last variable.
String sresult = getString(R.string.resource1, svalue, spercentage);