Setting attribute values dynamically - android

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.

Related

How to set default/fallback value in Android?

How to set default/fallback value in order to see something in layout preview, when attributes are set by style?
You can use Design-time view attributes.
From the documentation:
You can insert sample data in your layout preview by using the tools:
prefix instead of android: with any attribute from the Android
framework. This is useful when the attribute's value isn't populated
until runtime but you want to see the effect beforehand, in the layout
preview.
For example, if the android:text attribute value is set at runtime or
you want to see the layout with a value different than the default,
you can add tools:text to specify some text for the layout preview
only.

How to set property of a view in layout XML file in Android?

For example, I am using android.support.v4.view.PagerTabStrip and I want to call its setTabIndicatorColor with my choice of color. How to do it in layout XML file?
Best thing you can do is to consult the documentation. For PagerTabStrip it is here.
There's a Summary section where you can check all XML attributes defined for that class. In this case, you can see that there are only attributes inherited from ViewGroup and View. None of them sets the tab indicator color.
This means that you need to set it in the code.
Let's look at the example where there is an attribute, e.g. orientation for LinearLayout. There's a entry for android:orientation attribute which tells you exactly how to set it in XML.

How to set margin for all TextView using styles.xml

I've read from another post that it's possible to set attributes like color to every TextView in an application: Setting global styles for Views in Android. However, I can't set layout_margin nor layout_height and layout_width attribute using that method to any textView, using "android:textviewStyle". On the other hand, if I use the style attribute and reference it to a style with all the attributes above, it works. Is there a way that you can use global styles and still set margin?
Thank you in advance
You can set padding instead of margin and you will have the same result.

Getting Style Name of a specific view - Android

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.

Is it possible to disable specific XML attributes from a custom view in Android?

I'm creating my own personalized ListView by extending the ListView itself. This particular ListView shouldn't have scrollbars.
How can I disable the XML android:scrollbars attribute for my custom ListView?
I can't find a way to disable them programmatically. What am I missing?
The answer from dzeikei's will disable the scrollbars programmatically and ignore any value from android:scrollbars but what I'm really asking on 1. is how to make android:scrollbars an invalid attribute for my custom component.
Updated
OK as Richardo found out, seems like my original answer is the reverse way since the scrollbar is displayed internally :)
The correct way will be to call
super.setHorizontalScrollBarEnabled(false) and super.setVerticalScrollBarEnabled(false) in the constructor and override setHorizontalScrollBarEnabled() and setVerticalScrollBarEnabled() do nothing :D
Override isHorizontalScrollBarEnabled() and
isVerticalScrollBarEnabled() in your subclass to return false.
you could also override setHorizontalScrollBarEnabled() and
setVerticalScrollBarEnabled() for good measure.
Try keeping #null for android:scrollbars. Am not sure. But, I usually use #null when i want to remove anything from XML attributes.

Categories

Resources