Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get EditText maxLength setting in code

I would like to see the maxLength of an EditText at run time to be able to make a text display decision.
Is that possible?

Here is a description of what I wan't to do.

I have a ListView with many rows and each row have an EditText and a TextView.
I've made a subclass of ArrayAdapter to be able to feed the String that I want to place in the EditText of each row.
I have set android:maxLength="12" in the XML file.
I want to display a number in that EditText field, but if the number I want to display has more than android:maxLength="12" I want to display an "error message" instead.

And I would prefer not to hard code that 12 in my subclass of ArrayAdapter.

There is probably a simple solution, but I haven't found it yet.
(android first time...)

like image 337
Vincent Bernier Avatar asked Nov 09 '11 17:11

Vincent Bernier


People also ask

How do I edit EditText Uneditable?

If you want your disabled EditText to look the same as your enabled one, you should use android:enabled="false" in combination with android:textColor="..." . Show activity on this post. Used this if you have an icon to be clickable, this works for me.

How do I limit text size on android?

use an input filter to limit the max length of a text view. This is very useful if someone already made some InputFilter . It overrides android:maxlength in xml file, so we need to add LengthFilter this way.


1 Answers

Only limited parameters have their getters, so I don't think you can read it .

So write length (Say 12) in values folder and use it in xml layout and arrayAdapter . Now its not hard-coded .

1)Create integer.xml in values *

<?xml version="1.0" encoding="utf-8"?> <resources>     <item type="integer" name="max_length">12</item> </resources> 

2)In layout

<TextView  android:id="@+id/tv"     android:layout_width="fill_parent"      android:layout_height="wrap_content"     android:maxLength="@integer/max_length"     /> 

3) in ArrayAdapter :

int maxLength = getResources().getInteger(R.integer.max_length); 
like image 146
Shailendra Singh Rajawat Avatar answered Oct 14 '22 08:10

Shailendra Singh Rajawat