android


override empty method in custom view and handle it in MainActivity


To make thinks simple, I have made a custom calendar view (RelativeLayout). Events (linearLayouts) are placed inside it. When I click on the event I want to be able to do something, but I want this something to be done in my MainActivity. In other words I want to override the empty method in my custom view and handle it in my MainActivity.
eventView.setTag(event.getDatabaseId());
eventView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("test", "" + v.getTag());
onEventClick((int) v.getTag());
}
});
eventView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
onEventLongClick((int) v.getTag());
return false;
}
});
}
...
public void onEventClick(int eventId) {
}
public void onEventLongClick(int eventId) {
}
So in my MainActivity I instantiate my view:
cv = ((CalendarView)findViewById(R.id.calendar_view));
I want to do something like:
cv = ((CalendarView)findViewById(R.id.calendar_view)) {
#Override
public void onEventClick(int eventId) {
// here I will fetch the data and display a dialog
}
#Override
public void onEventLongClick(int eventId) {
// here I will fetch the data and display a dialog
}
};
I want to be able to use my calendar else where so this is why I don't want to tie the click functionality inside my custom view
I tried making my view abstract but got nowhere either.
EDIT: eventView's are in a viewpager
You should use Interface on your custom view.
In its most common form, an interface is a group of related methods with empty bodies. You can find more detailed instructions here
So in your case I assume you have a RelativeLayout as your custom view
public class MyCalendarLayout extends RelativeLayouts{
//Your constructor methods
public interface MyEventListener {
public void onEventClicked();
public void onEventLongClicked();
}
private MyEventListener myEventListener;
public void setListener(MyEventListener myEventListener) {
this.myEventListener= myEventListener;
}
}
Then on your Activity you can use it like this
myCustomView.setListener(new MyEventListener() {
#Override
public void onEventClicked() {
// TODO Auto-generated method stub
}
#Override
public void onEventLongClicked() {
// TODO Auto-generated method stub
}
});
Or you can use
YourActivity extends Activity implements MyEventListener
and you can call
myCustomView.setListener(this);
on your activity and let the handle override methods to your activity.

Related Links

Playing background sound loop
How to distinguish calls to onReceive
UI controls' width
Received Android src from dev, need to make tweaks, how do I load it on eclipse?
Cannot make Android In-App purchase using test account
Returning after a period of inactivity in my Android Application causes destroy set Values
Tether with mobile but use same IP address
AudioCache Heap size overflow issue req size: 1053184, max size: 1048576
I dont want ItemizedOverlay to draw a image on every overlayitem it haver
GoSms steals focus from alertDialog
Can't get Android SDK or Eclipse ADT to work
Scan files from hard drive with Android [closed]
Sending SMS with Android Emulator through telnet
how to use CCJumpTo in cocos2d-android
How to display a text in android after a button click in android?
How can I get a profile picture of an athlete from web page

Categories

HOME
algorithm
phantom-dsl
search
api
c++11
apache-jena
depth
character-encoding
apache-spark-mllib
battery
switch-statement
dojo
babeljs
jetty
google-admin-sdk
crystal-reports-2013
project
little-proxy
dkim
ipmitool
angular-dart
renjin
google-picker
replication
pimcore
mongoid6
monit
direct3d11
dapper-extensions
windows-server-2003
flooding
ms-access-2003
8051
android-things
sqlpackage
xbox
paragraph
cart
android-gridview
arduino-ide
numpy-broadcasting
android-augmented-reality
c9.io
trigonometry
odoo-website
escpos
oracle-service-bus
uiimage
xbmc
textsum
proget
cnc
wcftestclient
yii2-api
carrot2
arq
scalding
appdomain
spring-data-couchbase
oracleclient
boo
oozie-coordinator
meteor-packages
seek
google-cloud-console
macaulay2
eigenvector
php-socket
query-by-example
findfirst
qbxml
highland.js
toran-proxy
java-money
themoviedb-api
r-package
ess
rubaxa-sortable
dock
atoi
alloy-ui
entity-framework-4.3
public-html
adehabitathr
apache-spark-1.3
at-job
packet-sniffers
boost-program-options
contrast
formview
appserver
lnk
maven-scm
ebay-lms
flex++
kaleidoscope
rmiregistry
apache-whirr
azman
infobright
google-maps-mobile
pinch
workflow-services
django-paypal
twitterizer
purepdf
lemmatization

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App