Android : Hide facebook login button in Android

on Sunday, September 21, 2014


I followed facebook documentation to create a simple app with a login button.


Now, I wish to hide the login button after the user has already logged in.


I read here on stackoverflow that I should save the access token string and it's expiry date, and check if it's 'expired' (ignoring the case it can be expired at any time by facebook - just theoreticaly) or if it wasn't inserted yet (points that user hasn't logged in), and then show/hide the button respectively.


I tried adjusting the following method:



@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.activity_main, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
if(!hasToShowLogin()){
authButton.setVisibility(View.INVISIBLE);
}


return view;
}


the code in the method 'hasToShowButton' is not relevant because it works, it's just for the demo.


The code above should hide button when the view is first created, and this one should hide the button after the login accomplished, and located in OnActivityResult:



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);

View view = getView();
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setVisibility(View.INVISIBLE);
}


What happens is that the button keep showing no matter what, and is working as usual.


So to conclude, what I'm trying to do is:



  1. Hide login button after successful login

  2. Hide login button in the following times the user enters the app


(In other words: never show login button when user is logged in)


Been breaking my head over this for HOURS. thanks for the helpers!


0 comments:

Post a Comment