Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,733,782 Members 46,244 Now Online
XDA Developers Android and Mobile Development Forum

[Guide] Listeners in Java development

Tip us?
 
hacimurat
Old
#11  
Junior Member
Thanks Meter 1
Posts: 2
Join Date: Nov 2013
Mohamedrashad. Thanks a lot.

Sent from my P880 using Tapatalk
The Following User Says Thank You to hacimurat For This Useful Post: [ Click to Expand ]
 
Jonny
Old
#12  
Jonny's Avatar
Forum Moderator
Thanks Meter 7822
Posts: 7,832
Join Date: Jul 2011
Location: Cheshire

 
DONATE TO ME
If you have multiple clickable objects then it's best to use just 1 onClickListener for all of them and use a switch on their ID's. This reduces resource usage as you only have 1 listener, not 5, 10 or however many you would have otherwise. It's not essential for this but it is a best practice if you want to streamline your code.

Mobile right now so I can't chuck up an example until tomorrow evening or so.
The Following User Says Thank You to Jonny For This Useful Post: [ Click to Expand ]
 
fikmus24
Old
#13  
Junior Member
Thanks Meter 1
Posts: 2
Join Date: May 2014
You dude had a great thread. Its helping me. Bravoo !!

Sent from my GT-I8190 using XDA Premium 4 mobile app
The Following User Says Thank You to fikmus24 For This Useful Post: [ Click to Expand ]
 
GalaxyInABox
Old
#14  
Senior Member
Thanks Meter 200
Posts: 185
Join Date: May 2013
Location: Bonn
As @Jonny already pointed out: Use your class as a listener instead of creating a new (anonymous) inner class! Say you have a ListView, instead of doing this:
Code:
class MyFragment extends Fragment {
    private void someMethod() {
        ((ListView) getView().findViewById(R.id.someListView)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Code...
            }
        });
    }
}
you can do this:
Code:
class MyFragment extends ListFragment implements AdapterView.OnItemClickListener, View.OnClickListener {
    private void someMethod() {
        ((ListView) getView().findViewById(R.id.someListView)).setOnItemClickListener(this);
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Code...
    }
}
This may look stupid, but when you have many listeners, you can un-clutter it. In my opinion this is the best way. You can also add "this" class as listener for as many ui elements as you want(because all of them extend view, you can use one OnClickListener), then you only need to have a switch statement to distinguish between the views. And voila, you prevented cluttering the code with boilerplate stuff.
 
Jonny
Old
(Last edited by Jonny; 1st June 2014 at 10:41 PM.)
#15  
Jonny's Avatar
Forum Moderator
Thanks Meter 7822
Posts: 7,832
Join Date: Jul 2011
Location: Cheshire

 
DONATE TO ME
Example using code in an app I'm making - app for my school.

Code:
@Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
			
                        // Some code here for view/layouts etc

			websitebutton = (Button) view.findViewById(R.id.website_btn);
			facebookbutton = (Button) view.findViewById(R.id.facebook_btn);
			twitterbutton = (Button) view.findViewById(R.id.twitter_btn);
			websitebutton.setOnClickListener(handler);
			facebookbutton.setOnClickListener(handler);
			twitterbutton.setOnClickListener(handler);

			return view;
		}

		OnClickListener handler = new OnClickListener() {
			public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.website_btn :
                        Uri website = Uri.parse("http://wirralgrammarboys.com/");
                        Intent websiteintent = new Intent(Intent.ACTION_VIEW, website);
                        startActivity(websiteintent);
                        break;
                    case R.id.facebook_btn :
                        Uri facebook = Uri.parse("https://www.facebook.com/WirralGSB");
                        Intent facebookintent = new Intent(Intent.ACTION_VIEW, facebook);
                        startActivity(facebookintent);
                        break;
                    case R.id.twitter_btn :
                        Uri twitter = Uri.parse("https://twitter.com/WGSB");
                        Intent twitterintent = new Intent(Intent.ACTION_VIEW, twitter);
                        startActivity(twitterintent);
                        break;
				}
			}
		};
The Following User Says Thank You to Jonny For This Useful Post: [ Click to Expand ]
 
mohamedrashad
Old
#16  
mohamedrashad's Avatar
Senior Member - OP
Thanks Meter 416
Posts: 879
Join Date: Nov 2012
Location: ismailia
Quote:
Originally Posted by Jonny View Post
Example using code in an app I'm making.

Code:
@Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
			
                        // Some code here for view/layouts etc

			websitebutton = (Button) view.findViewById(R.id.website_btn);
			facebookbutton = (Button) view.findViewById(R.id.facebook_btn);
			twitterbutton = (Button) view.findViewById(R.id.twitter_btn);
			websitebutton.setOnClickListener(handler);
			facebookbutton.setOnClickListener(handler);
			twitterbutton.setOnClickListener(handler);

			return view;
		}

		OnClickListener handler = new OnClickListener() {
			public void onClick(View view) {
				int id = view.getId();
				if (id == R.id.website_btn) {
					Uri website = Uri.parse("http://wirralgrammarboys.com/");
			        Intent websiteintent = new Intent(Intent.ACTION_VIEW, website);
			        startActivity(websiteintent);
				} else if (id == R.id.facebook_btn) {
		        	Uri facebook = Uri.parse("https://www.facebook.com/WirralGSB");
		        	Intent facebookintent = new Intent(Intent.ACTION_VIEW, facebook);
			        startActivity(facebookintent);
				} else if (id == R.id.twitter_btn) {
		        	Uri twitter = Uri.parse("https://twitter.com/WGSB");
		        	Intent twitterintent = new Intent(Intent.ACTION_VIEW, twitter);
		        	startActivity(twitterintent);
				}
			}
		};
i'm adding this to OP if you don't mind jonny


Try Pi Developers apps

Socials Addict || Floata For Twitter
| | Pi locker

Check Pi Developers Blog

pidevelopers.blogspot.com


My Work For Android



 
Jonny
Old
#17  
Jonny's Avatar
Forum Moderator
Thanks Meter 7822
Posts: 7,832
Join Date: Jul 2011
Location: Cheshire

 
DONATE TO ME
Quote:
Originally Posted by mohamedrashad View Post
i'm adding this to OP if you don't mind jonny
That's fine - if I didn't want people to use/adapt/learn from the code then I wouldn't put it up, use it as you want

Sent from my HTC One using Tapatalk
The Following User Says Thank You to Jonny For This Useful Post: [ Click to Expand ]
 
DeathW1SH
Old
#18  
DeathW1SH's Avatar
Member
Thanks Meter 19
Posts: 32
Join Date: Sep 2011
Thumbs up Keep it up

Great tutorials, keep em coming!

Thread Tools
Display Modes