custom toast with pictures
in MainActivity
public class MainActivity extends Activity {
private Button button1;
protected void onCreate(Bundle savedInstanceState){
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {//Set the listener
public void onClick(View v){
showToast();
button1.setText("Downloading...");
}
});
}
public void showToast(){
//Get a Toast object, ready for the following operations
Toast toast = Toast.makeText(this, "Toast with pictures", Toast.LENGTH_SHORT);
ImageView imageView = new ImageView(this);
//Use the image provided by the system
imageView.setImageResource(R.drawable.ic_launcher);
//Get the layout object of the toast
LinearLayout toast_view = (LinearLayout)toast.getView();
//Add image resources to toast, the second parameter, 0 means the image is on top
toast_view.addView(imageView,1);
toast.show();
}
}
Color position custom toast
public class MyToast extends Activity {
public MyToast(Context context,String prompt){
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
//custom layout
View view = View.inflate(context, R.layout.activity_my_toast, null);
//load the view
TextView view2 = (TextView) view.findViewById(R.id.tv_msg);
view2.setText(prompt);//Display
toast.setView(view);
toast.setGravity(Gravity.CENTER, 0, 0);//Center
toast.show();
}
}
In xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/tv_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:textColor="#FF3E96"
android:background="#EEE685"
android:padding="15dp"
/>
</RelativeLayout>