How to display the numeric keyboard directly in the Android [duplicate] - java

I just basically want to switch to the number pad mode as soon a certain EditText has the focus.

You can configure an inputType for your EditText:
<EditText android:inputType="number" ... />

To do it in a Java file:
EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);

I found this implementation useful, shows a better keyboard and limits input chars.
<EditText
android:inputType="phone"
android:digits="1234567890"
...
/>
Additionally, you could use android:maxLength to limit the max amount of numbers.
To do this programmatically:
editText.setInputType(InputType.TYPE_CLASS_PHONE);
KeyListener keyListener = DigitsKeyListener.getInstance("1234567890");
editText.setKeyListener(keyListener);

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
android:inputType="number|phone"/>
will show the large number pad as dialer.

You can do it 2 ways
Runtime set
EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
Using XML
<EditText
...
android:inputType="number" />

If you need fractional number, then this is the answer for you:
android:inputType="numberDecimal"

If you are using your EditText in Dialog or creating dynamically and You can't set it from xml then this example will help you in setting that type of key board while you are using dynamic Edit Text etc
myEditTxt.setInputType(InputType.TYPE_CLASS_NUMBER);
where myEditTxt is the dynamic EDIT TEXT object(name)

editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);

Use the below code in java file
editText.setRawInputType(Configuration.KEYBOARD_QWERTY);

Below code will only allow numbers "0123456789”, even if you accidentally type other than "0123456789”, edit text will not accept.
EditText number1 = (EditText) layout.findViewById(R.id.edittext);
number1.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_CLASS_PHONE);
number1.setKeyListener(DigitsKeyListener.getInstance("0123456789”));

More input types and details found here on google website

Define this in your xml code
android:inputType="number"

EditText number1 = (EditText) layout.findViewById(R.id.edittext);
number1.setInputType(InputType.TYPE_CLASS_NUMBER);

Related

Android ellipsize EditText

So i want to cut off the end of an EditText after the User wrote something in a single line which is longer than the width of the edittext. If the user clicks/touches something else like f.e. the next Edittext, the text of the first EditText should be cut of and displayed with "...".
In your xml file on editText add this attribute
android:ellipsize="end"
If you are using ConstraintLayout ,android:ellipsize="end" attribute will work if you set "layout_constraintEnd_toEndOf="parent or your end constraint" to the Textview.
<Textview android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
android:ellipsize="end"
app:layout_constraintEnd_toEndOf="parent
app:layout_constraintTop_toTopOf="parent"/>

How can I force my textview to display the full text on a single line?

I have a TextView in my android app that I want to display all the text on a single line. Right now, it is displayed like this:
this is som
e text
When I set setSingleLine(), I get this:
this is som e text
(notice the space within some)
When I use setMaxLines(1), I get this:
this is som
What I want is this:
this is some text
How can I force the text to display fully on a single line?
You can try this on your textview's xml:
android:maxLines="1"
You can make scroll text using following code if you want.
<TextView
android:id="#+id/txtBigLongTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold"
tools:text="long text goes here......................"/>
The answers above provide a solution to my issue.
The problem was the previous programmer had implemented logic which automatically started a new line after 20 characters.'
Thanks for your help everyone!

maxLength property is not working on EditText

I have the following EditText where I have set maximum character limit to 10, but somehow it is not working. It accepts more than 10 characters.
<EditText
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left|center"
android:hint="Enter Full Name *"
android:maxLength="10"
android:padding="5dip" />
Please helps, thanks in advance.
Make sure that you don't have any InputFilter set to the EditText by code.
You should comment this line of code.
editText.setFilters(new InputFilter[] { filter });
You can try with this. This is the simple hack around this problem if you are setting it from xml.
Internally if your suggestion is switched on then as per dictionary its not able to judge the word character length. So If it has been switched off then maxlength will work and Android will understand it as own written word,where no dictionary should be applicable.
android:inputType="textNoSuggestions|textVisiblePassword"
android:maxLength="10"

Set EditText lines and position in Java?

Is there a way to set the lines of an EditText in Java? Example
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="5"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollHorizontally="false"
/>
So far I have this
EditText et = new EditText(getActivity());
et.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
obviously I need still need to set
android:lines="5"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollHorizontally="false" <-- may not be important, to be decided
Setting an Id is not important in this scenario, however making it appear as an EditText "Box" or "Area" is. I searched quite a bit for some information on this and found nothing. Either I searched for the wrong terms or this isn't possible. Any help is appreciated.
Not sure if this is what you are after. But you can set the attributes in Java as follows...
android:lines="5" as setLines(5);
android:gravity="top|left" as setGravity(Gravity.TOP | Gravity.LEFT)
android:inputType="textMultiLine" as setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE)
android:scrollHorizontally="false" as setHorizontallyScrolling(false)
See TextView docs
Is there a way to set the lines of an EditText in Java?
Yes there is. Try setLines(int lines).
Gravity: setGravity(int gravity)
InputType: setRawInputType (int type)

How to programmatically set edit text hint in android?

Is there a way to set the text hint displayed inside of an android edittext with java? I would like for my app to be able to display different things in the edittext at different times.
Did you try the setHint?
myTextView.setHint("My conditional hint");
Hope it helps..
If you are using simple Editext without TextInputLayout :
EditText etUsername;
etUsername = (EditText) findViewById(R.id.etUsername);
etUsername.setHint("Your Hint");
If you are using Editext within TextInputLayout then you need to change property of TextInputLayout not Edittext:
TextInputLayout layoutUser;
layoutUser = (TextInputLayout) findViewById(R.id.inputLayoutUname);
layoutUser.setHint(getResources().getString(R.string.hintFaculty));
Call setHint() on the EditText.
((TextView) findViewById(R.id.yourEditTextId)).setHint("hint text");
Kotlin
editText.hint = "Your hint"
if you are using actionbarsherlock searchview, this is the best way to set string dynamically
AutoCompleteTextView searchTextView =
(AutoCompleteTextView) mSearchView.findViewById(R.id.abs__search_src_text);
searchTextView.setHint(getResources().getString(R.string.search));
In Kotlin
val layoutMobile: TextInputLayout = findViewById<View>(R.id.inputLayout_MobileNumber) as TextInputLayout
layoutMobile.hint = "Your Hint"
for editText.hint you must delete name of your edit.Text from activity_main.xml then set in the MainActivity in java or activity_main.xml because text is preferred to hint.
<EditText
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine|textLongMessage"
android:hint="\n"/>
To be honest I don´t understand why.. but including "\n" as hint in the xml did the trick!
Now you can call setHint from Java code:
searchEditText.setHint(getString(R.string.search_hint));
and remove the \n from strings.xml if you want to allow the text split automatically according to the room
<string name="search_hint">keyword, location, max price (e.g. 1K, 1M)</string>
in Java
yourEditTex.setText("");//First
yourEditTex.hit("Your text");
this for alls cases.
You can try following,
in Java
yourEditText.setHint("hint");
in Kotlin
yourEditText.hint = "Your hint"

Categories

Resources