How to display Toast at center of screen

后端 未结 8 2033
北荒
北荒 2021-01-30 09:48

In Android I want to display a toast message at the bottom of the screen, I tried this:

Toast.makeText(test.this,\"bbb\", Toast.LENGTH_LONG).show();
8条回答
  •  臣服心动
    2021-01-30 10:36

    Layout file for custom toast

    
    
    
    

    .java file for custom toast on button's click event

    public class MainActivity extends Activity {
    
    private Button button;
    
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        button = (Button) findViewById(R.id.buttonToast);
    
        button.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
    
                // get your custom_toast.xml ayout
                LayoutInflater inflater = getLayoutInflater();
    
                View layout = inflater.inflate(R.layout.custom_toast,
                  (ViewGroup) findViewById(R.id.custom_toast_layout_id));
    
                // set a dummy image
                ImageView image = (ImageView) layout.findViewById(R.id.image);
                image.setImageResource(R.drawable.ic_launcher);
    
                // set a message
                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText("Button is clicked!");
    
                // Toast...
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
        });
    }
    

    }

提交回复
热议问题