Question Mark () in Xml Attributes for Android

Question mark (?) in XML attributes for Android

The question mark means it's a reference to a resource value in the currently applied theme. See the linuxtopia Android Dev Guide or the android.com Dev Guide for more about it.

\? escapes the question mark.

When to use a question mark for XML attributes?

the ? is for referencing style attributes; the documentation explains it:

To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional.

?[<package_name>:][<resource_type>/]<resource_name>

Question mark in XML as value

Try to use

 <string name="questionMark">\?</string>

and you can check this docs to handle special characters.

I've got a question mark to TextInputEditText. What does it mean?

Check out this similar question: https://stackoverflow.com/a/55541787/8242091.
(Would have commented but don't have enough reputation)

Question mark instead of RTL field

Thanks to @anil Finally i find two way for problem :

  1. change source file encoding in Settings>File Encoding :

Sample Image

if problem not resolve :


  1. First way + Using Strings from resources (R.String.nameOfString) instead type them directly in Java source code

initialize XML attributes in Custom View

You will want to use the AppCompat version of what you want to use, such as the AppCompatImageView as layoutParams would then be supported. One thing I also saw wrong: you must specify the context and all of the included attributes in the constructor of the class:

(context : Context, attrs : AttributeSet)

These will be auto assigned by the OS so nothing really needs to be done with them, but they must be there. Here is what I have for you:

class CustomImageView(context: Context,
attrs : AttributeSet) : AppCompatImageView(context, attrs) {
init {
applyDefaults()
}

private fun applyDefaults(){
val height = 130
val width = 300
layoutParams = ViewGroup.LayoutParams(width, height)
}
}

Get the current layout parameters by getting them from the base class, which would be AppCompatImageView. Then set the layout parameters to be the height and width you want as an integer.

What's the difference between @android: and ?android:

(from this answer and its comments)

Prefixing the ID with a question mark indicates that you want to access a style attribute that's defined in a style theme, rather than hard-coding the attribute, as explained in Referencing Style Attributes.

More specifically, the ? implies an extra level of indirection. Think of it as dereferencing a theme attribute to fetch the resource it points to rather than referring to the attribute itself. You see this with ?android:attr/foo



Related Topics



Leave a reply



Submit