Rectangle 27 4

In android tableView called as TableLayout here is the tutorial, will show you how to add rows to TableLayout dynamically. For scrolling feature you can always use ScrollView mentioned in older answers. Good Luck!

Thanks for the reply. But the rows are getting added dynamically and even the ScrollViews are added. Its not the problem. But my requirement is when a new row adds up the ScrollBar should auto scroll in order to show up the newly added row. How can I do this? Can you help me!!

go through above link I am sure u will find solution...

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

android - Auto Scroll of theTableView - Stack Overflow

android android-tablelayout
Rectangle 27 1

Put the Table Layout inside the ScrollView layout as

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableLayout android:id="@+id/score_table"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"> 
        <TableRow android:id="@+id/header"/>                    
    </TableLayout>
</ScrollView>

Thanks for the reply. But I am not creating tableView from Xml. Am creating it through the code itself. So can you tell me how to enable auto scrolling for the dynamically added TableView?

Create scroll view dynamically and start adding child view Table Layout to the ScrollView dynamically

android - Auto Scroll of theTableView - Stack Overflow

android android-tablelayout
Rectangle 27 1

try like this in your xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:scrollbars="vertical" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent">

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tLayout"
        android:scrollbars="vertical"
        >   
</TableLayout>
</Scrollview>

In your .java class, use id of TableLayout to add rows dynamically, When number of rows will exceed the screen height, scrollbar will be shown.

as shown below in your TableLayout

<TableLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tLayout"
            android:isScrollContainer="true"
            >   
    </TableLayout>

for that you can use

TableLayout tl=(TableLayout)findViewById(R.id.tLayout);    
TableRow tr1 = new TableRow(this);
//to add row on table ,you can use loops to add multiple rows on table
tl.addView(tr1);
if(tl !=null)
tl.removeAllViews();

if you don't want to remove all views and add new views everytime

you can add row using index like this

tl.addView(child, index); //where child is row

When i refer my table layout by the ID, the program is crashing.

what error u r getting ??

I have added a tableRow in it.

android - Auto Scroll of theTableView - Stack Overflow

android android-tablelayout
Rectangle 27 1

To scroll automatically to the end (assuming your TableView is contained by a ScrollView):

scrollView.post(new Runnable() {            
    @Override
    public void run() {
           scroll.fullScroll(View.FOCUS_DOWN);              
    }
});

Let's say you have this layout:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView1"
            android:text="Column 1"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/button1"
            android:text="Column 2" />
    </TableRow>

</TableLayout>

And you want to use a scrollView. It should NOT look like this:

Instead, you should group everything you want to include in the scrollView into one layout and use that as a child of the scrollView, like so:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dip" >

                <TextView
                    android:id="@+id/textView1"
                    android:text="Column 1"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <Button
                    android:id="@+id/button1"
                    android:text="Column 2" />
            </TableRow>

        </TableLayout>

        <Button
            android:id="@+id/myButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

    </LinearLayout>
</ScrollView>

On using the code which you had posted i got the exception10-05 11:44:51.674: E/AndroidRuntime(538): java.lang.IllegalStateException: ScrollView can host only one direct child My code was- VSC.fullScroll(tableLayout.FOCUS_DOWN); where VSC is the ScrollView and tableLayout is my Table.. Can you help me?How to debug it??

android - Auto Scroll of theTableView - Stack Overflow

android android-tablelayout
Rectangle 27 34

To auto scroll on the TextChanged event:

textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();

c# - How to make autoscroll multiline TextBox in WinForms? - Stack Ove...

c# .net winforms textbox
Rectangle 27 135

viewDidLayoutSubviews
-(void)viewDidLayoutSubviews {
   [super viewDidLayoutSubviews];
   [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
}
viewWillAppear
[self.view layoutIfNeeded];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];

That's exactly what I'm trying to use. I have the number of the object in the array and I know the name of that object. But don't know how to put correct indexPath... Any ideas?

indexPath
[NSIndexPath indexPathForItem:numberOfTheObject inSection:sectionNumber]

Had some errors. But then I've put all the mentioned code inside (void)viewDidLayoutSubviews{} and now it's working fine. Thanx Bogdan.

