Read custom attributes from activity
Its quite small article about reading custom attributes from theme in Activity code.
First we need to define our custom attribute.
<attr name="TitleText" format="reference|string"/>
Apply this attribute to Activity’s theme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="TitleText">@string/app_name</item>
</style>
Read this attribute in activity and set TextView text.
TextView text = (TextView) findViewById(R.id.text);
TypedValue lTypedText = new TypedValue();
getTheme().resolveAttribute(R.attr.TitleText, lTypedText, true);
//check if we have value set
if (lTypedText.resourceId != 0) {
String title = getResources().getString(lTypedText.resourceId);
text.setText(title);
}
Full source code for this article is available on GitHub