android


One method for several buttons?


Instead of have to call separate method for each button, would it be possible to just use one method and check which button it was that was clicked by it's id?
I'm using the simple way like android:onClick="activateButtons"
public void activateButtons(View v)
{
if(?? == btn_1)
{
Code for button with ID = btn_1
}
if(?? == btn_1)
{
Code for button with ID = btn_2
}
}
You can do that if you tag the buttons with an Id. Set the android:id="#+id/yourbuttonid" in the xml file. Then do if(v.getId() == R.id.yourbuttonid) in the onClick method.
Yes it is possible. You can assign specific ids(eg: button1) for buttons and then call v.getId() in activateButtons(onclick method) to check if it is equal to R.id.button1 and then do your operation there.
Cheers,
Richie
public void onClickKeyPad(View view)
{
EditText text = (EditText)findViewById(R.id.text_password);
switch(view.getId())
{
case R.id.button_0:
text.setText(text.getText() + "0");
break;
case R.id.button_1:
text.setText(text.getText() + "1");
break;
case R.id.button_cancel:
finish();
break;
default:
break;
}
}

Related Links

Android: Working with Notification Buttons in Expanded Custom Layout
create an alert for the date and time stored into sqlite
How to check the CheckBox of ListView when item clicked?
how to know the android version to which device is going to upgrade to
How to restrict or permit people from using your Content provider
How to call another screens from one screen that have seven buttons
Memory issue in my android
Auto complete address search Android Map v2 [closed]
Android - What is happens after calling startScan method of WifiManager
How to scale imageview image to specific size?
ClassCastException: SoapPrimitive can't be cast to SoapObject (Apparently there is no SoapPrimitive here)
Delete ParseObject matching Query
Simple Android app crashes when calling Button Method
Android diagonal triangle buttons
Android - communicate with the backend
Access to view elements in fragments

Categories

HOME
android
shell
porting
checkstyle
linkedin
devise
cublas
tail
azure-container-service
spin
fhir
jxl
favicon
cumulocity
delphi-xe5
apache2.4
drive
asciidoctor-pdf
user-experience
rubymotion
springfox
jquery-ias
dpdk
object-storage
onload
surveymonkey
jquery-multiselect
pylons
delimiter
errbot
sqsh
alm
event-flow
anki
python-2.6
r-grid
twos-complement
dynamic-jasper
openproject
cd
metaprogramming
mongoose-schema
visa
odoo-website
portforwarding
identification
mongotemplate
compiler-design
android-testing
pybrain
omxplayer
uicolor
rspec2
scaffolding
cifilter
shopping
kango-framework
angular-ui-typeahead
spreadjs
lz77
libav
cycle2
opl
xenomai
pypiserver
sony-lifelog-api
umlgraph
qtwebengine
iiop
structuremap3
maven-scm
decoupling
twitter-bootstrap-rails
spring-3
scriptaculous
ebay-lms
cocoalibspotify-2.0
rolify
asdf
actiondispatch
composite-component
alice
os.system
rmiregistry
subproject
database-create
floating-point-exceptions
jquery-address
user-tracking
exponentiation
tournament
aspmenu
qtabbar
fault-tolerance
contentpresenter
cardspace

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