If you don't use ScrollView, even if you're focused on EditText, tap anything other than the EditText portion of the screen to remove the focus of EditText.However, if you put ScrollView in LinearLayout, you won't be able to remove the focus of EditText in ScrollView.How can I remove the focus even within ScrollView?
Please let me know.
I am using Android studio.
xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android: orientation="vertical"
tools:context=".MainActivity"
android: id="@+id/main_layout3">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android: orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constrainBottom_toBottomOf="parent"
app:layout_constrainLeft_toLeftOf="parent"
app:layout_constrainRight_toRightOf="parent"
app:layout_constrainTop_toTopOf="parent"/>
<EditText
android: id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android: inputType="numberDecimal"/>
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="18sp"
app:layout_constrainBottom_toBottomOf="parent"
app:layout_constrainLeft_toLeftOf="parent"
app:layout_constrainRight_toRightOf="parent"
app:layout_constrainTop_toTopOf="parent"/>
</LinearLayout>
This is java.
public class MainActivity extensions AppCompatActivity{
// OBJECT FOR CONTROLLING KEYBOARD DISPLAY
public InputMethodManager inputMethodManager1;
// Background Layout
private LinearLayout mainLayout;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// OBJECT FOR CONTROLLING KEYBOARD DISPLAY
inputMethodManager1=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mainLayout=(LinearLayout) findViewById (R.id.main_layout3);
}
@ Override
public boolean onTouchEvent (MotionEvent) {
// hide one's keyboard
inputMethodManager1.hideSoftInputFromWindow(mainLayout.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
// shift focus to the background
mainLayout.requestFocus();
return true;
}
}
Thank you for your reply.We have added android:descendantFocusability="beforeDescendants"
to ScrollView as instructed.
It is true that when I tap Hello World outside ScrollView, the focus is off and the keyboard disappears, but when I tap Hello World inside ScrollView, the focus was not off.The keyboard is displayed as it is.I'm sorry for my lack of explanation.I want to remove the focus and erase the keyboard even if I tap another part of ScrollView (e.g., TextView).
Thank you for your cooperation.
In the ScrollView
of LinearLayout
, add android:descendantFocusability="beforeDescendants"
.
© 2024 OneMinuteCode. All rights reserved.