• Latest Code...

    How to create Custom Notification in Android


    Customization notification part needed sometimes, that you modified android notification of user request. Here is the sample code for custom notification:

    Main.Java code :

    public class NotificationToastActivity extends Activity {
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
         
    Button button = (Button) findViewById(R.id.toast_button);
    button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
         
           Toast toast = new Toast(getApplicationContext());

           toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
           toast.setDuration(Toast.LENGTH_LONG);
         
           toast.setView(getLayoutInflater().inflate(R.layout.custom_toast,null));

           toast.show();
    }
    });

        }
    }

    main.xml code:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/toast_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Show Toast"
            android:textSize="24sp" />

    </RelativeLayout>

    and final custom_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#0000FF"
        android:orientation="horizontal"
        android:padding="10dp" >

        <ImageView
            android:id="@+id/custom_image"
            android:layout_width="44dp"
            android:layout_height="44dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:src="@drawable/custom_image" />

        <TextView
            android:id="@+id/customtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/custom_image"
            android:text="Display, Custom Toast Here"
            android:textColor="#fff"
            android:textSize="15sp" />

    </RelativeLayout>

    Here is the some snapshots:




    Happy Coding!!!

    Circle

    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace