The following example demonstrates how to create a toast message in a device.
File name: content_main.xml
<?xml version = "1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res-auto"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:paddingBottom = "@dimen/activity_vertical_margin"
android:paddingLeft = "@dimen/activity_horizontal_margin"
android:paddingRight = "@dimen/activity_horizontal_margin"
android:paddingTop = "@dimen/activity_vertical_margin"
app:layout_behavior = "@string/appbar_scrolling_view_behavior"
tools:context = "com.example.itwin.toastdemo.MainActivity"
tools:showIn = "@layout/activity_main">
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Show Message"
android:textSize = "30dp"
android:id = "@+id/btnShow"
android:layout_marginTop = "72dp"
android:onClick = "showToast" // It will call showTost() method
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true" />
</RelativeLayout>
File name: MainActivity.java
public class MainActivity extends AppCompatActivity
{
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btnShow);
}
public void showToast(View v)
{
Toast toast = Toast.makeText(getApplicationContext(), "Welcome to TutorialRide", Toast.LENGTH_SHORT);
toast.show();
}
}