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
}
});
Related
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);
}
});
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
}
}
);
I have a layout that consists of 3 contiguous ListViews. Selecting an item in the left most list will start the second list, and so on. I am trying to begin the fragment with the first item in the left most list selected, and because a lot of logic is required, I am attempting to trigger on click instead of redo-ing all the logic. I am using this:
mListView.performItemClick(mListView, 0, mListView.getItemIdAtPosition(0));
Is what I am attempting possible? and if it is, where do I call this?
ANSWER:
So it seems that the reason this wasn't working for me is because I was manually highlighting a View within the row View and it wasn't instantiated yet (or something like that).
I used this code to call it after the ListView was laid out:
mListView.post(new Runnable() {
#Override
public void run() {
mListView.performItemClick(mListView, 0, mListView.getItemIdAtPosition(0));
}
});
EDIT:
I have 4 lists that depend on data being sent from the previous ones before being instantiated, and for some strange reason, this runnable method only works for the first list. For the others, I used this answer: https://stackoverflow.com/a/15399035/1549672. Also, strangely enough, this does not work on the first list. All the lists are nearly identical.
As per earlier comment:
You can try wrapping the selection change in a Runnable and post that to the ListView. That way the selection change will not happen until the ListView (and its children) has been measured and laid out etc. Do make sure you set the adapter before attempting to change the selection.
I wanted to develop an on screen keyboard similar to the square app (image below)
I would appreciate it if anyone could give me tips on which classes to focus on/ override for this. In particular, how do I associate the button clicks to the number input in the EditText field?
Every Button gets a Tag with the associate value (for example 1). Then, you use the android:onClick-attribute to set all buttons to the same method (input() for example) in the activity.
This method will be called with the clicked view, which you can then use the getTag()-method on to get the corresponding value:
// Will be called for every Button that is clicked
public void input(View v){
Log.v("APP", "Pressed: "+v.getTag());
}
Organize the Buttons in a GridLayout (GridView might also be possible).
What I want to do is to scroll view after listening a button.
Simply user taps button and view is scrolled to specific id below.
I tried to use scrollto and scrollby passing as an argument reference to object to which I want to scroll with no effect.
Anyone solved this problem yet?
I have spent many hours on this.
Try something like this. It makes sure that the item selected it the one in view:
ListView listView;
int positionSelected = listView.getSelectedItemPosition();
listView.setSelection(positionSelected);