EditText does not accept numbers Android - android

I am developing an app for Android and I have a problem with my EditText : I can't write numbers on it. I created a function in order to allow my users to valid an answer in an EditText when they click on "OK" but, with this function, they can't write numbers on my EditTexts.
Here is my function's code :
ed.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
ed.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == 66){
if (ed.getText().toString().equals(rep)) {
k++;
if (k == 1) {
Intent ActivityTransition = new Intent(Level6f.this, Transition6.class);
startActivityForResult(ActivityTransition, KEY1);
}
}
else {
ed.getText().clear();
Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(50);
}
}
if (keyCode == 67) {
ed.getText().clear();
}
return true;
}
});
}
});
Here my XML EditText's code :
<EditText
android:layout_width="110dp"
android:layout_height="45dp"
android:inputType="textVisiblePassword"
android:id="#+id/editText"
android:textSize="22sp"
android:layout_above="#id/curseur"
android:layout_centerHorizontal="true"/>

I think you can use
android:imeOptions="actionDone"
in your xml instead of android:inputType="textVisiblePassword"

Use this line in your xml (EditText)
android:inputType="number|numberDecimal"
this line allows number and number decimal.
Example -
<EditText
android:layout_width="110dp"
android:layout_height="45dp"
android:inputType="number|numberDecimal|textVisiblePassword"
android:id="#+id/editText"
android:textSize="22sp"
android:layout_above="#id/curseur"
android:layout_centerHorizontal="true"/>

Create your own ok button. Surround editText with a Tablerow and put the ok button in there.
<TableRow
android:id="#+id/tableRow1"
android:layout_width="110dp"
android:layout_height="45dp"
android:layout_above="#id/curseur" >
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/editText"
android:textSize="22sp"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/okbutton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Ok"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="bold" />
</TableRow>

Related

Button stopped generating View

I'm on a working on a project on Android Studio. Then I would like to implement a button that generate a view above him. And store it's text data in an ArrayList.
This is the java code:
addClassBtn.setOnClickListener((v)->{
View cricketerView = getLayoutInflater().inflate(R.layout.check_box,categoryList,false);
EditText checkBox_title = (EditText)cricketerView.findViewById(R.id.checkBox_title);
switch (appPreference.getInt("lang",0)){
case 0:checkBox_title.setHint(getResources().getString(R.string.en_en_editBox_Text));break;
case 1:checkBox_title.setHint(getResources().getString(R.string.ch_tw_editBox_Text));break;
default:checkBox_title.setHint("Error");
}
temporaryClass.add(checkBox_title.getText().toString());
checkBox_title.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
temporaryClass.remove(checkBox_title.getText().toString());
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
temporaryClass.add(checkBox_title.getText().toString());
}
});
CheckBox checkBox_check = (CheckBox)cricketerView.findViewById(R.id.checkBox_check);
checkBox_check.setOnClickListener((w)->{
});
Button checkBox_delete = (Button)cricketerView.findViewById(R.id.checkBox_delete);
checkBox_delete.setOnClickListener((w)->{
categoryList.removeView(w);
});
categoryList.addView(cricketerView);
});
And this is the XML Layout code of the view that I would like to add above the button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/checkBox_title"
android:layout_width="295dp"
android:layout_height="40dp"
android:layout_marginStart="13dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="5dp"
android:autofillHints="#string/ch_tw_editBox_Text"
android:background="#drawable/rounded_edittext"
android:hint="#string/ch_tw_editBox_Text"
android:inputType="text"
android:paddingStart="13dp"
android:paddingEnd="13dp"
android:selectAllOnFocus="false"
android:textSize="22sp" />
<CheckBox
android:id="#+id/checkBox_check"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:text=""
android:textColorLink="#000000" />
<Button
android:id="#+id/checkBox_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/ic_delete"
android:layout_marginStart="5dp"
android:layout_marginEnd="13dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
Then when I try it, it only generate the view one time and I can't generate more view. I can't even delete the view that was generated.
TestCase:
I already used the code above on an other activity, and I work really good.
Thank you

Move to next Edittext automatically after entering number

