0
@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btnOKButt:
            displayToast("My text");
            startActivity(new Intent(Activity1.this, Activity2.class));
            finish();
            break;
    }
}
private void displayToast(String s) {

//the default toast view group is a relativelayout

    Toast toast = Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG);
    LinearLayout toastLayout = (LinearLayout) toast.getView();
    TextView toastTV = (TextView) toastLayout.getChildAt(0);
    toast.getView().setBackgroundColor(Color.BLACK);
    toast.setGravity(Gravity.FILL, 0, -250);
    toastTV.setTextSize(30);

    toast**strong text**.show();
}

}

I would like to have the toast to fill the whole background and still have the text in the middle. But if I use 'Gravity.FILL' I always have the text at the top. What should one do?

Code-G
  • 41
  • 5

1 Answers1

0

You need to create a custom toast view with TextView's gravity = center. It would be something like that:

Layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
>
<TextView android:id="@+id/text_toast"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:text="@string/hello_toast" />
</LinearLayout>

Show toast

        LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.test_toast,
            (ViewGroup) findViewById(R.id.custom_toast_container));

    TextView text = (TextView) layout.findViewById(R.id.text_toast);
    text.setTextSize(40);
    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.FILL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();
Dmitrii Nechepurenko
  • 1,414
  • 1
  • 11
  • 13