Questions tagged [android-custom-attributes]

64 questions
223
votes
5 answers

Same-named attributes in attrs.xml for custom view

I'm writing a few custom views which share some same-named attributes. In their respective section in attrs.xml I'd like to use the same names for attributes:
36
votes
2 answers

How do you add user defined properties/values in to the Android manifest file?

I want to add a custom attribute / property into the manifest file, and be able to read it at run time. I want to do this so I can customize the app's behavior via these manifest properties. How can this be done?
inor
  • 2,781
  • 2
  • 32
  • 42
17
votes
1 answer

Changing ConstraintLayout children's margin and size programmatically

I'm writing a custom view to be able to use custom XML attributes to set a view's margin and size based on these attributes, everything worked fine until i got to the part where children of ConstraintLayout get's their margins and size determined by…
11
votes
3 answers

Set default colors to Android library

So I have an Android library that I want others to be able to easily customize its color when using it. The problem is that the color is not just a single property (like the view background), but it's more like a theme color (the view background,…
10
votes
3 answers

Android CustomAttribute in MotionLayout to change Text in TextView

Anyone can help me with the correct way to change TextView's text in MotionLayout... this is what I doing. I'm testing the MotionLayout on a Simple App... I reach the part on the Motion tutorials about CustomAttributes With them you can change the…
10
votes
2 answers

getResourceId method of TypedArray

I am reading docs about getResourceId() method. It says that: Retrieves the resource identifier for the attribute at index. Note that attribute resource as resolved when the overall TypedArray object is retrieved. As a result, this function…
10
votes
1 answer

Safe enum custom attributes in Android

I have a situation that I presume is pretty common. I have a Java enum: public enum Flavour { CHOCOLATE, STRAWBERRY } I have a custom attribute defined in attrs.xml:
Martin
  • 3,703
  • 2
  • 21
  • 43
8
votes
5 answers

Get multiple style attributes with obtainStyledAttributes

I am trying to obtain several of the style attributes of the android namespace from my code. Here I enclose the relevant extract. AttributeSet attrs is the parameter that is passed in to any custom TextView. private static final int[] ATTRS = new…
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
6
votes
1 answer

Define custom ViewGroup attributes for children

I am looking for a way to define custom attributes for views that are used by their children. For example layout_centerInParent for children of RelativeLayouts or layout_span for children of TableLayouts.
6
votes
4 answers

How to get attributes with namespace "android" in custom TextView

In custom TextView I'm trying to obtain value of a text attribute (for example). TypedArray values = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView); String text =…
Dima
  • 1,490
  • 16
  • 25
6
votes
0 answers

Using custom attributes in custom views while in preview layout

I use Android Studio and have created my custom view:
Mortalus
  • 10,574
  • 11
  • 67
  • 117
6
votes
2 answers

Custom XML attributes without custom View in Fragment

I'm trying to add custom XML attributes in existing View Adding them to a custom View is not a big deal but I don't know how to access those attributes in a "basic" View (e.g. TextView, LinearLayout, ImageView ...) And this is more difficult when…
5
votes
1 answer

Custom attributes in style

In my current project styles.xml I try to define: and I use this: