Toast class
Toast class is used to show notification for a particular interval of time. After sometime it disappears. It doesn’t block the user interaction.
Constants of Toast class
There are only 2 constants of Toast class which are given below.
Constant | Description |
---|---|
public static final int LENGTH_LONG | displays view for the long duration of time. |
public static final int LENGTH_SHORT | displays view for the short duration of time. |
Methods of Toast class
The widely used methods of Toast class are given below.
Method | Description |
---|---|
public static Toast makeText(Context context, CharSequence text, int duration) | makes the toast containing text and duration. |
public void show() | displays toast. |
public void setMargin (float horizontalMargin, float verticalMargin) | changes the horizontal and vertical margin difference. |
Android Toast Example
- Toast.makeText(getApplicationContext(),”Hello Javatpoint”,Toast.LENGTH_SHORT).show();
Another code:
- Toast toast=Toast.makeText(getApplicationContext(),”Hello Javatpoint”,Toast.LENGTH_SHORT);
- toast.setMargin(50,50);
- toast.show();
Here, getApplicationContext() method returns the instance of Context.
Full code of activity class displaying Toast
Let’s see the code to display the toast.File: MainActivity.java
- package example.javatpoint.com.toast;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //Displaying Toast with Hello Javatpoint message
- Toast.makeText(getApplicationContext(),”Hello Javatpoint”,Toast.LENGTH_SHORT).show();
- }
- }
Output:

Android CheckBox class
The android.widget.CheckBox class provides the facility of creating the CheckBoxes.
Methods of CheckBox class
There are many inherited methods of View, TextView, and Button classes in the CheckBox class. Some of them are as follows:
Method | Description |
---|---|
public boolean isChecked() | Returns true if it is checked otherwise false. |
public void setChecked(boolean status) | Changes the state of the CheckBox. |
Android CheckBox Example
activity_main.xml
Drag the three checkboxes and one button for the layout. Now the activity_main.xml file will look like this:File: activity_main.xml
- <?xml version=”1.0″ encoding=”utf-8″?>
- <android.support.constraint.ConstraintLayout xmlns: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”
- tools:context=”example.javatpoint.com.checkbox.MainActivity”>
- <CheckBox
- android:id=”@+id/checkBox”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_marginLeft=”144dp”
- android:layout_marginTop=”68dp”
- android:text=”Pizza”
- app:layout_constraintStart_toStartOf=”parent”
- app:layout_constraintTop_toTopOf=”parent” />
- <CheckBox
- android:id=”@+id/checkBox2″
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_marginLeft=”144dp”
- android:layout_marginTop=”28dp”
- android:text=”Coffee”
- app:layout_constraintStart_toStartOf=”parent”
- app:layout_constraintTop_toBottomOf=”@+id/checkBox” />
- <CheckBox
- android:id=”@+id/checkBox3″
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_marginLeft=”144dp”
- android:layout_marginTop=”28dp”
- android:text=”Burger”
- app:layout_constraintStart_toStartOf=”parent”
- app:layout_constraintTop_toBottomOf=”@+id/checkBox2″ />
- <Button
- android:id=”@+id/button”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_marginLeft=”144dp”
- android:layout_marginTop=”184dp”
- android:text=”Order”
- app:layout_constraintStart_toStartOf=”parent”
- app:layout_constraintTop_toBottomOf=”@+id/checkBox3″ />
- </android.support.constraint.ConstraintLayout>
Activity class
Let’s write the code to check which toggle button is ON/OFF.File: MainActivity.java
- package example.javatpoint.com.checkbox;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- CheckBox pizza,coffe,burger;
- Button buttonOrder;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- addListenerOnButtonClick();
- }
- public void addListenerOnButtonClick(){
- //Getting instance of CheckBoxes and Button from the activty_main.xml file
- pizza=(CheckBox)findViewById(R.id.checkBox);
- coffe=(CheckBox)findViewById(R.id.checkBox2);
- burger=(CheckBox)findViewById(R.id.checkBox3);
- buttonOrder=(Button)findViewById(R.id.button);
- //Applying the Listener on the Button click
- buttonOrder.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View view) {
- int totalamount=0;
- StringBuilder result=new StringBuilder();
- result.append(“Selected Items:”);
- if(pizza.isChecked()){
- result.append(“\nPizza 100Rs”);
- totalamount+=100;
- }
- if(coffe.isChecked()){
- result.append(“\nCoffe 50Rs”);
- totalamount+=50;
- }
- if(burger.isChecked()){
- result.append(“\nBurger 120Rs”);
- totalamount+=120;
- }
- result.append(“\nTotal: “+totalamount+”Rs”);
- //Displaying the message on the toast
- Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
- }
- });
- }
- }
Output:


