Use Android defined style programmatically?
-
11-02-2021 - |
Question
I want to keep my app as close to the devices original theme as possible. Therefor I use this layout for my separators:
This is spored in a separate layout file. Isn't there anyway to use the same resource programmatically?
I've tried with:
TextView tvSeparator = new TextView(context);
tvSeparator.setBackgroundResource(android.R.drawable.list_selector_background);
But I don't know the padding, the font size, the font style etc from this :/
Solution 3
I sovled it by just inflating android.R.layout.preference_category since that is a textfield
OTHER TIPS
You must use the
context.getResources().getDrawable(R.id.xxx);
(Might differ, just remembering from my head..)
Since the resource you are using is the android.R resources, what you can do is this:
The list_selector_background.xml file will be there in your android SDK (where ever you have downloaded it). Look for it, copy the contents of this file in a new file called my_list_selector_background.xml in your resources folder (where all the other layout files are present)
Change whatever you want to change in it - padding, font-size, anything.
Now access this resource from your R instead of Android.R ..
so the code would now look like:
TextView tvSeparator = new TextView(context);
tvSeparator.setBackgroundResource(R.drawable.my_list_selector_background);
Note however that you cannot change the original list_selector_background.xml