ResourcesNotFoundException - android

I have a value in my dimens.xml defined as follows:
<dimen name="input_text_width">250dp</dimen>
I want to use it to dynamically size the width of a TextView, as follows:
tv.setWidth(activity.getResources().getInteger(R.dimen.input_text_width));
The resource exists (checked in R.java). I have run "clean" on my project more than once. Why do I keep getting this exception?
android.content.res.Resources$NotFoundException: Resource ID #0x7f050012 type #0x5 is not valid

I have taken this from Android Docs
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="ball_radius">30dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
And in code
//Retreive dimension
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);
In your xml
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
I hope this will help!

You have to get the dimension pixels...
int size = activity.getResources().getDimensionPixelSize(R.dimen.input_text_width);
textView.setWidth(size);

Might it be because you use :
activity.getResources().getInteger(R.dimen.input_text_width)
Instead of :
activity.getResources().getDimension(R.dimen.input_text_width);

Related

What is the correct way to use typography in Material Design 3?

In material design 3, there are 15 types of typography tokens:
textAppearanceDisplayLarge (57sp)
textAppearanceDisplayMedium (45sp)
textAppearanceDisplaySmall (36sp)
textAppearanceHeadlineLarge (32sp)
textAppearanceHeadlineMedium (28sp)
textAppearanceHeadlineSmall (24sp)
textAppearanceTitleLarge (22sp)
textAppearanceTitleMedium (16sp)
textAppearanceTitleSmall (14sp)
textAppearanceBodyLarge (16sp)
textAppearanceBodyMedium (14sp)
textAppearanceBodySmall (12sp)
textAppearanceLabelLarge (14sp)
textAppearanceLabelMedium (12sp)
textAppearanceLabelSmall (11sp)
Here is how I used the tokens:
values\dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textSizeDisplay">36sp</dimen>
<dimen name="textSizeHeadline">24sp</dimen>
<dimen name="textSizeTitle">14sp</dimen>
<dimen name="textSizeBody">12sp</dimen>
<dimen name="textSizeLabel">11sp</dimen>
</resources>
values-large\dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textSizeDisplay">45sp</dimen>
<dimen name="textSizeHeadline">28sp</dimen>
<dimen name="textSizeTitle">16sp</dimen>
<dimen name="textSizeBody">14sp</dimen>
<dimen name="textSizeLabel">12sp</dimen>
</resources>
values-xlarge\dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textSizeDisplay">57sp</dimen>
<dimen name="textSizeHeadline">32sp</dimen>
<dimen name="textSizeTitle">22sp</dimen>
<dimen name="textSizeBody">16sp</dimen>
<dimen name="textSizeLabel">14sp</dimen>
</resources>
Depending on the text type, I use the tokens
<com.google.android.material.textview.MaterialTextView
...
android:textSize="#dimen/textSizeTitle" />
The question:
Did I use the tokens in the right way or there is another way to use the tokens?
Also, I know when I should use Display and Body, But when should I use Headline, Title, and Label because I think they are the same?
I read this and this but still don't know when should I use Headline, Title, and Label.
Thank you.

How can I share a int value globally (xml and code)?

In my Android app, I use LinearLayout's weight at multiple point in my app. Those weight are both used in xml and in code as I change them dynamically.
To reuse the same values everywhere, I thought I could declare them in my dimens.xml file like this:
<resources>
<dimen name="total_weight">5</dimen>
<dimen name="expanded_weight">3</dimen>
<dimen name="collapsed_weight">2</dimen>
</resources>
But it doesn't work as dimen only accept dp, px or sp units.
What workaround could I use?
you can use
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="integer_name">0</integer>
</resources>
access by
resources.getInteger(R.integer.integer_name)
you can take a look at https://developer.android.com/guide/topics/resources/more-resources

Padding value in android

What is the meaning of this line of code?
android padding Right = "# dimen /activity_horizontal_margin".
What is #dimen referring to?
you use the dimens file like this so that you can easily set many things to the same value (like a constant in code), or so you can override the value easily for different screen sizes.
dimen is located in / res / values / in studio project like below
dimen.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="action_button_min_width">56dp</dimen>
<dimen name="indeterminate_progress_size">32dp</dimen>
</resources>
Learn more about project structure in android studio
https://developer.android.com/studio/projects/index.html
dimen.xml is the file which you can set constant values for dimensions.
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
It is usually created inside /res/values folder. If you don't find it there you can create and add values.
You may refer the following link for more details.
https://developer.android.com/samples/BasicSyncAdapter/res/values/dimen.html

dimens.xml -> R not generating

When I add the file dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="smart_eyeglass_controll_width">419</dimen>
<dimen name="smart_eyeglass_controll_height">138</dimen>
</resources>
to my values folder, R will not be generated.
What is wrong?
The file is OK - the rest of the project to. Cleaning the project up in Eclipse won't work.
Why it Showing
Because there are some compile error (or bug?) regarding to the xml file in res, so R is not genetared .
So,basically just Add unit beside your dimen value Like dp
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="smart_eyeglass_controll_width">419dp</dimen>
<dimen name="smart_eyeglass_controll_height">138dp</dimen>
</resources>
Then rebuild and clean your project
because you can't create dimens whiteout indicating that whether its a dip or sp or... if you want to create an integer value use:
<integer name="something">100</integer>
and if it has a unit put it..
<dimen name="width">100dip</dimen>
you have to specify an unit for your value, such dp. E.g
<resources>
<dimen name="smart_eyeglass_controll_width">419dp</dimen>
<dimen name="smart_eyeglass_controll_height">138dp</dimen>
</resources>

How can I make a dimension a multiple of another dimension?

I was wondering if it is possible to make a dimension a multiple of another dimension in Android. E.g., so I can do something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="base">50dp</dimen>
<dimen name="size_of_something">4*#dimen/base</dimen>
</resources>
Right now, I use a workaround like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="base_x1">50dp</dimen>
<dimen name="base_x2">100dp</dimen>
<dimen name="base_x3">150dp</dimen>
<dimen name="base_x4">200dp</dimen>
<!-- etc... -->
<dimen name="size_of_something">#dimen/base_x4</dimen>
</resources>
But this means that I have to update all the multiples every time I change the base size, which is not only cumbersome, but also error-prone.

Categories

Resources