How to make ScrollTo scroll to the same y - android

I have a TextView that contains a big text and a ScrollView.
So I wanna make a Table of contents for it.
But using the smooth/scrollTo it gives different results on different devices.
I'm trying to achieve scrolling to the same position on different devices.
Thank you.

You can use the position of a widget, for example you can add a widget to any part you want and make it invisible and then get the coordinates of that widget.
scrollView.smoothScrollTo(0,layout_parent.getTop());
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.smoothScrollTo(0,textView.getTop()-100);
}
});

Related

onFocusChange including children?

Assume that I have a simple full-screen ConstraintLayout, with a few EditTexts and TextViews in the middle.
Is it possible to know whether one of those views is in focus? What I'm trying to achieve is basically a blanket onFocusChangeListener for all children of a layout.
I've tried assigning a listener to the ConstraintLayout, but it only fires if I tap on the layout itself, outside of those EditTexts.
I could simply assign a listener for each individual element in the UI, but what if I have hundreds?
(I could simply loop through all of them, but please assume I can't)
You could attach a global listener to your ViewTreeObserver with addOnGlobalFocusChangeListener. Something like:
view.getViewTreeObserver().addOnGlobalFocusChangeListener(
new OnGlobalFocusChangeListener() {
#Override
public void onGlobalFocusChanged(View prevFocus, View newFocus) {
// Do stuff here
}
}
);

Android search in recyclerview and scroll to that

You suppose in simple recyclerview i have some text, i'm trying to find how can i search text in that and scroll to found text position, for example in simple recyclerview i have 10 row and on 4th position i have this text:
Hello World
i want to scroll to that on 4th position, how can i do that?
Check your RecyclerView items for their texts, once you have a match to your desired text, get the item position and call:
myRecyclerview.scrollToPosition(position);
Iterate through your Adapter's data list and store the index for the item you're searching for. Then, use RecyclerView's scrollToPosition(y) method with that index to scroll to that particular item's position.
Workaround that I found on stackoverflow which really worked for me
myRecyclerView.scrollToPosition(position);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
myRecyclerView.smoothScrollToPosition(position);
}
}, 50); //sometime not working, need some delay
https://stackoverflow.com/a/57207024/6940711

Etsy Staggered gridview android smoothScrollToPosition

I have to implement a smoothScrollToPosition with Eatsy Staggered gridview but the Gridview method doesn't work. How can I do a method that simulate the smooth scroll to a specific cell?
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
if(*myCondition*){
adapter.notifyDataSetChanged();
gridView.smoothScrollToPosition(position);
}
}
}
With standard GridView smoothScrollToPosition works fine, not works with Staggered.
gridView.setSelection(position);
not work correctly, the cells change their position and if I scroll the grid manually restore their correct position (with standard grid view works fine, so I think the problem isn't my code but the library)
Late response but I think you'd be better off using RecyclerView with StaggeredManager: http://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html

Scroll to form element

I have a form with lots of fields and when I press the "submit" button, I'd like the scrollview to jump to the position where the unanswered field is located. Is there a method other than scrollto(x,y) to do this or such coordinates must always be mapped to the form's elements somehow ( something like (form element).getCoordinates() )?
Thanks
This should work -
view.setFocusableInTouchMode(true);
view.requestFocus();
view.requestRectangleOnScreen(viewRectangle);
You can always take a look at the View class.
P.S. The unanswered field will be selected but I'm not sure that if it is out of the screen the Activity will scroll to the requested position.
ScrollViews do have predefined methods scrollTo() & scrollBy() for such functionality.
I believe you are having issues in getting co-ordinates. So I suggest store height of each view (that consumes vertical space)by using View.getHeight method & store heights in a Map collection .
Below is the code snippet I tried
scrollView.post(new Runnable() {
public void run() {
Integer emailTextViewHeight = (Integer)hashMap.get("EmailTextViewHeight");
scrollView.scrollBy(0, emailTextViewHeight);//scrolls to emailTextView
}
});

How to get ListView to grow to the size of content

So I have a fairly complex activity the parent being a linearlayout with a table with some basic info and buttons. Then a couple listviews that the user can add items to. So these listviews grow more and more as the user uses the app. The problem I'm running into is the Linearlayout is bigger then the resolution of the screen and so it needs to scroll. So the scrolling doesn't work on the Listviews. I've tried playing with changing the layout_height of the listview and its child element with no success. Is there a way to make these couple listviews expand out to the amount of children? Or am I going about this all wrong? If so what other controls can I use?
I'm open to any ideas. I really just don't know how to go about this.
Thanks
The problem I'm running into is the
Linearlayout is bigger then the
resolution of the screen and so it
needs to scroll. So the scrolling
doesn't work on the Listviews.
You need to rethink the way you are building your UI, as you cannot have ListViews inside of a ScrollView.
One possibility is to put everything in the ListView, marking some rows as being non-clickable, so ListView handles your scrolling. You can use my MergeAdapter for cases like this.
If you have a simple layout then you can replace your ListView component with a LinearLayout and manually program together the functionality you need to add/remove and click the items within it.
Here is a simple example of my own. refreshStudentList() has taken the place of notifyDataSetChanged() being called on the adapter, recentConnections is a LinearLayout and adapter is an instance of an Adapter (You can use the adapter you were using for your ListView):
private void refreshStudentList() {
recentConnections.removeAllViews();
for(int i=0; i < adapter.getCount(); i++) {
View toAdd = adapter.getView(i, null, recentConnections);
final int position = i;
toAdd.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// onItemClick functionality here
}
});
recentConnections.addView(toAdd, new LayoutParams(LayoutParams.MATCH_PARENT,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics())));
}
recentConnections.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
As you can see, instead of an onItemClickListener I've set a onClick listener to each list item. This could easily be changed to be an onTouch listener if you wanted to implement the look and feel of clicking on a list item (by changing the view's background on MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP).
The other thing you need to note is I have hard coded a dp value for the height of each value (50dp in this case). When you are creating and inflating views programatically they don't have a "height" value so "wrap_content" becomes 0.
also, notifyDataSetChanged() on your adapter will do nothing.
As I said, this is a simple solution but it's the start of building up your own ExtendingListView custom component if that's what you're eventually aiming for.

Categories

Resources