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

After enabling webcam to emulator, my projects crash
Smack library warning
android - how to get the IP address of a BLE beacon device
after get an accesstoken ?in oauth
Show options spinner in action bar
Can't change target SDK in Android Studio
How to set font size in Android webView fixed , even if font size changed in Settings->Display->Font?
Azure Oauth authentication on embeded web browesers will be blocked on 20 April, 2017 warning came
Retrofit - Base URL is changed when request process is being executed
how to blur image in android and set it to relative layout
Xamarin.Android happens error on Android 7
Firebase database get children long value pass 0 to my class
EditText and Extra View on Android SoftKeyboard
Getting Black Screen when displaying Gallery picture into ImageView Android
No results when I set sort filters
Can I flash with fastboot image created with adb pull? [closed]

Categories

HOME
assembly
merge
phpmyadmin
depth
numbers
jelastic
knitr
opendj
edge
nested-if
pearson-correlation
polling
tvos
google-cloud-datalab
syntaxnet
ibeacon-android
plist
why3
onedrive-api
dynatrace
production
lilypond
question2answer
parcelable
highstock
google-api-client
data.stackexchange.com
asymptotic-complexity
outliers
inversion-of-control
utorrent
openwhisk
sendbird
lubridate
hivemq
jpad
graphhopper
diacritics
linq-to-objects
drupal-theming
vaadin-elements
dbcontext
podscms
semantic-logging
colorbar
edit
headless
css-transforms
chromium-os
flex-monkey
adafruit
gogs
clipboard.js
mediametadataretriever
fink
grass
terracotta
roadkill-wiki
viola-jones
vacuum
ocra
typeconverter
kubuntu
jquery-forms-plugin
shopizer
phpldapadmin
qtwebengine
android-cookiemanager
mruby
jbake
administration
webfinger
rickshaw
linkedin-jsapi
template-deduction
ss7
vsx
koala
android-audiorecord
visual-studio-express
printdialog
flex++
time.h
scmmanager
rmiregistry
garbage
cuda-gdb
fusefabric
cgimage
denied
role
winmain
google-ajax-api
entitydatasource
pylucene
qglwidget
instantiationexception
isapi-extension
zen
wysiwym

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