I have an EditText and a custom Style, and I would like to know how I can programmatically, get the "name" of this style, like below:
<EditText
android:id="#+id/idValue"
style="#style/integerNumber"
... />
In the code,
EditText edValue = findViewById (R.id.idValue);
In this case, I need to receive "integerNumber". Is there a specific method to get this information? (such as edValue.getResources().getStyle()) ... I could not find at all.
I'm afraid you can't. Looking at the view constructor (in particular ), the style is used just to define view's properties but it's thrown away afterwards. It is not stored anywhere, so I guess you can't retrieve it from the view object.
If you need to get the style you can still (conventionally) add a tag to the views and behave according to that.
Related
I have a recycler view which contains a list of classes for a particular day. Each class has a particular color associated with it. This color is shown in a view at the side of each CardView-
<View
android:id="#+id/colourBar"
android:layout_width="4dp"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
/>
In my recycler adapter I call holder.colourBar.setBackgroundColor()
However, this has no effect. The int values being passed are definitely valid, even if I just pass my apps primary color to all of them, it still doesn't show.
However, calling holder.colourBar.setBackgroundResource(R.color.colorPrimaryLight);
does work and sets the background colors of the view.
Why is it that passing an integer color value doesn't result in the color showing up?
The input for setBackgroundColor(int) is different than setBackgroundResource(int).
If you want to use a resource, like R.color.colorPrimaryLight, then you should use setBackgroundResource(R.color.colorPrimaryLight). The equivalent with setBackgroundColor(int) would be:
setBackgroundColor(getResources().getColor(R.color.colorPrimaryLight));
A valid use of setBackgroundColor(int) would be something like setBackgroudColor(Color.parseColor("#FF0000")), but using resources as much as possible is a best practice.
R.color.colorPrimaryLight is a resource not a color if you want to use in setBackgroundColor() you have to call the method getResources().getColor(R.color.colorPrimaryLight)
like this
holder.colourBar.setBackgroundColor(getResources().getColor(R.color.colorPrimaryLight))
I saw a xml layout which has a textView as below
<TextView
android:id="#+id/tvHeaderTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Change password"
android:tag="#string/regular"
android:textAllCaps="true"
android:textColor="#color/header_color"
android:textSize="#dimen/font30" />
I want to know what is android:tag property is used for. A detailed answer for be greatly appreciated.
Note - I read that it is used for data binding, but could not follow the context.
Update 1 - (Thanks Blackbelt for the answer)
Usages
1. Could be used for compile time binding of xml elements with the activity. Although android recommends the use of id.
2. Could be used for identifying elements for list adapter. For eg, a special case of a list adapter with multiple sort support. Then we could use the tag element to identify the desired list item.
I would be glad to know if it could also be used for assigning any other property?
I want to know what is android:tag property is used for
it is a different way to add associate additional information with the view object itself. In you case your tag is a string, but it can store complex objects too. For instance the tag could be a model class set and retrieve at runtime using the setTag()/getTag() pair. The way this information is used is up to the users. Android allows to findViewWithTag too. In your case, for instance you could look for the same object using findViewById(R.id. tvHeaderTitle); or using findViewWithTag(getString(R.string. regular)); . An example of usage is with ListView when you have a button part of you item and, upon click you want to know the which item in your dataset is associated with that row. Using the pair setTag/getTag this thing is easily achievable
I have been searching around with google about this topic, but found no relevant information. It is clear to me how I can do it extending Views, but I don't want to extend anything.
I would like to somewhat "annotate" whichever android view (or whichever descendant of view) with custom properties and then retrieve their value in runtime.
Like this:
<TextView
custom:my_property_name="foo here"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_view"
android:text="Rule" />
Then once I have a reference to this TextView I would like to call a method like:
String myProperty = textView.getProperty("my_property_name");
myProperty.equals("foo here");
Is this possible? How?
Thanks.
At the very least you have to create your own class that subclasses one of the standard View classes (or View itself). The existing framework code does not read attributes that are not defined by Android and that are not part of the styleable declaration for that View.
Android documentation has a page describing creating your own views with your own XML attributes: http://developer.android.com/training/custom-views/create-view.html
There are always a few attribute values that Android doesn't explicitly define how to change and set dynamically for views. In my case, I'm trying to set the ListView's vertical scroll bar drawable. There's no method in the ListView class that lets you set this. I can only define this in the XML using android:scrollbarThumbVertical="#drawable/new_scroll_bar". Is there ANY workaround that would let me change attributes not otherwise defined dynamically?
It seems impossible, since the document doesn't refer to a corresponding method to set that XML attribute.
In the docs it says it corresponds to the global resource attribute:
android.R.attr.scrollbarThumbHorizontal
You can set this equal to a drawable programatically.
Ref: http://developer.android.com/reference/android/view/View.html#attr_android:scrollbarThumbVertical
Let me know if that is what you were trying to get to.
using:
android:textAppearance="?android:attr/...."
I would like to style a TextView to look like the default style of the "summary" attribute within a PreferenceScreen.
using the styles from this official list, I have tried "summary" and "summaryOn", but neither of them looks like the default style for summaries. Can anyone explain why, or suggest which one I should be using?
There isn't a theme attribute that refers to the specific semantic of preference summary text, preferences use ?android:attr/textAppearanceSmall as illustrated here: http://goo.gl/3yZmx (This is the layout file used by the Android framework for preference items.)
The correct way to reference an ID is using #.
Such as #string/my_string
Since you wish to reference an ID defined by Android directly, try this:
android:textAppearance="#android:id/summary"