This doesn't work reliably on iOS 7.1 and can lead to the collection view appearing as a black screen. I also didn't want to maintain state like the solution in viewDidLayoutSubviews below. What I did is simply performed a [self.view layoutIfNeeded] right before the call to scrollToItemAtIndexPath

Doesn't work for me in the latest iOS (9/10)

ios - UICollectionView auto scroll to cell at IndexPath - Stack Overfl...

ios uicollectionview
Rectangle 27 66

I've found that scrolling in viewWillAppear may not work reliably because the collection view hasn't finished it's layout yet; you may scroll to the wrong item.

I've also found that scrolling in viewDidAppear will cause a momentary flash of the unscrolled view to be visible.

And, if you scroll every time through viewDidLayoutSubviews, the user won't be able to manually scroll because some collection layouts cause subview layout every time you scroll.

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    // If we haven't done the initial scroll, do it once.
    if (!self.initialScrollDone) {
        self.initialScrollDone = YES;

        [self.collectionView scrollToItemAtIndexPath:self.myInitialIndexPath
                                    atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
    }
}

Nice tip. I'll emphasize using an initialScrollDone flag as LenK has done since this method will get called more than once and if you call scrollToItemAtIndexPath: more than once it seems to render the collectionView unscrollable(iOS simulator iPhone 5/iOS 8)

Works great in iOS8. Should be the accepted answer.

This didn't work for me in iOS 8.3. Had to call [self.collectionView layoutIfNeeded]; before, which also works when you do this within viewDidLoad.

This definitely should be the accepted answer. The answer above gave me a flash as the first cell was shown and then updated with my new cell, this method transitions seamlessly.

ios - UICollectionView auto scroll to cell at IndexPath - Stack Overfl...

ios uicollectionview
Rectangle 27 2

// debounce/throttle plugin
(function(b,c){var $=b.jQuery||b.Cowboy||(b.Cowboy={}),a;$.throttle=a=function(e,f,j,i){var h,d=0;if(typeof f!=="boolean"){i=j;j=f;f=c}function g(){var o=this,m=+new Date()-d,n=arguments;function l(){d=+new Date();j.apply(o,n)}function k(){h=c}if(i&&!h){l()}h&&clearTimeout(h);if(i===c&&m>e){l()}else{if(f!==true){h=setTimeout(i?k:l,i===c?e-m:e)}}}if($.guid){g.guid=j.guid=j.guid||$.guid++}return g};$.debounce=function(d,e,f){return f===c?a(d,e,false):a(d,f,e!==false)}})(this);

//elements you want to scroll to go here
divs = [$(".my-div-1"),$(".my-div-2"),$(".my-div-3")];

var lastScrollTop = 0; //we'll update this on every scroll and compare it to the last scroll to determine the scroll direction
var run = true;

$(window).scroll($.debounce(250, true, function () { //debounce so it only runs once per scroll
  var st = $(window).scrollTop();
  if (st > lastScrollTop) { // if the scrollTop when the scroll event fires is larger than the last scroll, we can assume the scroll was in a downward direction
    $.each(divs, function (i, v) {
      ((v.offset().top - $(window).scrollTop()) < 0) && (next = i + 1); //compare each elements offset to the windows offset to determine which element we're currently scrolling through
    });      
    run = (next != divs.length) ? true : false; //dont run if we are at the last div
  } else {
    $.each(divs, function (i, v) {
      ((v.offset().top - $(window).scrollTop()) < 0) && (next = i);
    }); 
    run = true;
  }
  if (run) {
    $('html, body').animate({
      scrollTop: divs[next].offset().top
    }, 1000,'linear', function() {
      lastScrollTop = $(window).scrollTop();
    });
  } else { lastScrollTop = $(window).scrollTop(); }
}));

Thanks mate. I got the idea now. but it seems it gives trouble to scroll up.

@Zawad see my edit - it's not perfect but it now works scrolling up. This should be a sufficient answer to your question, please mark this as solved if so.

it looks good mate. Actually i am new here, so can u please tell me how to mark your reply as solved. i couldnt find the way to do it.

The checkmark under the vote up/down arrows on the left

javascript - Auto Scroll To A each div or element - Stack Overflow

