Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,810,757 Members 36,700 Now Online
XDA Developers Android and Mobile Development Forum

[Guide] Listeners in Java development

Tip us?
 
mohamedrashad
Old
(Last edited by mohamedrashad; 1st June 2014 at 07:33 PM.)
#1  
mohamedrashad's Avatar
Senior Member - OP
Thanks Meter 443
Posts: 941
Join Date: Nov 2012
Location: ismailia
Default [Guide] Listeners in Java development


You are new to java development and want to get buttons working?
Maybe you are a Pro but want a reminder?
whatever you are this Guide is to help you to make buttons/check boxes...etc working and functional


Some people are distracted between guides over internet and want the easiest way to get their project working, me too



Steps :

1-Define the button :

Code:
Button btn1;
Checkbox chkbox1;
RadioButton radio1;
2- Intialize it :

Code:
btn1 = (Button) findViewById(R.id.btn1);
chkbox1= (Checkbox ) findViewById(R.id.chkbox1);
radio1= (RadioButton) findViewById(R.id.radio1);
3-Add the listener :

Button:

Code:
btn1.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View arg0) {

                  //Write awesome code here
			}
		});
CheckBox :

Code:
chkbox1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				if (start.isChecked()) {

				//if the checkbox checked
					
				} else {
				
					//if not checked 
				}
			}
		});
	}
radio button:

Code:
public void onRadioButtonClicked(View view) {
  
    boolean checked = ((RadioButton) view).isChecked();
    
 
    switch(view.getId()) {

        case R.id.radio1:
            if (checked){

                     }
         else{

      }
           
            break;
       
    }
}
or use it in a radio Group :

Code:
public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();
    
    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio1:
            if (checked)
            //Write code
            break;
        case R.id.radio2:
            if (checked)
               //Write code
            break;
    }
}

Also insted of this you can use a onCheckedChanged for a radio button (Thanks for GalaxyInABox)

Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (radioGroup.getCheckedRadioButtonId()) {
            //Code
        }
}

__________________________________________________ __________________
__________________________________________________ __________________


Also you can implement a Onclicklistener for the whole class to save resources : (thanks for @Jonny )

after defining and initializing your objects add this :


Code:
OnClickListener click_listener = new OnClickListener() {
			public void onClick(View view) {
				int id = view.getId();
				if (id == your_id) {
					//do stuff for this object
				} else if (id == your_id2) {
		        	//do other stuff for diffrent object
				} else if (id == your_id3) {
		        	//and so on
				}
			}
		};

To do list :

-add on touch listeners
-add on drag listeners





Note : you can add a click listener to almost any thing (Textview or imageView or even EditText) just using the same method of adding listener to button

also there is some other ways to add a listener but this is the fastest and less disturbing

If this guide is useful, press thanks
Pi Locker Major Update 4.0 >>> Google play || XDA

Founder of Pi Developers

Website || Github || Blog

Floata || Pi Locker || Socials addict

The Following 19 Users Say Thank You to mohamedrashad For This Useful Post: [ Click to Expand ]
 
sak-venom1997
Old
#2  
sak-venom1997's Avatar
Senior Member
Thanks Meter 405
Posts: 920
Join Date: Feb 2013
Location: Lucknow

 
DONATE TO ME
@ OP
CheckBox and RadioButtons don't they provide a CheckedChangeListener ?

Sent from my GT-S5302 using Tapatalk 2
The Following User Says Thank You to sak-venom1997 For This Useful Post: [ Click to Expand ]
 
giuliomvr
Old
#3  
Senior Member
Thanks Meter 213
Posts: 190
Join Date: May 2013
Location: Cologne
Quote:
Originally Posted by sak-venom1997 View Post
@ OP
CheckBox and RadioButtons don't they provide a CheckedChangeListener ?
Yes, and now you can use
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (radioGroup.getCheckedRadioButtonId()) {
            //Code
        }
}
to get the checked button. They are pretty much the same, but you can use view.getTag() easier in the first one.

And @mohamedrashad please show how to put the listener into a inner class. Many people don't know/use it, but it's that useful!
The Following User Says Thank You to giuliomvr For This Useful Post: [ Click to Expand ]
 
