• Monday, March 20, 2017

    Android - New and easy way to handle findViewById.

    Syed Masoom Rizvi
    Field and Method binding method for android.

    A very easy method to bind your views to class.  It uses annotate fields @BindView and a view ID that automatically cast your view to field. 


    Old codes:

    class MyActivity extends Activity{
        Textview m1, m2;

        @Override
        protected void onCreate(.......){
          ..........
          ..........
          m1 =(TextView) findViewById(R.id.tv_m1);
          m2 =(TextView) findViewById(R.id.tv_m2);
       }
    }

    New way to write this code:

    class MyActivity extends Activity{
        @BindView(R.id.tv_m1) TextView m1;
        @BindView(R.id.tv_m2) TextView m2;
        
        @Override
        protected void onCreate(.....){
            .........
            .........
            ButterKnife.bind(this);
        }

    }