Showing posts with label customized toast without creating xml layout. Show all posts
Showing posts with label customized toast without creating xml layout. Show all posts

Sunday, April 20, 2014

Custom Toast

Custom Toast :

LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custom_toast,
                                               (ViewGroup) findViewById(R.id.
LL_Parent));
   
                TextView text = (TextView) layout.findViewById(R.id.txtShow);


                text.setText("This is My Custom Toast.");

                Toast toast = new Toast(getApplicationContext());


                // Display in center

                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);

                toast.show();


__________________________________________________________



custom_toast.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LL_Parent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DAAA"
    android:orientation="horizontal"
    android:padding="8dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF" />


</LinearLayout>

__________________________________________________________


customized toast without creating xml layout :


                    // create a LinearLayout and Views

                    LinearLayout layout1 = new LinearLayout(this);
                    layout1.setBackgroundResource(android.R.color.darker_gray);

                    // create a TextView

                    TextView tv = new TextView(this);
                    tv.setTextColor(Color.RED);
                    tv.setTextSize(15);
                    tv.setGravity(Gravity.CENTER_VERTICAL);
                    tv.setText("This is My Custom Toast.");

                    // create a ImageView

                    ImageView img = new ImageView(this);
                    img.setImageResource(R.drawable.ic_launcher);

                    // TextView and ImageView are add in LinearLayout 

                    layout1.addView(img);
                    layout1.addView(tv);

                    // Toast 

                    Toast toast1 = new Toast(this);
                    toast1.setView(layout1);
                    toast1.setGravity(Gravity.BOTTOM, 0, 50);
                    toast1.show();