SoFunction
Updated on 2025-03-03

Various pitfalls encountered in Android UI development

1. The soft keyboard hides the problem

  • Problem description: After the Activity presses the return call to the finish() method, the interface has been destroyed, but the soft keyboard is still left on the screen, which makes it impossible to see the activity currently displayed without the input box, which is very serious visual impact.
  • Try the solution: Find various ways to hide the soft keyboard, and find them online. The idea is that when the activity exits, the onDestroy method will be called to destroy the interface, and you can find a way to hide the interface in this method. I found the following method, but it still doesn't work. Also tried to find all edittexts with base classes and then let them lose focus, hiding the soft keyboard.
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
  • Solution: At the beginning, the idea of ​​typing hidden soft keyboards in onDestroy() was wrong, because onDestroy() had two life cycle methods before. For example, the above method of hiding soft keyboards has a getCurrentFocus(). Before onDestroy(), the control that correctly obtains the current focus will definitely not be obtained. Therefore, hiding the soft keyboard in the onPasue() method is effective, and no method is used in the onDestroy() method is invalid.
  • Note: Using this method of hiding the soft keyboard, it is best to make a null judgment, otherwise an exception with a null pointer may occur. If there is no control on the current interface to obtain focus, the getCurrentFocus() method gets a null.
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
if(getCurrentFocus!=null)
(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
  • The soft keyboard occupies layout problem. The soft keyboard sometimes overwrites some controls. How to push up the entire interface so that no control is overwritten? There are two steps, the first is to set a property in the activity, as follows. The second step is to add a scrollview to the layout to put the view you want to be pushed up here, and when the soft keyboard is displayed, it will scroll in the scrollview to obtain space to display the soft keyboard.

<activity Android:windowSoftInputMode="adjustResize">

Notes on tags

  • The merge tag is only useful if the root layout is FrameLayout, because the root layout of all Android interfaces is FrameLayout, so you can use the merge tag for fusion.
  • After the merge tag is used, even if there is EditText in the layout, the focus cannot be automatically obtained. You can only manually set the focus and call the requestFocus() method. Or use requestFocus in the XML layout file.
  • After using it, please note that if it is in the root layout, you cannot use LayoutInflater to generate a view, otherwise the following error will be reported. Since I used this layout with merge in the getview() of the listview, I crashed. To add, in the inflater() method, you can set attach root to true to parse it without crashing.
 : Binary XML file line 
 #2: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
   Caused by: : <merge /> can be used only with a valid ViewGroup root and attachToRoot=true

Note

  • Linear layout is horizontal by default, so make good use of the weight weight attribute.
  • A very important point, if the direction is set to horizontal, the top and bottom tags of layout_gravity have no effect. If the direction is set to vertical, left and right have no effect. At this time, if you want to place it to the right, you can use the space tag, set the width to 0dp, and set layoutweight to 1 to put it in front of the control.

4. Layout selection

FrameLayout is the simplest layout. The root layout of all Android interfaces is FrameLayout, which loads the fastest.

The loading speed of LinearLayou layout and RelativeLayout layout requires further in-depth research to draw specific conclusions.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links