Android RadioButton
RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user.
RadioButton is generally used with RadioGroup. RadioGroup contains several radio buttons, marking one radio button as checked makes all other radio buttons as unchecked.
Example of Radio Button
In this example, we are going to implement single radio button separately as well as radio button in RadioGroup.
activity_main.xml
File: activity_main.xml
- <?xml version=”1.0″ encoding=”utf-8″?>
- <LinearLayout
- xmlns:android=”http://schemas.android.com/apk/res/android”
- xmlns:tools=”http://schemas.android.com/tools”
- android:layout_width=”match_parent”
- android:layout_height=”match_parent”
- android:orientation=”vertical”
- tools:context=”example.javatpoint.com.radiobutton.MainActivity”>
- <TextView
- android:id=”@+id/textView1″
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:layout_marginTop=”30dp”
- android:gravity=”center_horizontal”
- android:textSize=”22dp”
- android:text=”Single Radio Buttons” />
- <!– Default RadioButtons –>
- <RadioButton
- android:id=”@+id/radioButton1″
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:layout_gravity=”center_horizontal”
- android:text=”Radio Button 1″
- android:layout_marginTop=”20dp”
- android:textSize=”20dp” />
- <RadioButton
- android:id=”@+id/radioButton2″
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:text=”Radio Button 2″
- android:layout_marginTop=”10dp”
- android:textSize=”20dp” />
- <View
- android:layout_width=”fill_parent”
- android:layout_height=”1dp”
- android:layout_marginTop=”20dp”
- android:background=”#B8B894″ />
- <TextView
- android:id=”@+id/textView2″
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:layout_marginTop=”30dp”
- android:gravity=”center_horizontal”
- android:textSize=”22dp”
- android:text=”Radio button inside RadioGroup” />
- <!– Customized RadioButtons –>
- <RadioGroup
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:id=”@+id/radioGroup”>
- <RadioButton
- android:id=”@+id/radioMale”
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:text=” Male”
- android:layout_marginTop=”10dp”
- android:checked=”false”
- android:textSize=”20dp” />
- <RadioButton
- android:id=”@+id/radioFemale”
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:text=” Female”
- android:layout_marginTop=”20dp”
- android:checked=”false”
- android:textSize=”20dp” />
- </RadioGroup>
- <Button
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:text=”Show Selected”
- android:id=”@+id/button”
- android:onClick=”onclickbuttonMethod”
- android:layout_gravity=”center_horizontal” />
- </LinearLayout>
Activity class
File: MainActivity.java
- package example.javatpoint.com.radiobutton;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- Button button;
- RadioButton genderradioButton;
- RadioGroup radioGroup;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
- }
- public void onclickbuttonMethod(View v){
- int selectedId = radioGroup.getCheckedRadioButtonId();
- genderradioButton = (RadioButton) findViewById(selectedId);
- if(selectedId==-1){
- Toast.makeText(MainActivity.this,”Nothing selected”, Toast.LENGTH_SHORT).show();
- }
- else{
- Toast.makeText(MainActivity.this,genderradioButton.getText(), Toast.LENGTH_SHORT).show();
- }
- }
- }
Output


Android AlertDialog Example

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.
Android AlertDialog is composed of three regions: title, content area and action buttons.
Android AlertDialog is the subclass of Dialog class.
Methods of AlertDialog class
Method | Description |
---|---|
public AlertDialog.Builder setTitle(CharSequence) | This method is used to set the title of AlertDialog. |
public AlertDialog.Builder setMessage(CharSequence) | This method is used to set the message for AlertDialog. |
public AlertDialog.Builder setIcon(int) | This method is used to set the icon over AlertDialog. |
Android AlertDialog Example
Let’s see a simple example of android alert dialog.
activity_main.xml
You can have multiple components, here we are having only a textview.File: activity_main.xml
- <?xml version=”1.0″ encoding=”utf-8″?>
- <android.support.constraint.ConstraintLayout xmlns: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”
- tools:context=”example.javatpoint.com.alertdialog.MainActivity”>
- <Button
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:id=”@+id/button”
- android:text=”Close app”
- app:layout_constraintBottom_toBottomOf=”parent”
- app:layout_constraintLeft_toLeftOf=”parent”
- app:layout_constraintRight_toRightOf=”parent”
- app:layout_constraintTop_toTopOf=”parent” />
- </android.support.constraint.ConstraintLayout>
strings.xml
Optionally, you can store the dialog message and title in the strings.xml file.File: strings.xml
- <resources>
- <string name=”app_name”>AlertDialog</string>
- <string name=”dialog_message”>Welcome to Alert Dialog</string>
- <string name=”dialog_title”>Javatpoint Alert Dialog</string>
- </resources>
Activity class
Let’s write the code to create and show the AlertDialog.File: MainActivity.java
- package example.javatpoint.com.alertdialog;
- import android.content.DialogInterface;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.app.AlertDialog;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- Button closeButton;
- AlertDialog.Builder builder;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- closeButton = (Button) findViewById(R.id.button);
- builder = new AlertDialog.Builder(this);
- closeButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //Uncomment the below code to Set the message and title from the strings.xml file
- builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);
- //Setting message manually and performing action on button click
- builder.setMessage(“Do you want to close this application ?”)
- .setCancelable(false)
- .setPositiveButton(“Yes”, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- finish();
- Toast.makeText(getApplicationContext(),”you choose yes action for alertbox”,
- Toast.LENGTH_SHORT).show();
- }
- })
- .setNegativeButton(“No”, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- // Action for ‘NO’ Button
- dialog.cancel();
- Toast.makeText(getApplicationContext(),”you choose no action for alertbox”,
- Toast.LENGTH_SHORT).show();
- }
- });
- //Creating dialog box
- AlertDialog alert = builder.create();
- //Setting the title manually
- alert.setTitle(“AlertDialogExample”);
- alert.show();
- }
- });
- }
- }
Output:


