1

I have a chat function built into my app that I am having problems with. Here is the XML:

<LinearLayout android:id="@+id/chat_window" android:orientation="vertical">
    <ListView
        android:id="@+id/received_chats"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll">
    </ListView>
    <LinearLayout android:id="@+id/control_window" android orientation="horizontal">
        <EditText android:id="@+id/chat_entry_window" />
        <Button android:id="@+id/send_chat_button" android:text="Send" />
    </LinearLayout>
</LinearLayout>

On the back end, I have an onClickListener control that fires when the button is clicked:

public class Chat extends Fragment {

    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        rootView = inflater.inflate(R.layout.chat, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View rootView, Bundle, savedInstanceState){
        super.onViewCreated(rootView, savedInstanceState);
        displayChats();
    }

    @Override
    public void onResume(){
        super.onResume();
        displayChats();
    }

    @Override
    public View getView(){
        Button sendChatButton = (Button) rootView.findViewById(R.id.send_chat_button);
        sendChatButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                // get EditText contents and send the chat ...
            }
        });
    }

    public void dispalyChats(){
        final ArrayAdapter adapter = new CustomArrayAdapter(getActivity, R.layout.line_of_chat_text, MainActivity.chatList);
        final ListView list = (ListView) rootView.findViewById(R.id.received_chats);
        list.smoothScrollToPosition(list.getCount() - 1);
    }
}

My problem is that when I click the "Send" button, the soft keyboard doesn't disappear and the text I entered into the EditText window does not get cleared.

I have tried this method to get the soft keyboard to disappear, but it doesn't seem to work. However, I may be trying it in the wrong place.

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

What do I need to do in order to make these two things happen?

Brian
  • 1,540
  • 2
  • 20
  • 42

2 Answers2

2

PLease try the following in your button on click listener

    @Override
        public View getView(){
            Button sendChatButton = (Button) rootView.findViewById(R.id.send_chat_button);
            sendChatButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
                   //Hide:
                  imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
                 //toclear edit text 
                  EditText.setText("");

                }
            });
        }
Mightian
  • 6,853
  • 3
  • 37
  • 50
1

See this question for hiding the keyboard programmatically.

To reset an EditTexts content simply call EditText.setText("");

Community
  • 1
  • 1
Jonas Köritz
  • 2,494
  • 18
  • 32
  • 1
    Thanks for the input, it fixed my issue. Like I had said in my original post, the method for hiding the keyboard that you suggested did not work for me. As it turns out, Android Studio was not correctly deploying my code changes to my device. What I had to do was uninstall the app and reinstall it so the hiding method would finally work. Thanks again. – Brian Apr 19 '16 at 15:08