임의의 위치에 원하는 레이아웃을 사용하여 Toast 팝업 표시 하기.


[소스]

    public static void showToast(Context context, String message, int showTime_msecs){

        Toast toast=new Toast(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View toastView = inflater.inflate(R.layout.toast_layout, null);

        ((TextView)toastView.findViewById(R.id.toast_root).findViewById(R.id.toast_message)).setText(message);

        toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);

        toast.setView(toastView);

        toast.setDuration(showTime_msecs);

        toast.show();

    }


[layout]
toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
    android:background="#ffff8000"
    android:padding="4dp"
android:id="@+id/toast_root"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:padding="10dp"
android:textColor="#000000"
android:background="@drawable/gradation_green"
android:gravity="left|center_vertical"
android:id="@+id/toast_message"
/>
</LinearLayout>



'programmer > android' 카테고리의 다른 글

onTouch와 onTouchEvent  (0) 2013.02.18
PendingIntent  (1) 2012.12.14
EditText bottom space  (0) 2012.12.13
android.database.StaleDataException  (0) 2012.12.12
Battery check  (0) 2012.10.19

+ Recent posts