[Solved] How to change mic icon with send button when user starts typing in android [closed]


Just add TextChangedListener on EditText:

EditText commentText=(EditText)findViewById(R.id.edit);
    commentText.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) {
            String val=charSequence.toString();
            // chaeck if edittext have any character or empty. then
            // there are two way to handle switch buttons
            //1. Take two button at same position and based on condition just change their visibility
            //2. Just take one button and based on condition just change src image and add Tag text to the button view.
            // Tag text will help to identify click listener action.
            ///****************** first way ******///////
            firstWay(val);
            ///****************** second way ******///////
            secondWay(val);
        }

        @Override
        public void afterTextChanged(Editable editable) {


        }
    });

1ST method ……

    private void firstWay(String val) {
    if (val.isEmpty())
    {
        button1.setVisibility(View.GONE);
        button2.setVisibility(View.VISIBLE);
    }
    else
    {
        button1.setVisibility(View.VISIBLE);
        button2.setVisibility(View.GONE);
    }
}

2nd method…….

    private void secondWay(String val) {
    if (val.isEmpty())
    {
        button1.setImageResource(R.drawable.ic_mic_black_24dp);
        button1.setTag("mic");
    }
    else
    {
        button1.setImageResource(R.drawable.ic_send_black_24dp);
        button1.setTag("send");
    }
}

In case of second way. when you click on button just check the tag value if it is equal to “mic” perform mic related operation else other operation.. like

button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String tagValue=button1.getTag().toString();
            if (tagValue.equals("mic"))
            {
                /// mic related operation 
            }
            else 
            {
                /// other operation 
            }

        }
    });

solved How to change mic icon with send button when user starts typing in android [closed]