How to pass the focus state to View's parent? - android

How can a view detect its child view's focus state? As you can see in this picture.
The parent view is a LinearLayout with a child view EditText. I wanna change the UI while the focus state changed(linearlayout turns red if focus on the edittext). But only the EditText can detect the focus state while the LinearLayout can not. Of cause I can listen the EditText's state using OnFocusChangedListrner. But I don't satisfy with it. Is there a simple way to make the parent view get the child view's focus event? The form has a lot of items.

I also think there is no other method than
onfocusChangeListner
If you have many edittext then you simply make one method with the parameter and inside it you write only one onfocuschangeListner like this
public void setColorToEditText(EditText editText) {
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
Log.e("userFocu", "userFocu");
if (!hasFocus) {
// code to execute when EditText loses focus
}
}
});

Related

How Do I Use setOnFocusChangeListener with RecyclerView?

I have the following on onBindViewHolder() in Adapter Class for RecyclerView:
holder.answerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
String answer = holder.answerEditText.getText().toString();
mDatasetAnswers.add(answer);
}
}
});
The above only returns input from the first editText in the recyclerview. What could I be doing wrong?
I would like it to return text from all EditTexts in the recyclerview.
This happens because of the keyboard which pops up once you click on any Edit box in a recyclerview because onbindview is called and the focus changes to the first box in the recyclerview as all rows are reinflated again.
Hence, monitor for on focus gain and do ur stuff first before keyboard pops up. Hope this helps.
Try to add these lines:
holder.answerEditText.setFocusable(true);
holder.answerEditText.setFocusableInTouchMode(true);
With these lines you make sure that the component can capture the focus.

Android - EditText in ListView and OnFocusChangeListener

I have a ListView that contains an EditText widget in each row. The user can update the numeric value in the EditText, and once the focus is lost on that EditText, I want a TextView updated on the screen that contains the sum. I'm using the below code to check if the focus has been lost, but it every time I put focus on an EditText, it enters the if statement, and I think that has to do with the fact it is contained within a ListView. I'd like it to only enter the if statement when the user removes the focus from the EditText.
MyEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
//Perform Calculation
}
}
});

ScrollTo() does not work eventhough "adjustResize" is used

I have a screen which fits to viewport and hence does not show a scrollbar by default.
It has some TextView elements, a EditText and a CheckBox below the EditText. My objective is to scroll and show CheckBox element when EditText control is in focus. When EditText is in focus, the screen becomes scrollable since I have "adjustResize" set for the activity.
Now here is the code which is trying to scroll:
mEditTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
final ScrollView mScrollView = (ScrollView) findViewById(R.id.scrollView1);
if (hasFocus){
if(mScrollView != null) {
mScrollView.post(new Runnable() {
#Override
public void run() {
mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}
}
And this does not scroll to the bottom. it just brings focus onto the soft keyboard and the checkbox is hidden behind it.
Can anybody tell me what is wrong here?
PS: My guess here is Even after 'adjustResize', Android still thinks that there is no scope for scrolling and hence does not scroll. this could be the case here?
How you tried to invoke the fullScroll() directly instead of posting a runnable? I think that this might be the problem, because post() just adds your wish of the scrollview to scroll to the message queue, and probably the message queue is not worked while the textview has the focus and the softkeyboard is shown.
Why don't you use the method scrollTo(x,y)? I used it, it's perfect. Just use Handler send a delay message,use this method when you receive the message.

Get child that last had focus

I have found that when using a d-pad or trackball to navigate in my app if you move to the right and the list view looses focus, when the list view regains focus, a different child is given focus than last had focus. I tried to use onFocusChange(...) to save which child had focus but it looks like this isn't called until after the focus is lost so I can never grab which child last had focus. Is there a way to grab who had focus so I can then call requestFocus() on the child once the list view grabs focus again?
Unfortunately I cannot use a handler because this isn't a much used feature and I don't want to sacrifice the performance for a smaller feature.
Here is the code I had that didn't work (focusedView was always null no matter what):
mainListView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(focusedView == null ) { Log.i("focus", "focusedView == null"); }
if(hasFocus && focusedView != null) {
Log.i("focus", "Focus has been Recieved.............");
focusedView.requestFocus();
} else {
// Focus has been lost so save the id of what the user had selected
Log.i("focus", "Focus has been Lost.............");
focusedView = mainListView.findFocus();
}
}
});
Thanks!
I think you are spot on with your own answer, bascially since the focus listener is called after its lost focus, no child will be in focus and therefore .findFocus() will return null.
I recon you best bet is to extend the ListView class, and override the onFocusChanged method. Basically do what you are doing, but do it inthere when gainFocus is true.
#Override
protected void onFocusChanged (boolean gainFocus,
int direction, Rect previouslyFocusedRect) {
if(gainFocus)
focusedView = mainListView.findFocus();
}
This one

EditText focus lost

In a custom ListView, there are two columns, one contains a TextView and the other a EditText component.To enter some preferences, as the user clicks on the EditText, the software keyboard comes in focus but focus from the EditText is lost. How I can do this?
Focusable EditText inside ListView
For editText use method setFocusable(false).
For textView use setFocusable(true).
Also write a listener on focus lost for both textView and editText:
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
{
//do job here when EditText loses focus
}
}
});

Categories

Resources