Nital Nital - 1 month ago 5
Android Question

How to set width of toast in Android?

Is there any easy way to

set width
of the
Toast
without customizing it?

Thanks,
Nital

Update:

there is no such method...
i finally customized it in this way...

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

Answer

I used the following way to set width and height of toast.

How to set width and height to toast

CancelProgressDialog.java

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CancelProgressDialog extends Activity {

    ProgressDialog myProgressDialog = null;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        /* Create a very simple button */

        Button b = new Button(this);

        this.setContentView(b);

        b.setText("Show ProgressBar...");

//      b.setOnClickListener(myProgressBarShower);
        Context context = getApplicationContext();

        LayoutInflater inflater = getLayoutInflater();

        View toastRoot = inflater.inflate(R.layout.customtoast, null);
        TextView tv = (TextView) toastRoot.findViewById(R.id.toasttext);
        tv.setWidth(200);
        tv.setHeight(100);

        Toast toast = new Toast(context);

        toast.setView(toastRoot);
        toast.show();
//      toast.setText("I am toast");
        toast.setDuration(Toast.LENGTH_LONG);


    }
}

customtoast.xml

<?xml version="1.0" encoding="utf-8"?>


<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="I am toast"
    android:textColor="#000000"
    android:id="@+id/toasttext"
    android:background="#ffffff" />

Thanks Deepak

Comments