Devrath Devrath - 3 years ago 144
Android Question

closing keyboard on click of outside edittext in android issue

What i have::


  • I have two containers each of them have a edittext Widget

  • When i click the edit text the keyboard popps up

  • I want to close the edit text on focus changed from the editText(if i
    click outside the edit text)



What is happening:


  • Edittext is not closing



This problem is not there in case of single container, How can i resolve this




MainActivity.java

public class MainActivity extends Activity {

FragmentTransaction ft;
Fragment fragment;

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


//Load header and body fragment to the container
ft = getFragmentManager().beginTransaction();
Fragment fragHeader = ContainerOne.newInstance();
Fragment fragBody = ContainerTwo.newInstance();

//Header
ft.replace(R.id.containerOne, fragHeader, ContainerOne.class.getSimpleName());
//Body
ft.replace(R.id.containerTwo, fragBody, ContainerTwo.class.getSimpleName());
ft.commit();
}
}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
android:id="@+id/containerOne"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>

<FrameLayout
android:id="@+id/containerTwo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>

</LinearLayout>


ContainerOne.java

public class ContainerOne extends Fragment {

EditText editText1;


public static ContainerOne newInstance(){
Log.d(ContainerOne.class.getSimpleName(), "newInstance");
ContainerOne fragment = new ContainerOne();
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

//Make sure the keyboard only pops up when a user clicks into an EditText
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.container_one, container, false);
editText1=(EditText) rootView.findViewById(R.id.editText1);
return rootView;
}

@Override
public void onStart() {
super.onStart();


editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
}


public void hideKeyboard(View view) {
//Hiding the keyboard onClick of outside the edittext
InputMethodManager inputMethodManager =(InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}



}


container_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>

</LinearLayout>


container_two:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>

</LinearLayout>


ContainerTwo.java

public class ContainerTwo extends Fragment {

public static ContainerTwo newInstance(){
Log.d(ContainerTwo.class.getSimpleName(), "newInstance");
ContainerTwo fragment = new ContainerTwo();
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.container_two, container, false);

return rootView;
}
}


How can I resolve this !

I have added all the code


Answer Source

Add a click listener on your container (outside your edit text) and call your hideKeyboard method

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download