Have an autocomplete textView in layout which is in a ScrollView.
mAutoCompleteTextView.addTextChangedListener(new TextValidator(mAutoCompleteTextView) {
#Override
public void validate(TextView textView, String text) {
if(text == null){
mAutoCompleteTextView.setError(text);
}
});
This shows a red error icon with a dropdown error message in case text is not valid. While error is visible and page is scrolled, the centre of page returns back to this view instead of scrolling up/down.
How to avoid this while error is still visible?
Try this ..
This will allow you to scroll smoothly, if setEnabled() doesnt work you can also user clearFocus() and requestFocus()
scrollLayout.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
Rect rect = new Rect();
mAutoCompleteTextView.setEnabled(false);
if (mAutoCompleteTextView.getGlobalVisibleRect(rect)
&& mAutoCompleteTextView.getHeight() == rect.height()
&& mAutoCompleteTextView.getWidth() == rect.width() && mAutoCompleteTextView.isShown()) {
mAutoCompleteTextView.setEnabled(true);
} else {
mAutoCompleteTextView.setEnabled(false);
}
}
});
Hope this helps
In the root Layout use this :
android:descendantFocusability="beforeDescendants"
And then in onCreate():
rootLayout.requestFocus();
Hope this helps.
Related
I am trying to add and remove edittext through RecyclerView Adapter and it's working fine but I also want to shift the focus to next empty edittext when I click on adapter item which fills the edit text. It would be grateful if someone could help ,I am newbie to android.
Here is my focus next method:
private void focusNext() {
View view = getAdapterView(mAddDropPointAdapter.getCurrentFocusedPosition() + 1);
}
if (view != null) {
view.findViewById(R.id.fragment_google_autoc_et_destination).requestFocus();
} else if (getActivity() != null) {
KeyboardUtils.hideKeyboard(getActivity());
}
}
Here is from where I am calling this method.
#Override
public void onAdapterItemClick(int position, #NonNull GoogleAutoCompleteAddressDto addressDto) {
/// Doing some stuff
focusNext();
}
After clicking the adapter item , focus stays at the same edittext it doesn't shift to next.
first time,click an edittext in expandablelistview,the whole layout scrolled ,and user can see edittext. then click softkeyboard finish to collapse softkeyboard and click this edittext second time ,softkeyboard show, but the whole layout not scrolled, and user cannot see edittext. I find this in listview ,too.I searched for a long time and some one says it is an anroid bug?
Is there any replacement?
By many days spent,I found a solution. It is a hack.
put you edittext wrapped in linearlayout.
detect softkeyboard show by visiable layout change.
public final void setKeyboardListener(final OnKeyboardVisibilityListener listener) {
final View activityRootView = getWindow().getDecorView()
.findViewById(android.R.id.content);
activityRootView.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
private final Rect r = new Rect();
private boolean wasOpened;
#Override
public void onGlobalLayout() {
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight()
- (r.bottom - r.top);
boolean isOpen = heightDiff > 100;
if (isOpen == wasOpened) {
return;
}
wasOpened = isOpen;
listener.onVisibilityChanged(isOpen);
}
});
}
public interface OnKeyboardVisibilityListener {
void onVisibilityChanged(boolean visible);
}
when the softkeyboard show,pick up a textview in listview item or expandablelistview child item. then set it text for any thing, the whole view scrolled as normal! code like this:
activity.setKeyboardListener(new OnKeyboardVisibilityListener() {
#Override
public void onVisibilityChanged(boolean visible) {
if (visible) {
Message message = new Message();
message.what = 999;
handler.sendMessage(message);
}
}
});
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (msg.what == 999) {
if (mDataMap.size() > 0) {
for (int i = 0; i < expandablelistview.getChildCount(); i++) {
View v = expandablelistview.getChildAt(i);
TextView tvCannotSee = (TextView) v.findViewById(R.id.tv_cannot_see );
if (tvCannotSee != null) {
//this is the key hack to make adjuspan work! and scroll the whole view up as normal
tvCannotSee.setText("this is an android bug");
return;
}
}
}
return;
}
super.handleMessage(msg);
}
};
this textview show invisble to user for proper purpose.if it is a expandablelistview and the edittext in childitem,the textview update must be in a childitem.
I also faced the same issue, but after googling the issue for some time i ended up the problem as below.
1) Add android:windowSoftInputMode="adjustPan" tag inside activity of manifest file
<activity
android:name=".MainActivity"
android:configChanges="locale|keyboardHidden|orientation|screenSize|layoutDirection|screenLayout"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"></activity>
2) add android:descendantFocusability="beforeDescendants" attribute to your expandable listview as below.
<ExpandableListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/textDate"
android:layout_below="#+id/relativeLayout2"
android:descendantFocusability="beforeDescendants"
android:layout_margin="#dimen/margin_meium"
android:groupIndicator="#android:color/transparent" />
I am using swipelayout from daimajia.I get some data from services.When the service has a date than will visible an icon that will open the left sideBut it do nothing when clicking the icon.I have no error that's the reason why cannot resolve it.The action is when clicking the icon,that visible a textview and start a chronometer.
Here is my swipeListener
private SwipeListener swipeListener = new SwipeListener() {
#Override
public void onStartOpen(SwipeLayout layout, SwipeLayout.DragEdge edge) {
if (edge == SwipeLayout.DragEdge.Left && viewHolder.discountText != null) {
**viewHolder.discountText.setTimeByTag().play();**
LogUtils.LogE("Left Open...");
}
}
#Override
public void onOpen(SwipeLayout layout, SwipeLayout.DragEdge edge) {
super.onOpen(layout, edge);
if (edge == SwipeLayout.DragEdge.Left && viewHolder.discountText != null) {
viewHolder.discountText.setTimeByTag().play();
LogUtils.LogE("Left Open...");
}
}
#Override
public void onClose(SwipeLayout layout, SwipeLayout.DragEdge edge) {
if (edge == SwipeLayout.DragEdge.Left && viewHolder.discountText != null) {
viewHolder.discountText.stop();
LogUtils.LogE("Left Close!");
}
}
};
public void openLeft() {
LogUtils.LogE("onClick openLeft-");
if (getSwipeLayout() == null){
LogUtils.LogE("onClick -null-");
return;
}
if (!getSwipeLayout().isOpen()) {
getSwipeLayout().open(true, SwipeLayout.DragEdge.Left);
LogUtils.LogE("onClick --");
}
}
all the codes working in other my fragments.I need some idea what's the reason that it not give true action that swipe to left.
EDIT:
Here is the Logs:
In this picture I have 2 items there which it's in my favorite page.The last one item has end date that will be visible the icon(ImageView).When pressing the icon,it should swipe to left side but it's not working and give the result like this picture which I'm shared.
EDIT
There is something more.I had looked the codes in debug mode and give here an message:
but put define swipeLayout into my code:
sl.setShowMode(SwipeLayout.ShowMode.LayDown);
sl.setDragEdges(SwipeLayout.DragEdge.Left, SwipeLayout.DragEdge.Right);
sl.setBottomViewIds(R.id.productBottomLeft, R.id.productBottomRight, SwipeLayout.EMPTY_LAYOUT, SwipeLayout.EMPTY_LAYOUT);
TextView button = SwipeLayout.findViewById(R.id.yourButtonId) //the bottom view;
button.setOnClickListener(v -> yourMethod());
My button use code that shows and hides the views:
public void onClick (View v){
if (What code you need to enter here to determine hidden views or shown)
{
testActivity.setVisibility(View.VISIBLE);
}
else
{
testActivity.setVisibility(View.GONE);
}
}
What code I need to add in the "if()", so that clicking on my button was checked condition. If the activity is hidden, it should be shown, and Vice versa. If the views is shown, hide it.
I'm guessing since you are using setVisibility, that you want to check the visibility of a View , not an Activity.
In that case you just use getVisibility()
(I used != cause the visibility could be IINVISIBLE as well, change per your needs) :
public void onClick (View v){
if (testActivity.getVisibility() != View.VISIBLE)
{
testActivity.setVisibility(View.VISIBLE);
}
else
{
testActivity.setVisibility(View.GONE);
}
} });
Don't understand why, but only that removed the answer of a man who has solved my problem. Here is his response, and this code works:
public void onClick (View v){
if ((testActivity.getVisibility() == View.VISIBLE))
{
testActivity.setVisibility(View.GONE);
}
else
{
testActivity.setVisibility(View.VISIBLE);
}
I have a listview where pople can click on items. When they do, they increase height and show more information.
However, when clicking the bottom item, it is not visible to the user that the item height has increased and there is content if you scroll down.
Using e.g. following code in onItemClick does not solve the problem:
if (position == myItemsDataArrayList.size() - 1) {
if (data.ui_flags == "clicked") {
catalogListView.smoothScrollToPosition(position);
}
}
Implement this function in your if statement.
private void scrollMyListViewToBottom() {
myListView.post(new Runnable() {
#Override
public void run() {
// Select the last row so it will scroll into view...
myListView.setSelection(myListAdapter.getCount() - 1);
}
});
}
code by: https://stackoverflow.com/a/7032341/2197087
Working code solution was this:
catalogListView.post(new Runnable() {
#Override
public void run() {
catalogListView.setSelection(position_final);
}
});