Tuesday, January 29, 2013

How-To: Send Facebook App Request

Hi developer,
the problem in which I came across today was: how to link my Android app using Facebook SDK for Android?
The premise is that your Android app must appear in the App Center of Facebook and must be integrated with Facebook by the appropriate SDK.

The crucial point that is not mentioned by facebook tutorial is that the method to be called is "apprequests".

This is the code:
Bundle parameters = new Bundle();
params.putString("method", "apprequests");
WebDialog.RequestsDialogBuilder req = new WebDialog.RequestsDialogBuilder(this, mSession, parameters);
req.setMessage("Your message that compare in the request");
req.setTitle("The title of the web dialog");
req.build().show();

Also not specifying your friends (like the example code above) user will see a Multi Friend Selector and will be able to select a maximum of 50 recipients.

Tuesday, January 15, 2013

Custom Font for View

Hi developers,
today I want to show you how to set a custom font to a View either by code or using an XML file.
The first way is very simple: just copy the font file into "assets/fonts/"  folder and  load it using Typeface classcalling createFromAssets method.

















TextView txtView1 = (TextView) findViewById(R.id.textView1);
Typeface externalFont = Typeface.createFromAsset(getAssets(), "fonts/CONSOLA.TTF");
txtView1.setTypeface(externalFont);

Very simple! The hardest way is set custom Font using an XML file, especially by using styles!!
Yes it is possible! unlike what you read in various forums...
Just extremize the concept of custom view.
So the first thing to do is create a CustomView (in this case is a TextView but the concept is valid for all View), within a method to set the "Custom Font" that we call in the constructor of View:

 public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomFontTxtView);
        String customFont = a.getString(R.styleable.CustomFontTxtView_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
        tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
        } catch (Exception e) {
            Log.e(TAG, "Error to get typeface: "+e.getMessage());
            return false;
        }
        setTypeface(tf);  
        return true;
    }

to be able to change from xml we need to create our style resources in this way:



    

And now we can set the font from layout XML file!!!:



    

    

    
    
    

Here you can download the complete example.



Friday, January 4, 2013

Sending SMS messages

Hi all,
in Android there are two ways to send SMS messages.
The first is to delegate the send action to SMS application by intent:
 
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "Content of the SMS goes here..."); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);

The second way is to use the SmsManager (added in API level 4):

//---sends an SMS message to another device---
    private void sendSMS(String phoneNumber, String message)
    {        
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, SMS.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    }

To use the second method you need to add the permission to the manifest, because you send the message directly