Android Image Switcher
Android image switcher provides an animation over images to transition from one image to another. In order to use image switcher, we need to implement ImageSwitcher component in .xml file.
The setFactory() method of ImageSwitcher provide implementation of ViewFactory interface. ViewFactory interface implements its unimplemented method and returns an ImageView.
Example of Image Switcher
Let’s implement an image switcher.
Create activity_main.xml and content_main.xml file in layout folder.
Place some images in drawable folder which are to be switch.
activity_main.xml
File: activity_main.xml
- <?xml version=”1.0″ encoding=”utf-8″?>
- <android.support.design.widget.CoordinatorLayout xmlns: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:fitsSystemWindows=”true”
- tools:context=”com.example.test.imageswitcher.MainActivity”>
- <android.support.design.widget.AppBarLayout
- android:layout_width=”match_parent”
- android:layout_height=”wrap_content”
- android:theme=”@style/AppTheme.AppBarOverlay”>
- <android.support.v7.widget.Toolbar
- android:id=”@+id/toolbar”
- android:layout_width=”match_parent”
- android:layout_height=”?attr/actionBarSize”
- android:background=”?attr/colorPrimary”
- app:popupTheme=”@style/AppTheme.PopupOverlay” />
- </android.support.design.widget.AppBarLayout>
- <include layout=”@layout/content_main” />
- </android.support.design.widget.CoordinatorLayout>
content_main.xml
File: content_main.xml
- <?xml version=”1.0″ encoding=”utf-8″?>
- <RelativeLayout xmlns: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.test.imageswitcher.MainActivity”
- tools:showIn=”@layout/activity_main”>
- <TextView
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:text=”Image Switcher Example”
- android:id=”@+id/textView”
- android:layout_alignParentTop=”true”
- android:layout_centerHorizontal=”true” />
- <ImageSwitcher
- android:id=”@+id/imageSwitcher”
- android:layout_width=”match_parent”
- android:layout_height=”250dp”
- android:layout_marginBottom=”28dp”
- android:layout_marginTop=”40dp” />
- <Button
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:text=”Next”
- android:id=”@+id/button”
- android:layout_marginBottom=”47dp”
- android:layout_alignParentBottom=”true”
- android:layout_centerHorizontal=”true” />
- </RelativeLayout>
Activity class
File: MainActivity.java
- package com.example.test.imageswitcher;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageSwitcher;
- import android.widget.ImageView;
- import android.widget.ViewSwitcher;
- import android.app.ActionBar;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- public class MainActivity extends AppCompatActivity {
- ImageSwitcher imageSwitcher;
- Button nextButton;
- int imageSwitcherImages[] =
- {R.drawable.cpp, R.drawable.c_sarp, R.drawable.jsp, R.drawable.mysql, R.drawable.hadoop};
- int switcherImageLength = imageSwitcherImages.length;
- int counter = -1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
- nextButton = (Button) findViewById(R.id.button);
- imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
- @Override
- public View makeView() {
- ImageView switcherImageView = new ImageView(getApplicationContext());
- switcherImageView.setLayoutParams(new ImageSwitcher.LayoutParams(
- ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.FILL_PARENT
- ));
- switcherImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
- switcherImageView.setImageResource(R.drawable.hadoop);
- //switcherImageView.setMaxHeight(100);
- return switcherImageView;
- }
- });
- Animation aniOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
- Animation aniIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
- imageSwitcher.setOutAnimation(aniOut);
- imageSwitcher.setInAnimation(aniIn);
- nextButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- counter++;
- if (counter == switcherImageLength){
- counter = 0;
- imageSwitcher.setImageResource(imageSwitcherImages[counter]);
- }
- else{
- imageSwitcher.setImageResource(imageSwitcherImages[counter]);
- }
- }
- });
- }
- }
Output

