Posts

Showing posts with the label Edit Text

Edit text with clear button in Android app

This simple example would help you to create a edit text with clear button EditText editText = (EditText) findViewById(R.id.Editdob); drawable created to assign in edit text (in Activity/Fragment) final Drawable drawable = getResources().getDrawable( R.drawable.clear_edit_text); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); editText.setCompoundDrawables(null, null, drawable, null); ImageView crossImageView = new ImageView(this); crossImageView.setImageDrawable(drawable); On touch listener for edit text (in Activity/Fragment) editText.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { if (arg1.getX() > editText.getWidth() - drawable.getIntrinsicWidth() - 10) editText.setText(""); } return false; } });

Input filter (space not allowed) for EditText

This example which prevent the user to enter blank space in edit text field in Android app  EditText.setFilters(new InputFilter[] { filter }); InputFilter filter = new InputFilter() { public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { for (int i = start; i < end; i++) { if (Character.isSpaceChar(source.charAt(i))) { return ""; } } return null; } }