In this AutoCompleteTextView i am able to enter all alphabets and special characters, just not numbers. Issue is observed on lollipop and above. works fine on kitkat
<AutoCompleteTextView
android:id="#+id/editTextSearchView"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_toLeftOf="#+id/searchclear"
android:layout_marginBottom="20dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:dropDownHeight="160dp"
android:gravity="center_vertical"
android:hint="Enter Location"
android:imeOptions="actionDone"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingTop="12dp"
android:singleLine="true"
android:textColor="#color/theme_color"
android:textColorHint="#color/theme_color"
android:textCursorDrawable="#null" />
textWatcher used on the edit text box :
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if(editable.length() > 0)
autoCompleteClearButton.setVisibility(View.VISIBLE);
else
autoCompleteClearButton.setVisibility(View.GONE);
}
});
try using the android:inputType property in the layout file, set it to number or text
add this line in your xml
<AutoCompleteTextView
android:id="#+id/editTextSearchView"
......
android:inputType="number"
.......
...... />
Related
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 add icon hint edittext in center
I have no idea to set that
and in xml file is not have android:drawbleCenter
How to create it
thanks for your help ;)
Unfortunately you can not set drawable at the center of the EditText like this, Alternatively, you can use background image which includes your hint + image what ever you display and inside,
Try below example.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#android:drawable/editbox_background"
android:gravity="center">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:drawableLeft="#android:drawable/ic_menu_search"
android:gravity="center"
android:hint="Search" />
</RelativeLayout>
You can achieve this type of view using above XML code
Add android:gravity="center_horizontal" to EditText
I have a solution
This is xml:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<EditText
android:id="#+id/edt_search"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/text_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableLeft="#android:drawable/ic_menu_search"
android:drawablePadding="8dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:text="Search"
android:textColor="#969696" />
</FrameLayout>
in onCreate()
edtSearch.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) {
textIcon.setVisibility(View.GONE);
}
#Override
public void afterTextChanged(Editable s) {
textIcon.setVisibility(s.length() == 0 ? View.VISIBLE : View.GONE);
}
});
Try it!
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!
Here is the problem with double hint - TextInputLayout and EditText double hint issue
I need to do the same. I need two hints. One is a title of EditText, and second is a possible value if nothing wasn't input.
But unfortunately there is only one tag:
android:hint
Can I do something with it?
UPD1:
The answer was to set hint for EditText programatically and hint for TextInputLayout in xml.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:hint="#string/prompt_quantity"
android:layout_height="wrap_content">
<EditText
android:id="#+id/quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLines="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
And
quantityEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
quantityEditText.setHint(hasFocus ? "0" : "");
}
});
With the Material Components library you can use
android:hint="Label" to add a floating label
app:placeholderText="Placeholder Text" to add a placeholder in the EditText.
Use something like:
<com.google.android.material.textfield.TextInputLayout
android:hint="Label"
app:placeholderText="Placeholder Text"
..>
<com.google.android.material.textfield.TextInputEditText />
</com.google.android.material.textfield.TextInputLayout>
Note: it requires at least the version 1.2.0-alpha03.
Just check your input and change the hint depending on that:
EditText editText = (EditText)findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher()
{
#Override
public void afterTextChanged(Editable s)
{
if(editText.getText().toString().equals(""))
editText.setHint("");
else
editText.setHint("Your normal hint");
}
});
Try this way
xml layout
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/non_active_edit_text"
android:focusable="false"
android:gravity="right|center_vertical"
android:hint="Right"
android:paddingTop="4dp"
android:background="#null"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/active_edit_text"
android:hint="Left"
android:layout_gravity="center_horizontal"/>
</merge>
JAVA class
public class TwoHints extends RelativeLayout{
EditText activeField;
EditText nonActiveEditText;
final CharSequence hint;
public TwoHints(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.two_hints_edit_text, this);
activeField = (EditText)findViewById(R.id.active_edit_text);
nonActiveEditText = (EditText)findViewById(R.id.non_active_edit_text);
hint = nonActiveEditText.getHint();
activeField.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) {
nonActiveEditText.setHint(s.length() !=0 ? "" : hint);
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
editetext for xml
<you.package.TwoHints
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
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>