Reputation: 75
I know how to hide keyboard if class is extends Activity, but when it extends DialogFragment my code for hide keyboard from Activity is doesn't work.
this is my code so far:
public class PersonalData extends DialogFragment
LinearLayout activity_personaldata;
//Oncreate:
activity_personaldata = (LinearLayout) view.findViewById(R.id._activity_personaldata_);
activity_personaldata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
});
and my XML is :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/_activity_personaldata_"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp">
</ScrollView>
</LinearLayout>
i have make ID in the linearlayout in my XML, and give focusable. but it still dont work. please help me, thanks in advance :)
Upvotes: 0
Views: 272
Reputation: 2806
Try this code in the onCreateView()
. getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
If you want to hide the keyboard when user clicks on outside of your EditText
, you can implement a focusListner
for that EditText
.
youreditText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
});
Upvotes: 1