Android - how to ensure that checked radiobutton in ScrollView will be visible on screen?

2011-11-02 android android-layout radio-button

I have RadioGroup with 19 RadioButtons in a ScrollView in DialogPreference. There are visible only around first 7 of RadioButtons in dialog. All is working fine, only one question. How to ensure that checked RadioButton will be always visible on screen after bind the dialog? How to scroll in ScrollView programmatically to show hidden checked RadioButton ?

Thank you for your help or suggestions.

Answers

You should be able to do something like this

int top = checkedRadioButton.getTop();
scrollView.scrollTo(0,top);

This is what worked for me.

final Scrollview scrollview = findViewById(R.id.myScrollView);
final RadioGroup rg = findViewById(R.id.myRadioGroup);
int position = //checked position

    scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.scrollTo(0, rg.getChildAt(position).getTop());
        }
    });

Related