While building apps with too many controls or views on same page then we need to use ScrollView so we can view each and every view inside an activity.But sometimes we want to focus on specific view on button click over any other action to get attentionĀ from user. for example if you have 20 EditTexts on a page and 1 button at bottom and you had applied a validation on button click to check if EditText is not empty. but if EditText is not empty then that empty EditText will focus automatically. and in this tutorial we will make same.

But in this tutorial i will show you how you can focus on any view like TextView,ImageView, button etc. code will be as below :

suppose i have 20 ImageView on a page. and i want to make focus on 5th ImageView (and id of 5th imageview is img5) on button click. then i will wrote like this :


private void focusOnView(){
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                your_scrollView.scrollTo(0, your_view.getBottom());
            }
        });
    }

so this will make focus on that imageview but if have is bigger in size then you will not see upper part of image. So to focus on starting part of image you can use below code :


private void focusOnView(){
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                your_scrollView.scrollTo(0, your_view.getTop());
            }
        });
    }

So below is complete code of MainActivity.java class


package com.learnpainless.focusonview;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;

public class MainActivity extends AppCompatActivity {
    private ImageView img5;
    private ScrollView scrollView;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img5 = (ImageView) findViewById(R.id.img5);
        scrollView = (ScrollView) findViewById(R.id.scrollView);
        btn = (Button) findViewById(R.id.submit);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                focusOnView();
            }
        });

    }
    private void focusOnView(){
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                scrollView.scrollTo(0, img5.getTop());
            }
        });
    }
}