2

My note application has 2 fragments on a screen: A list of notes fragment and a note detail fragment to display the selected note in the second fragment, it has an input text field (android:inputType="textMultiLine"). As I want to handle event keyboard hidden so I can save the change user has made when they close the keyboard. Can anyone give me a clue to do this task?

2
  • by default the android soft keyboard will provide you a done key. Onclick of this done key, keyboard automatically hides Commented Jul 16, 2012 at 7:06
  • But i can't use 'done' key because the text field is multiple line and i must use the 'enter line' key instead . can you help me more? Commented Jul 16, 2012 at 7:25

3 Answers 3

3

I think that the best and universal solution for keyboard issue, is measure the VisibleDisplayFrame on layout change:

Rect visibleRect = new Rect(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup mMain = (ViewGroup) inflater.inflate(R.layout.select_cover_album, container, false); mMain.getWindowVisibleDisplayFrame(visibleRect); mMain.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { Rect r = new Rect(); mMain.getWindowVisibleDisplayFrame(r); if(r.height() < visibleRect.height()){ //keyboard shown } if(r.height() == visibleRect.height()){ //keyboard hidden } } }); return mMain; } 
Sign up to request clarification or add additional context in comments.

Comments

2

Here's how i handle it on my Note Detail fragment:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { this.view = inflater.inflate(R.layout.fragment_note_detail_of_style_searching, container , false); listviewFilter = (ListView) view.findViewById(R.id.listview_filter); noteDetailView = view.findViewById(R.id.note_detail_of_style_view); photoDetailView = view.findViewById(R.id.gv_note_detail_photo); view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int heightDiff = view.getRootView().getHeight() - view.getHeight(); if (heightDiff > 200) { isWatchingKeyboard = true; photoDetailView.setVisibility(View.GONE); return; //must exit now } if(isWatchingKeyboard){ isWatchingKeyboard = false; viewForFocus.requestFocus(); photoDetailView.setVisibility(View.VISIBLE); } } }); return this.view; } 

2 Comments

The above works for me, but be careful of the magic number 200 on bigger devices, such as Nexus 5. The value was 217.
1 - not that clean, 2 - what isisWatchingKeyboard?
0

I did it this way:

private int prevBottom; @Override public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_status, container, false); prevBottom = view.getBottom(); view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (view.getBottom() > prevBottom) { view.requestFocus(); } prevBottom = view.getBottom(); } }); return view; } 

and added this to the view I was inflating:

android:focusableInTouchMode="true" 

and this to the activity in the AndroidManifest.xml

android:windowSoftInputMode="adjustResize" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.