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.
Related
My view got 3 item, EditText, ImageView and TextView. now i'm doing some animation base on a tutorial which was working fine, now in that tutorial guy was using onClickListener to animate view and hide image, and was working fine, but my problem is, i want to show image when user is not on EditText anymore.
Right the idea is working like this
Click on EditText => Gonna animate and hide image => and when u tap or click on layout/activity/view/etc. image won't comeback.
Code :
btnInvite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
titleInvite.animate().translationY(-350).setDuration(800).setStartDelay(100).start();
subtitleInvite.animate().translationY(-350).setDuration(800).setStartDelay(100).start();
inputInvite.animate().translationY(-350).setDuration(800).setStartDelay(200).start();
btnInvite.animate().alpha(1).translationY(-350).setDuration(800).setStartDelay(300).start();
imageView.startAnimation(disapear);
imageView.setVisibility(View.INVISIBLE);
}
});
This is what going to happen when someone click on EditText now i want to reverse it when edit text is not focused anymore, or something else clicked. I'm also using API 17, and i'm telling this bcs i looked into some of features but most of them couldn't be used in 17.
You need to add a listener for loosing focus on editText, in that listener execute the reverse animation.
EditText txtEdit = (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// Execute reverse animation
}
}
});
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.
i have EditText bottom of screen. but i want, it should go top of the screen when i start entering some value with keyboard.
as per image, Email id,Password are bottom of the screen. I want it goes up as top of screen (it means that facebook login & google login will disappear) like second image.
I have used below code in this activity under manifest.xml
android:windowSoftInputMode="adjustPan"
android:isScrollContainer="true"
but unable to get desired screen as per second image Please suggest me what can i do to achieve this ?
Add one more value to windowSoftInputMode like below,
android:windowSoftInputMode="adjustPan|adjustResize"
It cannot be done using these (android:windowSoftInputMode="adjustPan") flags.
Will suggest a work around to achieve the desired.
First of all have upper layout (FB & Gmail login) in one single layout (Ex LinearLayout or RelativeLayout) so that you can show/hide this layout.
Now set focus change listener on Email ID and Password, and check if any of these gets the focus hide above layout (facebook & google login) and whenever both the edittext loses the focus show the layout again.
editTextEmail.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
//check if password has focus as we need to consider both the edittext. If both don't have the focus then show the FB & Gmail login.
}
}
});
Hope this helps!
You can put your views inside ScrollView and when focus changed you can scroll it up and down
private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (v.getId() == _passwordText.getId()) {
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.smoothScrollTo(0, scrollView.getBottom());
}
}
);
} else {
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.smoothScrollTo(0, scrollView.getTop());
}
}
);
}
}
}
};
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
}
}
});
i'm facing wired problem in android when i try to focus for a EditText control.
In my application, i am dynamically adding the edit controls, whenever new control is added i wanted to
give auto focus and popup the softkeypad automatically.
sometimes editbox get focus, but if i type text will not appear, sometimes text will appear in the other text control.
i'm observing this behaviour in my phone.
Here is my code snippet which i used to provide focus.
if(mOldEditBox!=null)
{
mOldEditBox.clearFocus();
}
textView.requestFocus();
//textView.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad
//textView.setOnFocusChangeListener(focuschange);
mOldEditBox = textView;
i tried setting focuschangelistener event, still it didnt worked :(
OnFocusChangeListener focuschange = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
EditText txt = (EditText)v;
//txt.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
.showSoftInput(txt, InputMethodManager.SHOW_FORCED);
}
}
};
Kindly help me where is the problem.. thanks in advance
You are probably not on the UI thread when requesting focus which might cause the strange behavior of the EditText. Try adding it to the message queue using the post(Runnable) method:
textView.post(new Runnable() {
public void run() {
textView.requestFocus();
}
});