Diganta Diganta - 6 months ago 15
Java Question

How to show custom Toast in pecific position in Android

I need to show a customize toast in android. Which will show in a specific position.How can i show a custom toast in a specific position of screen.

Answer

1. create a layout custom_toast.xml with TextView like bellow

    <TextView
    android:id="@+id/textView1"
    android:layout_width="800dp"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_tost_shape"
    android:gravity="center_vertical|center_horizontal"
    android:text="Action Successfuly Completed!"
    android:textColor="#ff0000" 
    android:textSize="40sp"/>

2. Insert this code to show the toast in a method that will take message as parameter. myShowToastMethod(String message){//code}

LayoutInflater layoutInflater = GenApplicationId.this.getLayoutInflater();
             View view = layoutInflater.inflate(R.layout.custom_toast, null);
             TextView tvMessage = (TextView) view.findViewById(R.id.textView1);
             tvMessage.setHeight(100);
             tvMessage.setText(message);
             Toast toast = new Toast(getApplicationContext());
             toast.setDuration(Toast.LENGTH_LONG);
             toast.setGravity(Gravity.TOP, -0, 230);
             toast.setView(view);
             toast.show();
Comments