I have a layout where i have 4 circular edittext to enter PIN.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/four_pin_background"
android:inputType="number"
android:textAlignment="center"
android:maxLength="1"
android:imeOptions="actionNext"
android:cursorVisible="false"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/four_pin_background"
android:inputType="number"
android:textAlignment="center"
android:maxLength="1"
android:imeOptions="actionNext"
android:cursorVisible="false"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/four_pin_background"
android:inputType="number"
android:textAlignment="center"
android:maxLength="1"
android:imeOptions="actionNext"
android:cursorVisible="false"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/four_pin_background"
android:inputType="number"
android:textAlignment="center"
android:maxLength="1"
android:imeOptions="actionNext"
android:cursorVisible="false"/>
</LinearLayout>
I want like after entering number for first edittext, it should automatically move to next edittext.
Now after tapping on next edittext or after tapping on next button from soft keyboard only i am able to enter number.
Can anyone please help me how to do this?
Follow these steps it will give you your desired result :-
Update your XML with ids in EditTexts like this :-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id ="#+id/firstET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/four_pin_background"
android:inputType="number"
android:textAlignment="center"
android:maxLength="1"
android:imeOptions="actionNext"
android:cursorVisible="false"/>
<EditText
android:id ="#+id/secondET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/four_pin_background"
android:inputType="number"
android:textAlignment="center"
android:maxLength="1"
android:imeOptions="actionNext"
android:cursorVisible="false"/>
</LinearLayout>
Then inside your JAVA Code ,
Take Reference of these EditTexts , and add this code inside your onCreate() method of your Activity like :-
EditText firstET , secondET;
firstET = (EditText) findViewByID(R.id.firstET);
secondET = (EditText) findViewByID(R.id. secondET);
firstET.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// add a condition to check length here - you can give here length according to your requirement to go to next EditTexts.
if(firstET.getText().toString().trim().length() >2){
firstET.clearFocus();
firstET.requestFocus();
}
}
});
Attach a TextWatcher
Check for needed length of editText input and focus next.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (s.length() == PIN_LENGTH) {
focusNext();
}
}
});
You need to use addTextChangedListener
Adds a TextWatcher to the list of those whose methods are called whenever this EditText's text changes.
Sample Code
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// specify length of your editext here to move on next edittext
if(editText.getText().toString().trim().length()>=1){
NexteditText.requestFocus();
}
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
}
});

How to get input multiple time from a text in android?

How can we embed an input text with a TextView in Android?
For example, I want to implement this:
"Your Name is ___ , This is the Level ____"
The "___" is the blank space which the user must fill with a specified input and I'll check it for validation.
Please note that it is different from android:hint.
How can I implement this?
You could make a fill in the blanks type format using constant text inside EditText. Do this by creating an EditText in XML.
<EditText
android:id="#+id/editText"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
In your Java class add the following code:
final EditText edittext = (EditText) findViewById(R.id.editText);
edittext.setText("Something ");
Selection.setSelection(edittext.getText(), edittext.getText().length());
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) { if(!s.toString().contains("Something ")){
edittext.setText("Something ");
Selection.setSelection(edittext.getText(), edittext.getText().length());
}
}
});
This is output of the code. Here the string Something is static and cannot be removed.
You could hide the underline in Edittext using android:background="#android:color/transparent". I would also recommend you to disable enter in the EditText by using the code:
<EditText
android:id="#+id/editText"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxLines="1" <----- add this
android:inputType="text"/> <------ and this to disable enter in edittext
You can try like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textSize="16sp"
android:textColor="#000000"
android:text="Your Name is"
android:id="#+id/textview1" />
<EditText
android:layout_width="60dp"
android:textSize="16sp"
android:layout_height="40dp"
android:textColor="#000000"
android:id="#+id/edittext1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textSize="16sp"
android:textColor="#000000"
android:text=", This is the Level"
android:id="#+id/textview2" />
<EditText
android:layout_width="60dp"
android:textSize="16sp"
android:layout_height="40dp"
android:textColor="#000000"
android:id="#+id/edittext2"/>
</LinearLayout>

Content from EditText does not appear in TextView.

I am completely new in android but I thought I at lease learnt how to write simple code to have contents from EditText appear in the TextView. But seems like not. Can anyone help? Thank you in advance. Here is my code.
detailsActivity.java
public class DetailsActivity extends ActionBarActivity {
EditText etTop;
TextView tvTop;
String cTop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_activity);
//imageUri = Uri.parse(extras.getString("imageUri"));
Bitmap bitmap = getIntent().getParcelableExtra("id");
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(bitmap);
etTop = (EditText) findViewById(R.id.etTop);
tvTop = (TextView) findViewById(R.id.tvBottom);
cTop = etTop.getText().toString();
tvTop.setText(cTop);
}
}
details_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#b72828">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
<EditText
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/etTop"
android:hint="Enter Top Text"
android:textColor="#color/white"
android:clickable="true"
android:gravity="center"
android:inputType="textCapWords"
android:maxLines="2"
android:textColorHint="#fdfdfd"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="#+id/tvBottom"
android:layout_above="#+id/etBottom"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create"
android:id="#+id/cButton"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:clickable="false" />
<EditText
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/etBottom"
android:hint="Enter Bottom Text"
android:textColor="#color/white"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:gravity="center"
android:inputType="textCapWords"
android:maxLines="2"
android:textColorHint="#fdfdfd"
android:textSize="30dp"
android:textStyle="bold"
android:layout_above="#+id/cButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="#+id/tvTop"
android:layout_below="#+id/etTop"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Reason:
cTop = etTop.getText().toString();
tvTop.setText(cTop);
gets executed as soon as activity is created. By this time you don't get any chance to enter any text in EditText. When yo do, this code has been already executed. So you need to get the text from EditText and set it into TextView when you actually enter the text.
Solution:
Remove above mentioned two lines of code and add this
etTop.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
tvTop.setText(s.toString());
}
public void beforeTextChanged(CharSequence s, int start,int count, int after) {}
public void onTextChanged(CharSequence s, int start,int before, int count) {}
});
Now every time you will change the text in EditText, TextView will be update automatically.
try doing this:
etTop.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
tvTop.setText(s);
}
#Override
public void afterTextChanged(Editable s) {
//.. Or set it here if this is desired behavior
}
});
Your code doesn't listen for changes to the EditText instead your code is saying, go get me the text right now, instead of listening for text being added into the edit text.
So having said that drop these lines:
cTop = etTop.getText().toString();
tvTop.setText(cTop);
and add the above snippet of code in its place.
Good Luck and Happy Coding!

