Wednesday, September 14, 2011

Android Custom Component - Notify Number on icon


Custom Component using XML.

Basic approach to create your own View components:

·         Extending the View class for a completely custom component.
·         Parameterized constructor that takes the view inflation parameters (parameters defined in the XML). 
Create a custom component in xml (notifynumbercomp.xml) file with existing components available in android.

notifynumbercomp.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <RelativeLayout android:layout_width="32dip"
                        android:layout_height="wrap_content">
                        <ImageView android:src="@drawable/archive" android:id="@+id/NotifyImageView"
                                    android:layout_width="24dip" android:layout_height="24dip"
                                    android:paddingTop="3dip"></ImageView>
                        <TextView android:id="@+id/NotifyNumberTextView"
                                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                                    android:layout_alignParentRight="true" android:layout_alignParentTop="true"></TextView>
            </RelativeLayout>
</LinearLayout>


Create a class file, which extends View

NotifyNumberComponent.java

package com.cc.notify;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class NotifyNumberComponent extends LinearLayout {

            public NotifyNumberComponent(Context context, AttributeSet attrs) {
                        super(context, attrs);

                        LayoutInflater layoutInflater = (LayoutInflater) context
                                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View view = layoutInflater.inflate(R.layout.notifynumbercomp, this);
            }
}


Access the Custom component created in the main.xml file by loading view from Activity class.

NotifyNumber .java

package com.cc.notify;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class NotifyNumber extends Activity {
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main);

                        int count = 3;

                        TextView notifyNumber = (TextView) findViewById(R.id.NotifyNumberTextView);
                        notifyNumber.setPadding(3, 1, 2, 0);
                        notifyNumber.setBackgroundResource(R.drawable.bg_num);
                        notifyNumber.getLayoutParams().height = 21;
                        notifyNumber.getLayoutParams().width = 19;
                        notifyNumber.setText("3");
                        notifyNumber.setTextColor(Color.WHITE);
                        notifyNumber.setVisibility(View.VISIBLE);

                        if (count == 0) {
                                    notifyNumber.setVisibility(View.GONE);
                        }
            }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <com.cc.notify.NotifyNumberComponent
                        android:layout_height="wrap_content" android:layout_width="fill_parent" />
</LinearLayout>

Output looks:




No comments :

Post a Comment