0

I'm new to android development

I developing a new app with imagebutton to facebook profile/page

I tried this code but this code open the facebook in the browser

public class AboutActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);


    ImageButton f = (ImageButton)findViewById(R.id.f_logo);
      f.setOnClickListener(new OnClickListener() 
       {  
            public void onClick(View arg0) 
              { 
                   Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(
                   "http://www.facebook.com/sarmad.waleed.7"));
                    startActivity(browserIntent);

                   }
             }); 
          }
      }

My question is how open the facebook profile/page from imagebutton in FB app (if it installed) and if not then open it in the browser

I also check this

how to link the image button to a facebook page in android

but its the same facebook page opens in the browser

then I tried with "com.facebook.katana" but I don't know how to do it

Community
  • 1
  • 1
sham3on
  • 40
  • 1
  • 8

2 Answers2

3

For Facebook Profile:

//ID initialization
ImageView facebook = findViewById(R.id.facebookID);

//OnClickListener
facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=https://www.facebook.com/techsajib"));
                startActivity(intent);
            } catch(Exception e) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/techsajib")));
            }
        }
    });

For Facebook Page:

 //ID initialization
ImageView facebook = findViewById(R.id.facebookID);

//OnClickListener
facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String facebookId = "fb://page/327031464582675";
            String urlPage = "http://www.facebook.com/MDSaziburRahmanBD";

            try {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId )));
            }catch (Exception e){
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage )));
            }
        }
    });
Papel
  • 541
  • 4
  • 7
2

Facebook android app don't support implicit intent mechanism for this action since 1.9.11 version. Facebook now use the same iPhone scheme mechanism fb:// or facebook://to handle all actions mentioned here.

And here you can see that they support the fb and facebook scheme.

    <activity android:name="com.facebook.katana.IntentUriHandler">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="facebook" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="fb" />
        </intent-filter>
    </activity>

As per your requirement, this method will handle both scenarios. First it will check if the facebook app is installed otherwise it will open facebook profile page in browser.

public Intent getFBIntent(Context context, String facebookId) {

    try {
        // Check if FB app is even installed
        context.getPackageManager().getPackageInfo("com.facebook.katana", 0); 

        String facebookScheme = "fb://profile/" + facebookId;
        return new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme)); 
    }
    catch(Exception e) {

        // Cache and Open a url in browser
        String facebookProfileUri = "https://www.facebook.com/" + facebookId;
        return new Intent(Intent.ACTION_VIEW, Uri.parse(facebookProfileUri));
    }

    return null;
}

In order to open the facebook app with a user profile all you need to do is:

Intent facebookIntent = getFBIntent(this, "2347633432");
startActivity(facebookIntent);

** EDIT **

This is how you can call the above method in your activity. That's it!

public class AboutActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_about);

     ImageButton f = (ImageButton)findViewById(R.id.f_logo);
     f.setOnClickListener(new OnClickListener() 
     {  
        public void onClick(View arg0) 
        { 
           // Get the intent
           Intent intent = getFBIntent(AboutActivity.this, "sarmad.waleed.7");

           // Start the activity
           if (intent != null)
                startActivity(intent);
        }
     }); 
   }

   /**
    * Get the facebook intent for the given facebook
    * profile id. If the facebook app is installed, then
    * it will open the facebook app. Otherwise, it will
    * open the facebook profile page in browser.
    *
    * @return - the facebook intent
    */
   private Intent getFBIntent(Context context, String facebookId) {

      try {
         // Check if FB app is even installed
         context.getPackageManager().getPackageInfo("com.facebook.katana", 0); 

         String facebookScheme = "fb://profile/" + facebookId;
         return new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme)); 
      }
      catch(Exception e) {

         // Cache and Open a url in browser
         String facebookProfileUri = "https://www.facebook.com/" + facebookId;
         return new Intent(Intent.ACTION_VIEW, Uri.parse(facebookProfileUri));
     }

     return null;
   }
}
Deminem
  • 672
  • 8
  • 19
  • I have just written the code to give you an idea, how does it works. In order to work it properly without any compiler error, my assumption was that it would be tweaked accordingly wherever it's necessary. Can you please let me know what kind of errors? – Deminem Nov 08 '14 at 23:45
  • I know thank you I appreciate that, but i suck in java so i dont know where i can put the method – sham3on Nov 08 '14 at 23:53
  • Updated my answer to show how does it works. If it solve your problem, then accept as answer to help the community. – Deminem Nov 09 '14 at 00:17
  • I did the same but I frogot one } thats why hah so dummy – sham3on Nov 09 '14 at 00:55