Search code examples
javaandroidgenericsonclicklistener

setting OnClickListener generic method


I have an activity, that have different TextView's. Now, by clicking on each TextView it should open another activity. I implemented a generic way, because I wanted to remove redundant code. So I just wanted to ask about your opinion if I am using the generic-concept in java in a correct way and if you would use a similar approach to set Listeners to multiple Views in Android?

(I used generic names for demonstration purposes and I didnt want to use the onClick declaration on the xml-Layout):

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView activity1 = (TextView) findViewById(R.id.activity1);
    TextView activity2 = (TextView) findViewById(R.id.activity2);
    TextView activity3 = (TextView) findViewById(R.id.activity3);
    TextView activity4 = (TextView) findViewById(R.id.activity4);

    setOnClickListener(activity1, Activity1.class);
    setOnClickListener(activity2,  Activity2.class);
    setOnClickListener(activity3,  Activity3.class);
    setOnClickListener(activity4, Activity4.class);
}


private void setOnClickListener (TextView textView, final Class<? extends AppCompatActivity> activityClass){

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent activityIntent = new Intent(MainActivity.this, activityClass);
            startActivity(activityIntent);
        }
    });
}

}


Solution

  • I would recommend overriding View.OnClickListener. Decoupling the code from the Activity has the added benefit of decoupling your logic from the Activity itself, so you could use the GenericClickListener in different contexts. Something like this:

    public class GenericClickListener implements View.OnClickListener {
        public GenericClickListener(Activity activity, Class<? extends AppCompatActivity> activityClass) {
            @Override
            public void onClick(View v) {
                Intent activityIntent = new Intent(activity, activityClass);
                startActivity(activityIntent);
            }
        } 
    }
    

    And then in your onCreate() method:

    TextView activity1 = (TextView) findViewById(R.id.activity1);
    TextView activity2 = (TextView) findViewById(R.id.activity2);
    TextView activity3 = (TextView) findViewById(R.id.activity3);
    TextView activity4 = (TextView) findViewById(R.id.activity4);
    
    activity1.setOnClickListener(new GenericClickListener(thia, Activity1.class));
    activity2.setOnClickListener(new GenericClickListener(this, Activity2.class));
    activity3.setOnClickListener(new GenericClickListener(this, Activity3.class));
    activity4.setOnClickListener(new GenericClickListener(this, Activity4.class));
    

    Apart from that, your implementation seems like a reasonable approach.