This question already has answers here:
Detect end of ScrollView
(13 answers)
Closed 4 years ago.
I want to detect the end of scroll event of a scrollView.
first,i implement the OnCrollChange method and i try to listen some values if they can help me to know the end of scroll.
Thank to help me.
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener(){
#Override
public void onScrollChange(View view, int i, int i1, int i2, int i3){
Toast.makeText(getApplicationContext(),"in SCroll ------->"+i+" "+i1+" "+i2+" "+i3,Toast.LENGTH_SHORT).show();
shadowView.setBackgroundResource(R.drawable.shadow);
if ((i3==0)){
Toast.makeText(getApplicationContext()," equality ----------------------> ",Toast.LENGTH_SHORT).show();
shadowView.setBackgroundResource(R.drawable.trans);
}
else {
// Toast.makeText(getApplicationContext()," NO ----------------------> ",Toast.LENGTH_SHORT).show();
}
}
});
This will help
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if (scrollView != null) {
if (scrollView.getChildAt(0).getBottom() <= (scrollView.getHeight() + scrollView.getScrollY())) {
//scroll view is at bottom
} else {
//scroll view is not at bottom
}
}
}
});
Modify your onScrollChanged method as below
#Override
public void onScrollChanged(View view, int i, int i1, int i2, int i3){
View view = (View) getChildAt(getChildCount()-1);
int delta = (view.getBottom()-(getHeight()+getScrollY()));
if(delta == 0){
// You have reached bottom of the scroll view
}
}
Thank to all of you.
after some testes;i see that the values int i, int i1, int i2 take the same value egual to 0 at the real time so it solve my problem.
I will not use your suggestions now but i learn some important notion from them only by reading.
Related
I'd like to have a progress bar that tracks a user's form progress. For example:
TextFieldName: _______
TextFieldPhone: ________
With both fields empty, the progress bar should read 0%. If the user has filled out one of either their name or phone, the progress bar should update to 50%. If the user has filled out both name and phone, the progress bar should read 100%.
I have tried to approach the problem, but I can't deal with the fact that there should be some sort of global variable that's stored somewhere that can be grabbed at any time between edits (also not sure where that would be as I am new to this). Here is my code:
binding.progressBarNewRelease.setProgress(0);
float numInputs = 20;
float increment = (1 / numInputs) * 100;
binding.textInputEditTrackName.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
int currProgress = binding.progressBarNewRelease.getProgress();
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().length() > 0) {
binding.progressBarNewRelease.incrementProgressBy((int)increment);
}
else {
binding.progressBarNewRelease.incrementProgressBy((int)-increment);
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
binding.textInputEditArtist.addTextChangedListener(...); // similar code for artist name field
I have been playing around with the code a bit so it's missing most of what I have tried like setting the maxProgress to (currentProgress + increment) to prevent additional increments once the field is not empty.
Also, if there is a way, I would like to avoid having to have multiple bindings for each field, but if that is unavoidable then I understand.
EDIT
I have achieved the initial functionality I was looking for by using a combination of beforeTextChanged and onTextChanged, although I am a little skeptical about whether it covers all edge cases:
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().length() == 0) {
binding.progressBarNewRelease.incrementProgressBy((int)increment);
}
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().length() == 0) {
binding.progressBarNewRelease.incrementProgressBy((int)-increment);
}
}
I am also now setting the progress bar's max to the number of fields, and incrementing/subtracting by 1 for each field. Additionally, rather than new TextWatcher(), I am also now using a shared function where possible, which reduces amount of code.
Going to explore getting all the logic right, but still looking for how I could avoid having to repeat the same code for every field. For example, this is how I am handling multiple buttons:
MaterialButtonToggleGroup.OnButtonCheckedListener gridRowButtonListener = new MaterialButtonToggleGroup
.OnButtonCheckedListener() {
#Override
public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
if (isChecked) {
binding.progressBarNewRelease.incrementProgressBy(1);
}
else {
binding.progressBarNewRelease.incrementProgressBy(-1);
}
}
};
binding.buttonsGridRowOne.addOnButtonCheckedListener(gridRowButtonListener);
binding.buttonsGridRowTwo.addOnButtonCheckedListener(gridRowButtonListener);
binding.buttonsGridRowThree.addOnButtonCheckedListener(gridRowButtonListener);
binding.buttonsGridRowFour.addOnButtonCheckedListener(gridRowButtonListener);
I need to check the direction of the scroll when my listview is scroll up or down, I am getting it as:
int lastVisibleItem = 0;
boolean isScrollingDown = false;
void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem > lastVisibleItem) {
isScrollingDown = true;
Log.e("logkey","down");
}
else {
Log.e("logkey","up");
isScrollingDown = false;
}
lastVisibleItem = firstVisibleItem;
}
The problem is when the visible items are equal to the screen or when there are items to the whole screen suppose that only 6 items fit in the screen and the last item is half visible, the log cat starts showing me the both down and up at the same time!
In simple words, in the above case, the scroll direction is ambiguous to get when there are items equal to the screen to fit in and the very last item is half visible and when I scroll I am getting this problem!
Can somebody please tell me what I am doing wrong? Thanks in advance!
Implement the listview ScrollListener
listview.setOnScrollListener(new OnScrollListener() {
private int LastVisibleItem;
#Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(LastVisibleItem<firstVisibleItem){
Log.d("Tag","Scroll down");
}
if(LastVisibleItem>firstVisibleItem){
Log.d("Tag","Scroll up");
}
LastVisibleItem=firstVisibleItem;
}
});
So luckily i found something great on the GitHub! I have changed my simple native ListView to Observable List View listed here
https://github.com/ksoichiro/Android-ObservableScrollView
and it worked like a charm, as i want to work it as!
This question already has answers here:
How to remove drawableleft
(6 answers)
Closed 4 years ago.
I have read many answers in Stack but none of them can't help me, so please don't mark it as Duplicate ,I have EditText, and I want to make visible drawable left when the length of inserted data in EditTextView become equals to 11.
If you want to visible/invisible drawable left on user typing then do like this:
EditText et = (EditText)view.findViewById(R.id.edt);
et.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable editable) {
if(editable.length() >= 11){
// visible
et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.your_icon, 0, 0, 0);
}
else {
// hide
et.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
});
You can use the setCompoundDrawables method to do this:
Drawable img = getContext().getResources().getDrawable(R.drawable.add_more);
img.setBounds(0, 0, 60, 60);
edittext.setCompoundDrawables(img, null, null, null);
if (edittext.lenght == 11) {
img.setVisible(false, false);
}
I hope it will help you!
Is it possible to check if a ScrollView is scrolled all its way in the top?
I want to check this so I can enable a SwipeRefreshLayout, otherwise keeping it disabled.
With a ListView it could be done like this, but there's no setOnScrollListener for ScrollViews
listView.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
boolean enable = false;
if(listView != null && listView.getChildCount() > 0){
// check if the first item of the list is visible
boolean firstItemVisible = listView.getFirstVisiblePosition() == 0;
// check if the top of the first item is visible
boolean topOfFirstItemVisible = listView.getChildAt(0).getTop() == 0;
// enabling or disabling the refresh layout
enable = firstItemVisible && topOfFirstItemVisible;
}
swipeRefreshLayout.setEnabled(enable);
}
});
This link might be helpful to You. It shows, how to set scroll listener for ScrollView. Then, refer to #antonio answer.
For your case it would be:
mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollY = mScrollView.getScrollY(); //for verticalScrollView
if (scrollY == 0)
swipeRefresh.setEnabled(true);
else
swipeRefresh.setEnabled(false);
}
});
You can use the getScrollY() method (from the View class)
In Kotlin:
scrollView.viewTreeObserver.addOnScrollChangedListener {
if (!scrollView.canScrollVertically(1)) {
// Bottom of scroll view.
}
if (!scrollView.canScrollVertically(-1)) {
// Top of scroll view.
}
}
When you remember a good tutorial for checkBoxes please let me know!
You're getting the State using:
boolean selected = checkBox.isChecked();
Then you can add the OnLayoutChangeListener on your View Object.
Example Usage:
View.addOnLayoutChangeListener(new OnLayoutChangeListener(){
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom){
boolean selected = ((CheckBox)v.findViewById(R.id.yourCheckBoxID)).isChecked();
//saveTheStateHere
}
});