EditText value is reversed

I've got an odd one. I'm trying to format some text while the user types into an EditText field. I am using the TextWatcher and responding to afterTextChanged event (code below). What is odd is after the first time this logic runs the text starts to become reversed. It seems that the Editable object contains the string backwards. Does anyone have any idea how to fix this?
_textWatcher = new TextWatcher()
{
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
}
#Override
public void afterTextChanged(Editable editable)
{
Editable e = textView.getText();
String text = e.toString();
logger.d("[FormListAdapter][addTextChangedListener] Format Text: " + text);
Number numVal = FormFieldBusiness.ConvertStringToNumber(text, formField.GetFormFieldType());
String fText = FormFieldBusiness.GetFormattedValue(numVal, formField.GetFormFieldType());
editText.removeTextChangedListener(_textWatcher);
textView.setText(text);
editText.addTextChangedListener(_textWatcher);
}
};
editText.addTextChangedListener(_textWatcher);
* UPDATE *
In an attempt to help anyone out there who's looking at this here is my XML layout file.
As I mentioned before the text is correct until after the first the time setText method is called. After that the Editable object is reversed.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="120dp">
<!-- Top Layout -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:weightSum="10"
android:id="#+id/TopLayout">
<TextView
android:id="#+id/TitleTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center_vertical"
android:text="Headling Rent (pa)"
android:textSize="16dp"
style="#style/KelFormListItemLabel" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:weightSum="2"
android:background="#color/formListItemValueBackgroundColor">
<ImageView
android:id="#+id/CurrencyIconView"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_weight="0"
android:src="#drawable/icon_currency_pound"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp" />
<EditText
android:id="#+id/ValueTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="right|center_vertical"
android:text="20,000"
android:textSize="16dp"
android:inputType="number"
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:textIsSelectable="true"
style="#style/KelFormListItemValue"/>
</LinearLayout>
</LinearLayout>
<!-- Bottom Layout -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:weightSum="10"
android:id="#+id/BottomLayout"
android:layout_below="#+id/TopLayout">
<TextView
android:id="#+id/BottomTitleTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center_vertical"
android:text="Headling Rent (pa)"
android:textSize="16dp"
style="#style/KelFormListItemLabel" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:weightSum="2"
android:background="#color/formListItemValueBackgroundColor">
<ImageView
android:id="#+id/BottomCurrencyIconView"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_weight="0"
android:src="#drawable/icon_currency_pound"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp" />
<EditText
android:id="#+id/BottomValueTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="right|center_vertical"
android:text="20,000"
android:textSize="16dp"
android:inputType="number"
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:textIsSelectable="true"
style="#style/KelFormListItemValue"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
The issue is you are setting the text in the EditText field (even though you have removed the listener) you are listening for. this is causing the cursor to move to the beginning of the line, therefore making it type backwards
try changing from a TextWatcher to TextView.OnEditorActionListener and listen for the action key set on the EditText so when they hit the action key, it does your desired functionality.
add
android:imeOptions="actionDone"
to your button xml and assign it with
TextView.OnEditorActionListener editingActionListener = new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) || actionId == EditorInfo.IME_ACTION_DONE) {
//do stuff where you set the text here
}
return true;
}
};
editText.setOnEditorActionListener(editingActionListener);
alternatively, you can use OnFocusChangeListener
View.OnFocusChangeListener focusChangeListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.getClass().equals(EditText.class)) {
if (!hasFocus) {
//do your stuff here
}
}
}
};
editText.setOnFocusChangeListener(focusChangeListener);
Simply move the cursor to the correct position in your TextWatcher after setting the EditText content:
#Override
public void afterTextChanged(Editable editable)
{
...
int position = editText.getSelectionStart();
editText.setText(text);
editText.setSelection(position);
...
}

Categories

Resources