Clickable connectivity in EditText that displays a click view

I use TextWatcher to validate input and want my red tooltip image to be clickable, so I can give the user visual feedback as shown in the image below. What I have so far is the edit text with a red tooltip that appears when entering invalid data. Now I want to know how to make this red tooltip clickable and make a tooltip as shown.

EditText hint

This is the code that I still have,

public class MainActivity extends Activity implements TextWatcher{

    EditText username,email = null;
    boolean flag=false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);      

        username=(EditText) findViewById(R.id.usernametxt);
        email=(EditText) findViewById(R.id.emailtxt);

        username.addTextChangedListener(this);

        username.setOnTouchListener(new View.OnTouchListener(){
        public boolean onTouch(View view, MotionEvent motionEvent) {
            // your code here...
            if(flag)
            {
                email.setText("Error. . .");
            }
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);                
            return false;
            }
        });

        Button loginbtn = (Button) findViewById(R.id.login);
        loginbtn.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                //Check Validations
                if(username.getText().toString().equalsIgnoreCase(""))
                {
                    username.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.alert, 0);
                    flag=true;
                }
            }
        });

        Button clearbtn = (Button) findViewById(R.id.clear);
        clearbtn.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                username.setText("");
                email.setText("");      
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub      
        username.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        flag=false;
        email.setText("");
    }   
}
+5
source share
2 answers

To get the first function, you must create a custom one EditText.

Solved here

drawable PopupWindow

.

+5

, ​​ , EditText usernametxt. .

EditText username = (EditText) findViewById(R.id.usernametxt);
username.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP) {
            if(event.getRawX() >= username.getRight() - username.getTotalPaddingRight()) {
                // your action for drawable click event

             return true;
            }
        }
        return true;
    }
});

, , if:

if(event.getRawX() <= username.getTotalPaddingLeft())

, .

username.getTotalPaddingTop()
username.getTotalPaddingBottom()

, . TextView, Button ..

Android.

+3

All Articles