Android ChatHead Tutorial - (1) Setup ChatHead

Source code: Github (branch "step-1")



Step 1: Setup an Android Project


Android ChatHead requires "SYSTEM_ALERT_WINDOW" permission which is available since API level 1, so minimum API 7 is fine.

Just next, next next... and create a blank activity :)



Step 2: Add ChatHead Service


If you want to know more details about ChatHead service. Read this article by Pierre-Yves Ricau.
Create a ChatHead Class that extends Service.
/java/com/gbinghan/androidchathead/ChatHeadService.java


Create ChatHead layout file.
/res/layout/service_chat_head.xml


Add ChatHeadService to AndroidManifest.xml

<service android:name="com.gbinghan.androidchathead.ChatHeadService" ></service>

Add SYSTEM_ALERT_WINDOW to AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Your AndroidManifest.xml should look like this.


Step 3: Add a button to launch the service :)


Add OnClickListener in Activity file java/com/gbinghan/androidchathead/MainActivity.java.
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ChatHeadService.class);
                intent.putExtra("title", "Hello");
                intent.putExtra("text", "ChatHead");
                startService(intent);
            }
        });

Add the button in layout file /res/layout/activity_main.xml.
    <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:text="Button"/>



Step 4: Run!


You should see this on your emulator/device.







Note:
If you use different package name, remember to replace all instance of "gbinghan".


References:
http://www.piwai.info/chatheads-basics/
https://github.com/venator85/ChatHeads/blob/master/src/com/example/chatheads/MainActivity.java

Comments

Popular posts from this blog

[Azure Websites PHP] Cross Domain request results in blank response page after Preflight HTTP OPTIONS

[Magento] Create Contact Form with Dynamic Recipient