Powered By Blogger

Monday 27 February 2012

custom toast in android


import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CustomToastEx extends Activity {
    /** Called when the activity is first created. */
 
ImageView img;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        LayoutInflater li=getLayoutInflater();
        ViewGroup vg=(ViewGroup)findViewById(R.id.toast);
        View v=li.inflate(R.layout.main, vg);
        img=(ImageView)v.findViewById(R.id.imageView1);
        img.setImageResource(R.drawable.icon);
        TextView tx=(TextView)v.findViewById(R.id.text);
        tx.setText("hello! this is a custom toast");
       
        Toast t=new Toast(getApplicationContext());
        t.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        t.setDuration(Toast.LENGTH_LONG);
        t.setView(v);
        t.show();
    }
}
main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="#DAAA"
    >
    <ImageView android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="10dp"></ImageView>
    <TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF"></TextView>

</LinearLayout>


No comments:

Post a Comment