Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I integrate Facebook LoginButton in PreferenceScreen

I want to give my users ability to connect/disconnect to Facebook through Settings screen which uses simple PreferenceScreen

How can I integrate this custom Button class in PreferenceScreen. https://developers.facebook.com/docs/reference/android/current/LoginButton/

The login button greatly simplifies session management which otherwise needs to be done manually, if I provide simple Preference and use onClickListener

like image 200
Madhur Ahuja Avatar asked Oct 18 '13 13:10

Madhur Ahuja


3 Answers

Use a normal PreferenceScreen with text that indicates to the user to click to login to facebook. Register an OnPreferenceClickListener when it is clicked and in the definition of the listener use this code:

LoginButton l = new LoginButton(this);
l.performClick();

What I am basically trying to do is programmatically create a button that is never shown, and then calling its onClick function which starts the Facebook login/logout procedure. Does this do the trick?

like image 118
rarp Avatar answered Oct 20 '22 02:10

rarp


What I would do is create a custom XML which contains your preference with the login button embedded using a LinearLayout. Using LinearLayout is important because the way it handles onClick events is that the lowest child with an onClickListener gets priority over a parent onClickListener. This means that the only time your login button's onClick logic is called is when the user directly taps on the button.

In code, you can now extend the PreferenceFragment class where you can set up the onClick listeners and anything else you may need. Note that if you're supporting API level 10 or lower, you may need to use in order to create the custom preference:

addPreferencesFromResource(R.xml.custom_preference);

Hopefully this is what you're looking for, and I appreciate any feedback.

like image 25
Rahin Avatar answered Oct 20 '22 04:10

Rahin


Extend Preference class and override onCreateView(ViewGroup parent). Then you can return your LoginButton as the view of your preference. But make sure that you implemented all the necessary methods to make the preference work properly. Here is the detail of how to create a custom preference.

like image 41
paulovap Avatar answered Oct 20 '22 03:10

paulovap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!