AppCompatEditText focus Issue - android

I have long form (LinearLayout as root) which consist of EditTexts and TextView.
Now Issue i am facing is : After clicking the Edit Text, if i clicking on textviews then the focus of an edit text is not clearing. Hence, screen scroll to that edit text due to focus.
I have tried solution - android:focusableInTouchMode="true" in parent layout but no luck.
I have attached an image showing an issue. I have clicked on consulting doctor field which is a TextView but focus is till on email Edit text.
Please help me with some solution. Thanks.

After clicking the Edit Text, if i clicking on textviews then the focus of an edit text is not clearing. Hence, screen scroll to that edit text due to focus.
Wrap your EditText inside FrameLayout as shown below
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>

public static void hideSoftKeyboard (View view)
{
InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
//Call this method on textview click

Related

Scroll nestedscrollview to bottom of textView

I have some editText in my activity so put them into nestedScrollView and my activity has the adjustResize attribute.
I put a textView at bottom of each editText to show the exitText input error.
But their visibility are Gone.
When the soft-keyboard open I don't have a problem whit the first exitText, when I click on Next button of the soft-jetboard nestedScrollView scrolled to the bottom of next editText and if user write a wrong input to my editText the textView of them will be visible and show the error of input to the user.
BUT PROBLEM IS.
They are below of their editText.
How I can scroll nestedScrollView to the bottom of the textView?
You could just use TextInputEditText and TextInputLayout instead of EditText and TextView.
The scrolling behavior will be managed by Android's system and should result correctly.
Also, it will simplify your code:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/text_input_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hint"/>
</com.google.android.material.textfield.TextInputLayout>
You can set and remove errors with:
Java
text_input_layout.setError("Error message");
text_input_layout.setError(null);
Kotlin
text_input_layout.error = "Error message"
text_input_layout.error = null

Android: Prevent focus from jumping when hiding view

I have an Android activity that contains multiple EditText views.
If I programmatically hide the currently focused EditText, then the focus will automatically jump to the next EditText.
Is there anyway to disable this behavior so that the focus is cleared when the EditText is hidden rather than automatically jumping to the next EditText?
Thanks.
You need to provide Focus to a parent view above your EditText just like the sample code below
<RelativeLayout
android:id="#+id/search_edit_text_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true" >
<EditText...
Do try this to avoid providing focus directly to the next EditText

In android, how to drop down edit text fields when a button is pressed

I have a button Add new address and when it is pressed, I want to show EditText fields to collect the new address details. Is there any layout to do that. Or hiding the text fields when the Button is unpressed, is that the only way to do this?
Define the edit box in a layout as below -
<LinearLayout
android:id="#+id/exp_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/exp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="10dp"
/>
</LinearLayout>
And now use the layout id to get the view like below.
LinearLayout l=(LinearLayout)findViewById(R.id.exp_linear_layout);
And just toggle the visibility on button click event -
l.setVisibility(View.GONE) and vice versa.
I hope it will help u.
There is no built in framework to do it. You can do this by setting View.SetVisibility() to visible or gone. Initially the button is visible but textfield is invisible. When user click on the button, you can set this button visibility invisible or gone and visible the text fields.

How to hide keyboard in a edittext + listview layout

I have a LinearLayout composed by a ListView(which loads from an AsynTask) and an EditText(so I can filter that list). Here is the code:
<?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" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Filtrar por nombre..." >
</EditText>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
The problem is that when the activity starts, the keyboard is shown up and I only want to show it when the EditText is clicked for a search.
I've tried several methods such as writing the following in the manifest
android:windowSoftInputMode="stateHidden"
and including this in the class
InputMethodManager iMM = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
iMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
The only thing I've achieved is to not show the keyboard but not showing the loaded stuff of the list(just the EditText and the rest of the screen plain black), and once I click on the EditText, they appear both keyboard and the stuff of the listView.
Any posible solution for this?
EDIT:
I've tried the following solutions(thanks to #Sush, #boztalay and #Agata Sworowska), but none did the trick.
-Placed in my layout:
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
-And placed in my onCreate() method:
ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();
Any other posibility?
NEW EDIT:
I've noticed that when it doesn´t focus on the EditText, the listView doesn´t load until I focus back the EditText, llike if it was waiting for it. I've tried to set up the EditText after the code that loads the ListView but the problem persists.
This sounds like an issue with focus. What's happening is when the app is started, the EditText gets focus, and the keyboard pops up. You should try to make it so that the ListView gets focus. This way, the keyboard won't pop up until the user gives the EditText focus when they click on it.
In your Java, you should get an instance of the ListView, then call a couple of functions:
ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();
If you put that in your onCreate(), focus will be given to the ListView, and they keyboard won't come up until the user clicks the EditText.
See this SO question for more details.
You can just add below two attributes to your LinearLayout.
android:focusable="true"
android:focusableInTouchMode="true"
Try adding in Your layout, that contains EditText this:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
I hope it helps.
http://www.mysamplecode.com/2012/02/android-edittext-hide-soft-keyboard.html
This is a solution I found for the issue here. Do not understand why listivew should do this, but the workaround helps.

Android : clear focus on edittext when activity starts

I've some settings page in my app. Once the activity gets starts directly it focus to edittext and i used following code to clear foucs.
<RelativeLayout
android:id="#+id/RequestFocusLayout"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
and in java code
RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
focuslayout.requestFocus();
The above code is working fine when activity starts at first time and if same activity starts again, automatically edittext get focus.
Can anyone help me to solve this issue.
Actually, the first focusable view in the activity receives initial focus. If that happens to be your EditText, it will be initially focused.
If you don't want that, here's your options:
Focus another view
Programmatically identify what view you do want to give initial focus to.
#Override
protected void onStart() {
super.onStart();
findViewById( R.id.yourOtherViewId ).requestFocus();
}
Make an earlier view in your layout focusable
If you rather it appears as though "no view has initial focus" you could make the parent view group focusable. In the following example, I make my LinearLayout focusable by setting android:focusableInTouchMode="true":
<LinearLayout
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
...
If Come back from other activity edittext get focused.
put these line onStart() or on onReusme()
RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
focuslayout.requestFocus();
If your EditText is a child of your RelativeLayout you can use android:descendantFocusability to request the focus:
<RelativeLayout
android:id="#+id/RequestFocusLayout"
android:focusable="true"
android:layout_width="0px"
android:layout_height="0px"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" />
I used the solution shown up in this page but it didn't work. Add this attribute to your activity tag into AndroidManifest:
android:windowSoftInputMode="stateHidden"
It works perfectly.
Me realy help only android:focusableInTouchMode="true" in my parent view group.
Put the code to remove the focus in your onStart() method and it should work fine.
In the layout XML file, specify an imeOption on your EditText:
android:imeOptions="actionGo"
Next, add an action listener to your EditText in the Activity's java file
mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
return true;
}
return false;
}
});
Where mYourEditText is an EditText object
If on starting the Activity it focuses on EditText on the screen.
use
android:focusableInTouchMode="true"
in the parent containing that EditText. Like if EditText is inside a RelativeLayout use this line in the RelativeLayout.

Categories

Resources