Step 1. Create layout activity_layout.xml
<RelativeLayoutStep 2. Create class MainActivity.java
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity"
android:background="#76b3ca"
>
<Button
android:id="@+id/btn_short"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Short Duration Toast"
/>
<Button
android:id="@+id/btn_long"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Long Duration Toast"
android:layout_toRightOf="@id/btn_short"
/>
</RelativeLayout>
public class MainActivity extends Activity {Result:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
Button btn_short = (Button) findViewById(R.id.btn_short);
Button btn_long = (Button) findViewById(R.id.btn_long);
btn_short.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(
getApplicationContext(), // Context
"Short Duration Toast Message", // Message
Toast.LENGTH_SHORT // Short Duration
);
toast.show();
}
});
btn_long.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(
getApplicationContext(), // Context
"Long Duration Toast Message", // Message
Toast.LENGTH_LONG // Long Duration
);
toast.show();
}
});
}
}