sak-venom1997
Old
(Last edited by sak-venom1997; 28th May 2014 at 07:11 PM.)
#4  
sak-venom1997's Avatar
Senior Member
Thanks Meter 405
Posts: 920
Join Date: Feb 2013
Location: Lucknow

 
DONATE TO ME
Quote:
Originally Posted by GalaxyInABox View Post
Yes, and now you can use
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (radioGroup.getCheckedRadioButtonId()) {
            //Code
        }
}
to get the checked button. They are pretty much the same, but you can use view.getTag() easier in the first one.
I meant that the op shuld edit this guide and use those instead of OnCickListeners
Quote:
Originally Posted by GalaxyInABox View Post
And @mohamedrashad please show how to put the listener into a inner class. Many people don't know/use it, but it's that useful!
ya new with java8 it will be a nice usage scenario of lambadas

Sent from my GT-S5302 using Tapatalk 2
 
mohamedrashad
Old
#5  
mohamedrashad's Avatar
Senior Member - OP
Thanks Meter 443
Posts: 941
Join Date: Nov 2012
Location: ismailia
Quote:
Originally Posted by GalaxyInABox View Post
Yes, and now you can use
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (radioGroup.getCheckedRadioButtonId()) {
            //Code
        }
}
to get the checked button. They are pretty much the same, but you can use view.getTag() easier in the first one.

And @mohamedrashad please show how to put the listener into a inner class. Many people don't know/use it, but it's that useful!
Quote:
Originally Posted by sak-venom1997 View Post
@ OP
CheckBox and RadioButtons don't they provide a CheckedChangeListener ?

Sent from my GT-S5302 using Tapatalk 2
ok, i will add this
Pi Locker Major Update 4.0 >>> Google play || XDA

Founder of Pi Developers

Website || Github || Blog

Floata || Pi Locker || Socials addict

 
rafalniski
Old
#6  
Junior Member
Thanks Meter 2
Posts: 3
Join Date: May 2014
You can also add onClick property in XML and then handle it in a code.
The Following User Says Thank You to rafalniski For This Useful Post: [ Click to Expand ]
 
brian.houaiss
Old
#7  
Senior Member
Thanks Meter 32
Posts: 116
Join Date: Feb 2011
Location: Vancouver
Awesome tutorial! Thank you very much!
Please, you could share more related knowledge. It's really useful!
 
SKAm69
Old
#8  
SKAm69's Avatar
Senior Member
Thanks Meter 60
Posts: 565
Join Date: Oct 2009
Also, an activity can be a listener. In this case:
MyActivity implements onClickListener {

btn1.setOnClickListener(this);

public void onClick (View v) {
//your code
}
}
The Following User Says Thank You to SKAm69 For This Useful Post: [ Click to Expand ]
 
Tolriq
Old
#9  
Senior Member
Thanks Meter 156
Posts: 139
Join Date: Oct 2007
For this kind of stuff, using some well known libraries from well known Android dev is a must.

https://github.com/JakeWharton/butterknife

Very powerfull, super easy to use, error prone and without any performance impact.
Yatse 2 : Media Center Remote Control for Touch Screens
Yatse, the Xbmc Remote and Widgets for Android
The Following User Says Thank You to Tolriq For This Useful Post: [ Click to Expand ]
 
mohamedrashad
Old
#10  
mohamedrashad's Avatar
Senior Member - OP
Thanks Meter 443
Posts: 941
Join Date: Nov 2012
Location: ismailia
Quote:
Originally Posted by rafalniski View Post
You can also add onClick property in XML and then handle it in a code.
Quote:
Originally Posted by SKAm69 View Post
Also, an activity can be a listener. In this case:
MyActivity implements onClickListener {

btn1.setOnClickListener(this);

public void onClick (View v) {
//your code
}
}

will add them both, although I don't like this way
Pi Locker Major Update 4.0 >>> Google play || XDA

Founder of Pi Developers

Website || Github || Blog

Floata || Pi Locker || Socials addict


The Following User Says Thank You to mohamedrashad For This Useful Post: [ Click to Expand ]
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes