How to hide keyboard when leaving Edittext?

Asked

Viewed 807 times

0

I have three edittext, I want that when I click out of them or finish filling my keyboard sum. Being that they are a Fragment. How to do this?

My XML with editText.

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:paddingBottom="10dp"
        android:background="@drawable/shadow_grey"
        android:id="@+id/cadastroAvaliador"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <AutoCompleteTextView
        android:id="@+id/colaboradoresAmc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:hint="Avaliador">

        </AutoCompleteTextView>


        <AutoCompleteTextView
            android:id="@+id/contratadasAmc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Contratada"
            android:layout_below="@+id/colaboradoresAmc"
            android:layout_alignStart="@+id/colaboradoresAmc">
        </AutoCompleteTextView>


        <EditText
            android:id="@+id/dataRealizada"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/contratadasAmc"
            android:layout_alignStart="@+id/contratadasAmc"
            android:ems="6"
            android:inputType="date"
            android:hint="Data Realizada"
            android:clickable="true"
            android:focusableInTouchMode="true" />

        <Spinner
            android:id="@+id/spinnerTipo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/dataRealizada"
            android:layout_alignTop="@+id/dataRealizada"
            android:layout_alignBottom="@+id/dataRealizada"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/contentTipo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/cadastroAvaliador"
        android:layout_alignEnd="@+id/cadastroAvaliador"
        android:layout_alignStart="@+id/cadastroAvaliador">

    </RelativeLayout>

</RelativeLayout>

When I click on the marked/scribbled area I wanted the keyboard to hide.inserir a descrição da imagem aqui

Thanks in advance.

  • 1

    https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard

  • @acklay is not what I want. I want that after using the keyboard it leaves the screen.

  • 1

    And how do you get out of Edittext? Press a button?! You can edit the question and explain it better, or even exemplify it, or even put some code you think is relevant to your question?!

  • I updated the question @acklay

  • What is this area? In the xml you posted, what is the part that declares this area?

  • It’s the <Relativelayout android:id="@+id/contentTipo". @ramaral

  • This Relativelayout has something inside?

  • It has a listview, I already put the ID for it. But it generates an error. This contentType is where the listview comes in. java.lang.Nullpointerexception: Attempt to invoke interface method 'Boolean android.app.Iactivitymanager.handleApplicationWtf(android.os.Ibinder,

  • 2

    So it becomes complicated to give an answer. First you say it’s outside, then it’s in a certain area, now the area has a Listview. Where we are?

Show 4 more comments

1 answer

3


If you want to hide the keyboard when you click that area set an Onclicklistener for it and use Inputmethodmanager to hide the keyboard.

Put into the method onCreateView after "inflating" the layout.

    RelativeLayout contentTipo = (RelativeLayout) view.findViewById(R.id.contentTipo);
    contentTipo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            hideSoftKeyboard();
        }
    }); 

Substitute view by the name of the variable to which "inflated" the Fragment layout.

Method hideSoftKeyboard():

private void hideSoftKeyboard() {
    InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getView().getWindowToken(),
                                                   InputMethodManager.HIDE_NOT_ALWAYS);
}
  • The first link I posted as a comment would do this, but without the question of clicking on any corner of the screen. But I guess he couldn’t adapt, said it wasn’t that; =)

  • @acklay But in editing the question he talks about clicking out.

  • @ramaral made these changes but nothing has changed. I edited my question

  • @Lucascharles the suggestion of the ramaral solves your problem!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.