基本 package com.example.hellotoast; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; public class HelloToast extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Toast.makeText(this, "Hello! Toast!", Toast.LENGTH_SHORT).show(); } } ボタンを押したら 基本は同じですが、Context に this とは書けないので、HelloToast.this もしくは、getApplicationContext() とします。 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); setContentView(layout); layout.setOrientation(LinearLayout.VERTICAL); Button btn = new Button(this); btn.setText("Hello Toast"); layout.addView(btn); btn.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(HelloToast.this, "Hello! Toast!", Toast.LENGTH_SHORT).show(); } }); }
Toast.makeText(getApplicationContext(), "Hello! Toast!", Toast.LENGTH_SHORT).show(); カスタマイズ アイコン付き Toastです。setView で指定しています。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:background="#999" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/imageView1" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /> </LinearLayout> LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.toast_layout, null); TextView text = (TextView)layout.findViewById(R.id.textView1); text.setText("Hello! Toast!"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); カスタムその2 角を丸くした背景にしてみました。 drawable/screen_backgound.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp"> <solid android:color="#333"/> <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp"/> <stroke android:width="2px" android:color="#CCCCCC" /> </shape> layout/rounded_corners.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#0fff" android:padding="10dp" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:background="@drawable/screen_background"> <TextView android:text="custom toast!" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" /> </LinearLayout> </LinearLayout> View view = (View)getLayoutInflater().inflate(R.layout.rounded_corners, null); TextView text = (TextView)view.findViewById(R.id.textView1); text.setText("Hello! Toast!"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(view); toast.show(); |