In the last post i've shown the advantages of AndroidAnnotations against the tratidional instantiation way from Androd using castings while using the method findById(). AndroidAnnotatations are fine, but if for some reason (additional depencency, License or somethign else) you can't use it, there is an alternative to it. Generics solves this problem in my opinion also very elegant.
Pre-Conditions
Do the steps of post AndroidAnnotations and come back to this post when you are done.BaseActivity with generics
Create the following class in your sample project (Attention: for some reason i don't know, while inserting code snippets here, generic types are not displayed correctly. For this reason i copied the generics like a String "T extends View". Please remove the Strings from it after copying):import android.app.Activity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.view.View.OnTouchListener; @SuppressWarnings("unchecked") public class BaseActivity extends Activity { /** * Convenience method. Does exactly the same as {@link BaseActivity#initById(Class, int)} * @param safetyCheck the class type to be returned * @param id the defined R.id... in the layout xml file * @return a reference of the specified class type. */ public <"T extends View"> T getReferenceById(ClasssafetyCheck, final int id) { return initById(safetyCheck, id); } /** * Initializes views over id's. * @param safetyCheck the class type to be returned * @param id the defined R.id... in the layout xml file * @return a reference of the specified class type. */ public <"T extends View"> T initById(Class safetyCheck, final int id) { return (T) findViewById(id); } /** * Initializes views over id's with onClickListeners. * @param id the defined R.id... in the layout xml file * @param callback a concrete implementation of {@link OnClickCallback} * @return a reference of the specified class type in {@link OnClickCallback}. */ public <"T extends View"> T initWithListener(final int id, final OnClickCallback callback) { T type = (T) findViewById(id); type.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { callback.onClickPerformed((T) view); } }); return type; } /** * Initializes views over id's with onTouchListeners. * @param id the defined R.id... in the layout xml file * @param callback a concrete implementation of {@link OnTouchCallback} * @return a reference of the specified class type in {@link OnTouchCallback}. */ public <"T extends View"> T initWithListener(final int id, final OnTouchCallback callback) { T type = (T) findViewById(id); type.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { return callback.onTouchPerfomed((T) view, event); } }); return type; } /** * Initializes views over id's with onLongClickListeners. * @param id the defined R.id... in the layout xml file * @param callback a concrete implementation of {@link OnLongClickCallback} * @return a reference of the specified class type in {@link OnLongClickCallback}. */ public <"T extends View"> T initWithListener(final int id, final OnLongClickCallback callback) { T type = (T) findViewById(id); type.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View view) { return callback.onLongClickPerformed((T) view); } }); return type; } /** * Implement this interface and add an instance of it to the method * {@link BaseActivity#initWithListener(int, Callback)} while initializing your components * (Views) * @author Ricardo Ferreira * @since 20/12/2013 * @version 1.0 */ public interface OnClickCallback<"T extends View"> { void onClickPerformed(T view); } /** * Implement this interface and add an instance of it to the method * {@link BaseActivity#initWithListener(int, Callback)} while initializing your components * (Views) * @author Ricardo Ferreira * @since 20/12/2013 * @version 1.0 */ public interface OnLongClickCallback<"T extends View"> { boolean onLongClickPerformed(T view); } /** * Implement this interface and add an instance of it to the method * {@link BaseActivity#initWithListener(int, Callback)} while initializing your components * (Views) * @author Ricardo Ferreira * @since 20/12/2013 * @version 1.0 */ public interface OnTouchCallback<"T extends View"> { boolean onTouchPerfomed(T view, MotionEvent event); } }
Modify your MainActivity
Copy paste the next code segment into your MainActiviy. (Attention: for some reason i don't know, while inserting code snippets here, generic types are not displayed correctly. For this reason i copied the generics like a String "T extends View". Please remove the Strings from it after copying):import android.os.Bundle; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends BaseActivity { private OnClickCallback<"Button"> btnClickCallback = new ButtonClickCallback(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initById(TextView.class, R.id.textView1); initWithListener(R.id.button1, btnClickCallback); initWithListener(R.id.button2, btnClickCallback); initWithListener(R.id.button3, btnClickCallback); initWithListener(R.id.button4, btnClickCallback); initWithListener(R.id.button5, btnClickCallback); initWithListener(R.id.button6, btnClickCallback); initWithListener(R.id.button7, btnClickCallback); initWithListener(R.id.button8, btnClickCallback); initWithListener(R.id.button9, btnClickCallback); initWithListener(R.id.button10, btnClickCallback); initWithListener(R.id.button11, btnClickCallback); initWithListener(R.id.button12, btnClickCallback); initWithListener(R.id.button13, btnClickCallback); initWithListener(R.id.button14, btnClickCallback); initWithListener(R.id.button15, btnClickCallback); } private class ButtonClickCallback implements OnClickCallback<"Button"> { @Override public void onClickPerformed(Button type) { switch (type.getId()) { case R.id.button1: // returns a Button... do something useful with... if you need to... getReferenceById(Button.class, R.id.button1); handleClick(); break; case R.id.textView1: // returns a TextView... do something useful with... if you need to... getReferenceById(TextView.class, R.id.textView1); handleClick(); break; default: handleClick(); break; } } } private void handleClick() { Toast.makeText(this, "Generics are even cooler!", Toast.LENGTH_SHORT).show(); } }
That's all. Hope you like it.
😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO 😱👇
Be sure to read, it will change your life!
Show your work by Austin Kleon: https://amzn.to/34NVmwx
This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practices: https://amzn.to/30WQSm2
Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv
This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp
Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy
😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO 😱👆