javascript jquery css html
Rectangle 27 4

Its a bad practice to have two different scrolling views together. ListView itself has its own scrolling functionality and height is auto adjusted according to Adapter settings for your row items (Keelping in mind, we are not setting specific height to ListView in our Layout xml). However in your case, you can replace ListView with a class and this will adjust height for your ListView keeping in mind that your ListView is inside ScrollView.

public class ExpandableHeightListview extends ListView
{

    boolean expanded = false;

    public ExpandableHeightListview(Context context)
    {
        super(context);
    }

    public ExpandableHeightListview(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightListview(Context context, AttributeSet attrs,int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

Now extend your ListView with this class, just change your ListView tag from your resource xml to smothing like this,

<yourpackagename.ExpandableHeightListview
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/White"
            android:scrollbars="none"
            android:orientation="vertical"
            android:fadingEdge="none">
            </cyourpackagename.ExpandableHeightListview>

This will solve your scrolling problem of ListView, as now the parent scrolling is managed by ScrollView rather then ListView.

ListView inside ScrollView is not scrolling on Android - Stack Overflo...

android listview android-layout scrollview
Rectangle 27 4

Its a bad practice to have two different scrolling views together. ListView itself has its own scrolling functionality and height is auto adjusted according to Adapter settings for your row items (Keelping in mind, we are not setting specific height to ListView in our Layout xml). However in your case, you can replace ListView with a class and this will adjust height for your ListView keeping in mind that your ListView is inside ScrollView.

public class ExpandableHeightListview extends ListView
{

    boolean expanded = false;

    public ExpandableHeightListview(Context context)
    {
        super(context);
    }

    public ExpandableHeightListview(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightListview(Context context, AttributeSet attrs,int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

Now extend your ListView with this class, just change your ListView tag from your resource xml to smothing like this,

<yourpackagename.ExpandableHeightListview
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/White"
            android:scrollbars="none"
            android:orientation="vertical"
            android:fadingEdge="none">
            </cyourpackagename.ExpandableHeightListview>

This will solve your scrolling problem of ListView, as now the parent scrolling is managed by ScrollView rather then ListView.

ListView inside ScrollView is not scrolling on Android - Stack Overflo...

android listview android-layout scrollview
Rectangle 27 10

scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {  
        public void adjustmentValueChanged(AdjustmentEvent e) {  
            e.getAdjustable().setValue(e.getAdjustable().getMaximum());  
        }
    });

The only problem with this is that you can't adjust the scrollbar manually.

How to auto scroll to bottom in Java swing - Stack Overflow

java swing scroll jpanel jscrollpane
Rectangle 27 5

I had a similar issue, that was driving me crazy! I was looking for an anchor with a #, and also searching for a javascript action forcing it to scroll to the window bottom.

It turns out that the AJAX code that I was loading had an anchor at the bottom (a link to my website). The web browser rendered the code such that it went to the only anchor on the page.

To eliminate this, I added a negative tabindex to the anchor:

<a href="http://www.keppiehed.com" tabindex="-1">Keppiehed.com</a>

According to the first answer at How to ignore HTML element from tabindex?, The W3C HTML5 specification supports negative tabindex values.

I know this post is a little old, but I hope it helps you and others with this hard to diagnose problem!

@FahadIshaque, I'm glad it worked out for you! That is why i turn to StackOverflow so much, and add answers to even old questions when I have spend a bit of time researching them.

After seeing this answer a lightbulb clicked in my head and found out I also had a link in my loaded content that was causing the scroll to the bottom. Thank you for the insight! Setting the tabindex solved it for me.

JQuery Dialog Box auto scrolling to bottom - Stack Overflow

jquery jquery-ui jquery-ui-dialog
Rectangle 27 13

I totally agree with the above answer. The only thing is that for me the solution was set the code in viewDidAppear

viewDidAppear 
{
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
}

When I used the viewWillAppear the scrolling didn't work.

viewDidAppear has the disadvantage that you see the collection view moving/flashing, the view has already appeared (as the name implies) ... I really wonder why Apple has not designed this not so unusual case in a nicer way, the viewDidLayoutSubviews way is a bit awkward

ios - UICollectionView auto scroll to cell at IndexPath - Stack Overfl...

ios uicollectionview
Rectangle 27 11

- (void)viewDidLayoutSubviews
{     
   [self.collectionView layoutIfNeeded];
   NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
   NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
   NSIndexPath *nextItem = [NSIndexPath indexPathForItem:someInt inSection:currentItem.section];

   [self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}

ios - UICollectionView auto scroll to cell at IndexPath - Stack Overfl...

ios uicollectionview
Rectangle 27 8

your_CollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
let indexPath = IndexPath(row: itemIndex, section: sectionIndex)

collectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.right, animated: true)
UICollectionViewScrollPosition.CenteredHorizontally / UICollectionViewScrollPosition.CenteredVertically

ios - UICollectionView auto scroll to cell at IndexPath - Stack Overfl...

ios uicollectionview
Rectangle 27 5

When you add/remove components for a panel you should invoke revalidate() on the panel to make sure the components are laid out properly.

Then, if you want to scroll to the bottom then you should be able to use:

JScrollBar sb = scrollPane.getVerticalScrollBar();
sb.setValue( sb.getMaximum() );

How to auto scroll to bottom in Java swing - Stack Overflow

java swing scroll jpanel jscrollpane
Rectangle 27 13

As I explained in a comment, the scroll isn't actually changing. The window's scroll is based on "distance from top" (DfT). That is to say that if you have no scrollbar, you're DfT is 0. Once the scrollbars are introduced you now have a distance to work with.

Because the content is getting longer the viewport is only so many pixels high some content "falls off" the bottom of the page (prepend is making the DfT off by the height of your new element).

Best way I can think of is to counter it with the height of the new element. This allows you to scroll to a position then, as new elements are added, you modify the scroll position accordingly.

And the code (for reference):

var f = function(){
    var t = $(window).scrollTop(),      // Window's current scroll position
        $d = $(d()).prependTo('#main'), // store the new element
        h = $d.outerHeight();           // also get its height
    if (t){                             // Only adjust if they've scrolled
        $(window).scrollTop(t + h);     // add the delta to the scroll position
    }
    setTimeout( f, 1000 );
};

And, append has nothing to do with DfT and I didn't have any problem. Right?

@goldenparrot: Not that it has no relevance, it's just that append doesn't shift a lot of elements down. However, if you had two lists on a page and you keep appending elements to the list on top, the second lists' elements would shift and you'd have the same problem.

@goldenparrot: An example of what I mean here (using append instead of prepend). Notice it matters now with content after it.

Thank you. I think I got the gist of it now.

This is exactly what I was looking for. Thanks for the solution and the explanation about the "DfT". :D

jQuery prepend - prevent auto scroll - Stack Overflow

jquery scroll prepend
Rectangle 27 5

You can use GCD to dispatch the scroll into the next iteration of main run loop in viewDidLoad to achieve this behavior. The scroll will be performed before the collection view is showed on screen, so there will be no flashing.

- (void)viewDidLoad {
    dispatch_async (dispatch_get_main_queue (), ^{
        NSIndexPath *indexPath = YOUR_DESIRED_INDEXPATH;
        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    });
}

This was the only approach that worked for me

ios - UICollectionView auto scroll to cell at IndexPath - Stack Overfl...

ios uicollectionview
Rectangle 27 24

OnTouchListener
ListView
lvCustomList.setOnTouchListener(new OnTouchListener() {
    @Override
        public boolean onTouch(View v, MotionEvent event) {

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

        return false;
    }
});

this is hiding keyboard properly after touching listview but im not able to scroll the list

i got the solution on your above code 'return false' give the required solution

auto hides keyboard after scrolling ListView on android - Stack Overfl...

android android-listview scroll keyboard
Rectangle 27 22

OnTouchListener
ListView
lvCustomList.setOnTouchListener(new OnTouchListener() {
    @Override
        public boolean onTouch(View v, MotionEvent event) {

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

        return false;
    }
});

this is hiding keyboard properly after touching listview but im not able to scroll the list

i got the solution on your above code 'return false' give the required solution

auto hides keyboard after scrolling ListView on android - Stack Overfl...

android android-listview scroll keyboard