I have the following code:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:inputType="text"
android:maxLength="10"
android:textColorHighlight="#7cff88"
android:hint='#string/nome'
android:textColor="#ff0000"
android:textAlignment="center"
android:layout_weight="1"
>
></EditText>
Is there a way to set a double string in the hint? Something like #string/age & #string/name or #string/age :
I am trying to add ´:´ without adding it to the string.xml file.
You shouldn't do that. Why? Localization. To you, as an English speaker, "name:age" might make a lot of sense. It may not make sense in other languages. Other languages have syntactic rules that differ and may make it nonsensical or worse. Or it may have different words for the same concept in different contexts, and this will cause it to use the wrong one.
In general, you should never concatenate strings for display. You should always use a single parameterized string, and allow the translators to translate the entire thing in context. This minimizes the chance of a mistake.
Now if this is a personal app that will never be translated- no there's no ability to do that in xml, unless possibly via databinding. But I wouldn't suggest using databinding like that unless its an architectural decision that your entire app is going to invest into databainding.
Related
I am trying android data-binding and I am really amazed with its capabilities. It really shifts all the getters and setters of views to a single place (layout files or custom binding adapters) .
But, I don't understand what is the use of the namespace here? I was reading a recent article from George on Medium here, and he mentioned that:
Anything in the application namespace doesn’t need any namespace in the parameter, but for attributes in the android namespace, you must give the full attribute name including the “android.”
So, if I don't give a namespace, it works. My custom attributes are namespaced with xmlns:app="http://schemas.android.com/apk/res-auto" . I really don't know what that means.
Say I have a text view where I am doing this like this (hardcoded strings just for example) :
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text='#{"Created By:" + user.name + " on " + user.time}' />
It's really good as I can provide arguments here:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:createdBy="#{user.name}"
app:createdAt="#{user.time}" />
And handling that code in Java by setting that text to the same as on above example. This provides an easy to use and understandable code for the layout files.
But, this arguments can collide sometimes. If I am using this at 2 different places with 2 different formats like Created by User A on 7/10/2016 and From User A (7/10/2016). This both can requires same arguments as I showed in second example. How can I distinguish them?
I can also provide third argument like app:format and so that my custom binding function can understand, but can namespaces play the important role here? Can I provide a custom namespaces for multiple data-binding elements and handle them accordingly in my binding adapters? Or namespaces will just be ignored as George mentioned in his article?
In the official documentation they have used bind: namespace and in binding adapter methods they have provided namespace there. I am little bit confused about namespaces' role in data-binding.
XML is very flexible and allows you to provide any number of namespaces. Android, however, isn't (yet) flexible enough to handle more than two -- android and your application's namespace. Android data binding doesn't distinguish between any application namespace name. e.g. bind: or app: doesn't matter because they all refer to http://schemas.android.com/apk/res-auto.
So, the answer to your question is "No." Namespaces won't really help. You could use the android namespace -- assuming there aren't unfortunate collisions. That only extends your flexibility from one to two and I'm pretty sure you want something better than that.
This specific example is perfect for string formatting expressions:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text='#{#string/createdBy(user.name, user.time)}' />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text='#{#string/fromUser(user.name, user.time)}' />
But generally speaking, you'll have to either use different attributes names for different uses or add an attribute for the differentiator as you suggested.
This is more a 'is there a more appropriate way' question as I have found a work around.
Some of my table headers are being picked up as spelling errors and underlined in red. Of course, that is not what I would like to see. I have found that using
android:inputType="textNoSuggestions"
does disable the spell check markings. I find it odd (bug?) that this is necessary as the docs state:
inputType: The type of data being placed in a text field, used to help
an input method decide how to let the user enter text.
and there is no input associated with just textView. So is this the only/more appropriate way of avoiding the spell check and also, is this a bug that it is spell checking non-input fields?
UPDATE: per request this is sample xml
<TextView
android:text="ID#"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textSize="14sp"
android:digits="4"
android:textAlignment="gravity"
android:layout_weight="5"
android:gravity="left"
android:singleLine="true"
android:phoneNumber="true"
android:inputType="textNoSuggestions|none">
</TextView>
First, I would try removing the android:digits, android:phoneNumber, and android:inputType attributes.
All of those are more intended for use with fields that allow input (such as EditTexts). It also doesn't look like you are using the android:digits attribute correctly, as the string you provide defines the only allowable characters.
In essence, this combination of attributes is telling Android that your TextView accepts input in the form of telephone numbers that contain only the number 4, that this TextView doesn't accept input of any type, and Android should not provide spellcheck suggestions.
If you are setting the content of the TextView yourself, there really is no reason to try to restrict the content of the TextView with flags such as android:phoneNumber since you are the one controlling that.
I know this is an old thread but removing the following from content XML worked for me:
android:autoText="true"
On later android studio versions try:
android:autoText="true"
inside of the (or any input) in the xml.
On newer versions try:
android:inputType="textNoSuggestions"
So I'm trying to decide whether it would be worth it to refactor out my current use of id's in all of my android layouts to an ids.xml file, or to just leave my view code the way it is (with multiple views sharing ids and both views using "#+id/id_name).
Is there a significant compile/runtime performance benefit to refactoring out the ids to the ids.xml file? How about if the application gets bigger?
Related resources:
http://developer.android.com/guide/topics/resources/more-resources.html#Id
Thank you for your time.
I used <item type="id"> resources in my app because I have TextEdit views that serve a similar purpose in more than one Activity.
ids.xml has the following advantage: all ids were declared, so compiler can recognize them. If something like this:
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBelow="#id/text2"
android:text="...."/>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="...."/>
Can result in compiling error because text2 was refered before declared
I have here an EditText which I copied from a dialog example. The textPersonName however doesn't capiltlize like I would expect, which is to put the keyboard into caps before each word in their name. I find myself using textCapWords to achieve this.
<EditText
android:id="#+id/player_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
</EditText>
What is the point of textPersonName and what might I be loosing switching to textCapWords? I thought maybe there was some locale logic behind textPersonName, but it doesn't seem to even work in English like I would expect so I'm not so sure.
Edit
I have also tried android:inputType="textCapWords|textAutoComplete|textPersonName" to see if it would auto complete peoples names, but it doesn't seem to.
You don't lose anything if you stop using it. Currently Android doesn't use this flag at all. At least I checked the source code of the latest Android, and I didn't find any mentioning of this parameter in widgets.
It is possible that this is something that will be used in the future versions of Android, so I would leave it just in case, but you'll do fine without it.
How do I get a Edittext with both a phone input and the ability to hide the string. I know that
android:inputType="textPassword"
hides the string, while
android:inputType="phone"
brings up a dialpad interface.
How to combine the two?
android:password is deprecated, but AFAIK is the only way because android:inputType="phone|textPassword" is ignored ...
<EditText
android:id="#+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:password="true"
android:inputType="phone" />
I believe this is what you want?
android:inputType="numberPassword"
Edit: At the time of the question (2010) this may have not been in the API, but for contemporary development, it's available.
I have not found an appropriate solution to this problem. dtmilano's accepted solution doesn't fully work. If the EditText is focused in landscape mode where you have the full screen keyboard, the numbers still display in clear text, not masked.
I spent significant time going through the actual TextView code, and the reason this is a problem is that they are explicitly checking the InputType against InputType.TYPE_CLASS_TEXT and, if I recall correctly, TYPE_MASK_CLASS. So if you include any other InputType within those bounds (I think the range used by TYPE_CLASS_TEXT and TYPE_MASK_CLASS is the first byte), then it won't be recognized as a password that needs masking.
I know what I said is pretty confusing. The actual code is a LOT more confusing. I was pretty appalled at the TextView's code to be honest. It's a tangled mess, with hard coded checks everywhere. Horrible coding practice which leads to problems like this.
This problem can be solved without using deprecated android:password. See my answer here.
I haven't tried this, but it might be possible to combine the two like so:
android:inputType="textPassword|phone"
since inputType can take on multiple values.