Friday 7 October 2011

Android Custom Toast in Service Example



Here the below code is use full for show Custom Toast message in Service

Toast UI Layout(custom_toast.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="horizontal"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/header"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >
        <ImageView
            android:id="@+id/toastImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:padding="3dp"/>
        <TextView
            android:id="@+id/toastMes"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center_vertical"
            android:textColor="@android:color/white"
            android:textSize="7pt"
            android:padding="3dp"
            android:layout_marginRight="2dp"/>
    </LinearLayout>
</LinearLayout>

Call this Toast layout in Service using Below code

 LayoutInflater layoutInflater = (LayoutInflater)
 getSystemService(LAYOUT_INFLATER_SERVICE);
 View layout = inflater.inflate(R.layout.custom_toast, null);
 ImageView toastimg= (ImageView)
 layout.findViewById(R.id.toastImg);
 toastimg.setImageResource(R.drawable.anyimage);
TextView toastmes= (TextView)
 layout.findViewById(R.id.toastMes);
 toastmes.setText("Here your ur toast message");
 toast = new Toast(getApplicationContext());
 toast.setGravity(Gravity.BOTTOM, 0, 0);
 toast.setDuration(Toast.LENGTH_SHORT);
 toast.setView(layout);
 toast.show();

No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...