Thursday, July 12, 2012

An Android Dialog before the Facebook Dialog

While I was writing an award screen for PollKarma I decided that users would like award screens.  The behavior occurs when a user answers x questions they got a pop-up with a cool image/message and an option to share on Facebook.  To get this done I made a custom dialog box that serves as a pre-cursor to the facebook dialog to provide some more context for the user and a nicer graphic (Facebook's is limited to the format of a wall post.

The simplest way to get this done is with a new xml file for the custom dialog box and to dump all the code in  the main execution file.

The Award.XML file is very simple.  Any good share dialog will need a big image along with a share and close button:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
        <Button android:id="@+id/share" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Share"></Button>
   
        <Button android:id="@+id/close" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Close"></Button>
    <ImageView
    android:id="@+id/YourImage"
    android:src="@drawable/yourimage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
android:scaleType="fitXY"
></ImageView>
</LinearLayout>


Note the android:scaleType="fitXY" which auto-sizes your image to fill the layout and be big and pretty.

So, assuming you have a file named award.xml you can dump this code in the method triggered on any award event to provide a nice wrapper for a facebook wall share dialog:


final Dialog dialog = new Dialog(YourActivity.this);
dialog.setContentView(R.layout.award);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
Button closeBtn = (Button) dialog.findViewById(R.id.close);
closeBtn.setOnClickListener(new OnClickListener() {
@Override
    public void onClick(View v) {
        dialog.dismiss();
    }
});
Button shareBtn = (Button) dialog.findViewById(R.id.share);
shareBtn.setOnClickListener(new OnClickListener() {
@Override
      public void onClick(View v) {
      Bundle params = new Bundle();
      params.putString("caption", "yourcaption");
      params.putString("description", "yourdescription");
      params.putString("picture", "http://yoursite.com/images/yourimage.png");
      params.putString("name", "yourname");
      Utility.mFacebook.dialog(YourActivity.this, "feed", params, new DialogListener() {
          @Override
          public void onComplete(Bundle values) {
           dialog.dismiss();
          }
          @Override
           public void onFacebookError(FacebookError error) {}
          @Override
          public void onError(DialogError e) {}
          @Override
          public void onCancel() {}
      });
    }
});
dialog.show();
}

No comments:

Post a Comment