/* * Copyright (C) 2014 Tubor Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sgu.findyourfriend.screen; import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import com.sgu.findyourfriend.R; public class MyEditText extends EditText { //The image we are going to use for the Clear button private Drawable imgCloseButton = getResources().getDrawable(R.drawable.clear_button_image); public MyEditText(Context context) { super(context); init(); } public MyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); init(); } void init() { // Set bounds of the Clear button so it will look ok imgCloseButton.setBounds(0, 0, imgCloseButton.getIntrinsicWidth(), imgCloseButton.getIntrinsicHeight()); // There may be initial text in the field, so we may need to display the button handleClearButton(); //if the Close image is displayed and the user remove his finger from the button, clear it. Otherwise do nothing this.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { MyEditText et = MyEditText.this; if (et.getCompoundDrawables()[2] == null) return false; if (event.getAction() != MotionEvent.ACTION_UP) return false; if (event.getX() > et.getWidth() - et.getPaddingRight() - imgCloseButton.getIntrinsicWidth()) { et.setText(""); MyEditText.this.handleClearButton(); } return false; } }); //if text changes, take care of the button this.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { MyEditText.this.handleClearButton(); } @Override public void afterTextChanged(Editable arg0) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } }); } void handleClearButton() { if (this.getText().toString().equals("")) { // add the clear button this.setCompoundDrawables(this.getCompoundDrawables()[0], this.getCompoundDrawables()[1], null, this.getCompoundDrawables()[3]); } else { //remove clear button this.setCompoundDrawables(this.getCompoundDrawables()[0], this.getCompoundDrawables()[1], imgCloseButton, this.getCompoundDrawables()[3]); } } }