title: Scroll to the top of recyclerView in Android using a button
body: <p>I have used <strong>recyclerView</strong> in my application and want to add a <strong>scroll up to top button</strong> in that, I have added the button in it but could not be able to scroll my recyclerView on top when I clicks the button, also I have <strong>reversed</strong> my RecyclerView so that the new post I add in it comes on top not on bottom. please help me, much thanks in advance. I have also used <code>getPosition</code> method but that did not work for me.</p>
  
  <blockquote>
    <p>I am using Android Studio</p>
  </blockquote>
  
  <p><strong>scroll action class file code:</strong></p>
  
  <pre><code>public class HomePage extends AppCompatActivity {
  
  private FirebaseAuth mAuth;
  private RecyclerViewPager mrecyclerView;
  private DatabaseReference mDatabaseReference;
  private Button rollUp;
  public static int recyclerPos = 0;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_home_page);
      mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("post");
      mrecyclerView = (RecyclerViewPager) findViewById(R.id.list);
      final LinearLayoutManager layout = new LinearLayoutManager(HomePage.this, LinearLayoutManager.VERTICAL, false);
      layout.setReverseLayout(true);//&lt;---- to get new posted data on top of list
      layout.setStackFromEnd(true);//&lt;-----
      mrecyclerView.setLayoutManager(layout);
      rollUp = (Button) findViewById(R.id.roll_up);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      mAuth = FirebaseAuth.getInstance();
  
      this.mrecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
          int mLastFirstVisibleItem = 0;
  
          @Override
          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
          }
  
          @Override
          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
              super.onScrolled(recyclerView, dx, dy);
              final int currentFirstVisibleItem = layout.findFirstVisibleItemPosition();
  
              if (currentFirstVisibleItem &lt; this.mLastFirstVisibleItem) {
                  HomePage.this.getSupportActionBar().hide();
              } else if (currentFirstVisibleItem &gt; this.mLastFirstVisibleItem) {
                  HomePage.this.getSupportActionBar().show();
              }
              this.mLastFirstVisibleItem = currentFirstVisibleItem;
          }
      });
  
  }
  
      private void sentToStart() {
      Intent intent = new Intent(HomePage.this, LoginPage.class);
      startActivity(intent);
      finish();
  }
  @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      super.onCreateOptionsMenu(menu);
      getMenuInflater().inflate(R.menu.menu_home_page, menu);
      return true;
  }
  @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      super.onOptionsItemSelected(item);
      int id = item.getItemId();
  
      if (id == R.id.action_settings){
          Intent intentSettings = new Intent(HomePage.this,settings.class);
          startActivity(intentSettings);
          LinearLayoutManager layoutManager = ((LinearLayoutManager) mrecyclerView.getLayoutManager());
          recyclerPos = layoutManager.findFirstVisibleItemPosition();
      }
      if (id == R.id.roll_up){
          LinearLayoutManager layoutManagerroll = ((LinearLayoutManager) mrecyclerView.getLayoutManager());
          recyclerPos = layoutManagerroll.findFirstVisibleItemPosition();
          layoutManagerroll.scrollToPositionWithOffset(0,0);
  
      }
      return super.onOptionsItemSelected(item);
  }
  </code></pre>