Programmatically scroll a ScrollView to focus on a Button or a view - Android

18,548

Solution 1

try

yourScrollView.scrollTo(0, (int) button.getY());

Solution 2

For Kotlin user


your_scrollview.post( { your_scrollview.scrollTo(0, your_view_inside_SV.getY().toInt()) })
Share:
18,548
Matt
Author by

Matt

Updated on July 27, 2022

Comments

  • Matt
    Matt almost 2 years

    I have an Activity that has a ScrollView of 125 buttons. The 125 buttons are Levels in my game that slowly get unlocked (think of Candy Crush Saga). Right now if the user is on the level, let's say, 88 of 125 then the user has to manually scroll all the way down to button 88.

    My question is, how would I make it so that when the Activity loads, it automatically scrolls down and centers the ScrollView on a certain button?

    Below is my code of how the buttons are created and populated programmatically.

    ScrollView scrollView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    
        LinearLayout lay = (LinearLayout)findViewById(R.id.linearlayout);
    
        for(int i = 0; i < 125; i++) {
    
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
            params.weight = 1;
            params.gravity = Gravity.CENTER;
    
            if(i == 0) {
                TextView textview = new TextView(this);
                textview.setLayoutParams(params);
                textview.setTextSize(15);
                textview.setText(c.getResources().getString(R.string.l1to5));
    
                TextView textview2 = new TextView(this);
                textview2.setLayoutParams(params);
                textview.setTextSize(15);
                textview2.setText(c.getResources().getString(R.string.goal100));
    
                TextView textview3 = new TextView(this);
                textview3.setLayoutParams(params);
                textview.setTextSize(15);
                textview3.setText(c.getResources().getString(R.string.catRandom));
    
                if((getResources().getConfiguration().screenLayout & 
                    Configuration.SCREENLAYOUT_SIZE_MASK) == 
                    Configuration.SCREENLAYOUT_SIZE_LARGE) {
                        textview.setTextSize(22);
                        textview2.setTextSize(22);
                        textview3.setTextSize(22);
                } else if((getResources().getConfiguration().screenLayout & 
                    Configuration.SCREENLAYOUT_SIZE_MASK) == 
                    Configuration.SCREENLAYOUT_SIZE_XLARGE) {
                        textview.setTextSize(22);
                        textview2.setTextSize(22);
                        textview3.setTextSize(22);
                }
    
                lay.addView(textview);
                lay.addView(textview2);
                lay.addView(textview3);     
    
                View ruler = new View(this);
                ruler.setBackgroundColor(0xFFC2BEBF);
                lay.addView(ruler, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2));
            }
    
            if(i == 5) {
    
     //  ....
    
    }
    
    
    
            Button button = new Button(this);
            int _id = getResources().getIdentifier("b" + (i + 1), "id", this.getPackageName());
            button.setTag(_id);
            button.setLayoutParams(params);
    
            if((getResources().getConfiguration().screenLayout & 
                Configuration.SCREENLAYOUT_SIZE_MASK) == 
                Configuration.SCREENLAYOUT_SIZE_LARGE) {
                    button.setTextSize(22);
            } else if((getResources().getConfiguration().screenLayout & 
                Configuration.SCREENLAYOUT_SIZE_MASK) == 
                Configuration.SCREENLAYOUT_SIZE_XLARGE) {
                    button.setTextSize(22);
            }
    
            button.setText("Level " + (i + 1));
            final int x = i + 1;
            button.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    singleton.setLevelJustPlayed(x);
                    // ...
                    } else {
                     // ...
                        Intent intent = new Intent(Levels.this, Categories.class);
                        startActivity(intent);
                    }
                }
            });
    
            lay.addView(button);
    
            if(singleton.getLevel() >= (i + 1)) {
                button.setEnabled(true);
            } else {
                button.setEnabled(false);
            }
        }
    // end the onCreate() method
    
  • Matt
    Matt over 10 years
    Ah ok, that is what I am looking for. What is the getY() referring to, though?
  • trippedout
    trippedout over 10 years
    getY() - should be more than enough to find out where the button is lying in your scroll view
  • Zin Win Htet
    Zin Win Htet about 8 years
    It works, but it didn't work with smoothScrollBy => new Handler().post(new Runnable() { @Override public void run() { scrollView.smoothScrollBy(0, centreY); } });