Practical No 15
Practical No 15
15
The Toast.makeText() method is a factory method which creates a Toast object. The method
takes 3 parameters. First the methods needs a Context object which is obtained by calling
getApplicationContext(). Note: The getApplicationContext() method is a method that exists
inside activities, so the above code has to be located in an Activity subclass towork.
The second parameter is the text to be displayed in the Toast. The third parameter is the time
duration the Toast is to be displayed.
You can change the positioning on the screen of a Toast message using the setGravity()
method. Here is a Toast setGravity() example:
toast.setGravity(Gravity.CENTER, 0, 0);
2.
Constants:
int LENGTH_LONG
Show the view or text notification for a long period of time.
int LENGTH_SHORT
Show the view or text notification for a short period of time.
X. Exercise
(Use blank space provide for answers or attached more pages if needed) 1. Write
a program to display following toast message.
2. Write a program to display three checkboxes and one button named “Order “as shown
below. Once you click on button it should toast different selected checkboxes along with
items individual and total price.
Answers:
1.
//MainActivity.java
package com.jamiapolytechnic.experiment151;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showToast(View view)
{
String str = getString(R.string.message);
Toast.makeText(this,str,Toast.LENGTH_LONG).show();
}
}
//Note: Define a string in string.xml file with id as message and value as :
//”Message for you: \n You have got mail”
//=====================================================================
//activity_main.xml
android:layout_height="wrap_content"
android:text="Show Toast"
android:id="@+id/button"
android:onClick="showToast"/>
</LinearLayout>
//=====================================================================
2.
//MainActivity.java
package com.jamiapolytechnic.experiment15_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
//=====================================================================
//activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<CheckBox
android:id="@+id/piz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_gravity="center"
android:text="Pizza" />
<CheckBox
android:id="@+id/cof"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_gravity="center"
android:text="Coffee" />
<CheckBox
android:id="@+id/bur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_gravity="center"
android:text="Burger" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/ord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:onClick="showDetails"
android:text="Order" />
</LinearLayout>
</LinearLayout>
Dated signature of
Marks Obtained
Teacher
Process Product Total
Related(10) Related(15) (25)