Luckily the Android SDK allows any view to be clickable; all we need to do is assign an OnClickListener callback handler to the targeted view.
Assume you have this TextView in your layout file
All you need then is to use the method setOnClickListener() of the view to assign a callback object to it. These callbacks are objects that implement the interface View.OnClickListener, as show below in the all-purposed method setupEventsListeners()
private void setupEventsListeners(){ View myTextView = findViewById(R.id.myTextView); myTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View caller) { Toast .makeText(caller.getContext(), "You have clicked on a TextView", Toast.LENGTH_LONG) .show(); } }); }
You need to assign an click handler to it as soon as possible, let's say, when the activity is created.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // your other initialization code here setupEventsListeners(); }
And then you are done!
No comments:
Post a Comment