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.

ConstantDescription
public static final int LENGTH_LONGdisplays view for the long duration of time.
public static final int LENGTH_SHORTdisplays view for the short duration of time.

Methods of Toast class

The widely used methods of Toast class are given below.

MethodDescription
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

  1. Toast.makeText(getApplicationContext(),”Hello Javatpoint”,Toast.LENGTH_SHORT).show();  

Another code:

  1. Toast toast=Toast.makeText(getApplicationContext(),”Hello Javatpoint”,Toast.LENGTH_SHORT);  
  2. toast.setMargin(50,50);  
  3. 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

  1. package example.javatpoint.com.toast;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.widget.Toast;  
  6.   
  7. public class MainActivity extends AppCompatActivity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.   
  14.         //Displaying Toast with Hello Javatpoint message  
  15.         Toast.makeText(getApplicationContext(),”Hello Javatpoint”,Toast.LENGTH_SHORT).show();  
  16.     }  
  17. }  

Output:

android toast example output 1

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:

MethodDescription
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

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.     xmlns:app=”http://schemas.android.com/apk/res-auto”  
  4.     xmlns:tools=”http://schemas.android.com/tools”  
  5.     android:layout_width=”match_parent”  
  6.     android:layout_height=”match_parent”  
  7.     tools:context=”example.javatpoint.com.checkbox.MainActivity”>  
  8.   
  9.   
  10.     <CheckBox  
  11.         android:id=”@+id/checkBox”  
  12.         android:layout_width=”wrap_content”  
  13.         android:layout_height=”wrap_content”  
  14.         android:layout_marginLeft=”144dp”  
  15.         android:layout_marginTop=”68dp”  
  16.         android:text=”Pizza”  
  17.         app:layout_constraintStart_toStartOf=”parent”  
  18.         app:layout_constraintTop_toTopOf=”parent” />  
  19.   
  20.     <CheckBox  
  21.         android:id=”@+id/checkBox2″  
  22.         android:layout_width=”wrap_content”  
  23.         android:layout_height=”wrap_content”  
  24.         android:layout_marginLeft=”144dp”  
  25.         android:layout_marginTop=”28dp”  
  26.         android:text=”Coffee”  
  27.         app:layout_constraintStart_toStartOf=”parent”  
  28.         app:layout_constraintTop_toBottomOf=”@+id/checkBox” />  
  29.   
  30.     <CheckBox  
  31.         android:id=”@+id/checkBox3″  
  32.         android:layout_width=”wrap_content”  
  33.         android:layout_height=”wrap_content”  
  34.         android:layout_marginLeft=”144dp”  
  35.         android:layout_marginTop=”28dp”  
  36.         android:text=”Burger”  
  37.         app:layout_constraintStart_toStartOf=”parent”  
  38.         app:layout_constraintTop_toBottomOf=”@+id/checkBox2″ />  
  39.   
  40.     <Button  
  41.         android:id=”@+id/button”  
  42.         android:layout_width=”wrap_content”  
  43.         android:layout_height=”wrap_content”  
  44.         android:layout_marginLeft=”144dp”  
  45.         android:layout_marginTop=”184dp”  
  46.         android:text=”Order”  
  47.         app:layout_constraintStart_toStartOf=”parent”  
  48.         app:layout_constraintTop_toBottomOf=”@+id/checkBox3″ />  
  49.   
  50. </android.support.constraint.ConstraintLayout>  

Activity class

Let’s write the code to check which toggle button is ON/OFF.File: MainActivity.java

  1. package example.javatpoint.com.checkbox;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.CheckBox;  
  8. import android.widget.Toast;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.     CheckBox pizza,coffe,burger;  
  12.     Button buttonOrder;  
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         addListenerOnButtonClick();  
  18.     }  
  19.     public void addListenerOnButtonClick(){  
  20.         //Getting instance of CheckBoxes and Button from the activty_main.xml file  
  21.         pizza=(CheckBox)findViewById(R.id.checkBox);  
  22.         coffe=(CheckBox)findViewById(R.id.checkBox2);  
  23.         burger=(CheckBox)findViewById(R.id.checkBox3);  
  24.         buttonOrder=(Button)findViewById(R.id.button);  
  25.   
  26.         //Applying the Listener on the Button click  
  27.         buttonOrder.setOnClickListener(new View.OnClickListener(){  
  28.   
  29.             @Override  
  30.             public void onClick(View view) {  
  31.                 int totalamount=0;  
  32.                 StringBuilder result=new StringBuilder();  
  33.                 result.append(“Selected Items:”);  
  34.                 if(pizza.isChecked()){  
  35.                     result.append(“\nPizza 100Rs”);  
  36.                     totalamount+=100;  
  37.                 }  
  38.                 if(coffe.isChecked()){  
  39.                     result.append(“\nCoffe 50Rs”);  
  40.                     totalamount+=50;  
  41.                 }  
  42.                 if(burger.isChecked()){  
  43.                     result.append(“\nBurger 120Rs”);  
  44.                     totalamount+=120;  
  45.                 }  
  46.                 result.append(“\nTotal: “+totalamount+”Rs”);  
  47.                 //Displaying the message on the toast  
  48.                 Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();  
  49.             }  
  50.   
  51.         });  
  52.     }  
  53. }  

Output:

android checkbox example output 1
android checkbox example output 2

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

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <LinearLayout  
  3.     xmlns:android=”http://schemas.android.com/apk/res/android”  
  4.     xmlns:tools=”http://schemas.android.com/tools”  
  5.     android:layout_width=”match_parent”  
  6.     android:layout_height=”match_parent”  
  7.     android:orientation=”vertical”  
  8.     tools:context=”example.javatpoint.com.radiobutton.MainActivity”>  
  9.   
  10.     <TextView  
  11.         android:id=”@+id/textView1″  
  12.         android:layout_width=”fill_parent”  
  13.         android:layout_height=”wrap_content”  
  14.         android:layout_marginTop=”30dp”  
  15.         android:gravity=”center_horizontal”  
  16.         android:textSize=”22dp”  
  17.         android:text=”Single Radio Buttons” />  
  18.   
  19.   
  20.   
  21.     <!–   Default RadioButtons  –>  
  22.   
  23.     <RadioButton  
  24.         android:id=”@+id/radioButton1″  
  25.         android:layout_width=”fill_parent”  
  26.         android:layout_height=”wrap_content”  
  27.         android:layout_gravity=”center_horizontal”  
  28.         android:text=”Radio Button 1″  
  29.         android:layout_marginTop=”20dp”  
  30.   
  31.         android:textSize=”20dp” />  
  32.     <RadioButton  
  33.         android:id=”@+id/radioButton2″  
  34.         android:layout_width=”fill_parent”  
  35.         android:layout_height=”wrap_content”  
  36.         android:text=”Radio Button 2″  
  37.         android:layout_marginTop=”10dp”  
  38.   
  39.         android:textSize=”20dp” />  
  40.   
  41.   
  42.     <View  
  43.         android:layout_width=”fill_parent”  
  44.         android:layout_height=”1dp”  
  45.         android:layout_marginTop=”20dp”  
  46.         android:background=”#B8B894″ />  
  47.   
  48.     <TextView  
  49.         android:id=”@+id/textView2″  
  50.         android:layout_width=”fill_parent”  
  51.         android:layout_height=”wrap_content”  
  52.         android:layout_marginTop=”30dp”  
  53.         android:gravity=”center_horizontal”  
  54.         android:textSize=”22dp”  
  55.         android:text=”Radio button inside RadioGroup” />  
  56.   
  57.   
  58.     <!–   Customized RadioButtons  –>  
  59.   
  60.   
  61.     <RadioGroup  
  62.         android:layout_width=”wrap_content”  
  63.         android:layout_height=”wrap_content”  
  64.         android:id=”@+id/radioGroup”>  
  65.   
  66.         <RadioButton  
  67.             android:id=”@+id/radioMale”  
  68.             android:layout_width=”fill_parent”  
  69.             android:layout_height=”wrap_content”  
  70.             android:text=”  Male”  
  71.             android:layout_marginTop=”10dp”  
  72.             android:checked=”false”  
  73.             android:textSize=”20dp” />  
  74.   
  75.         <RadioButton  
  76.             android:id=”@+id/radioFemale”  
  77.             android:layout_width=”fill_parent”  
  78.             android:layout_height=”wrap_content”  
  79.             android:text=”   Female”  
  80.             android:layout_marginTop=”20dp”  
  81.             android:checked=”false”  
  82.   
  83.             android:textSize=”20dp” />  
  84.     </RadioGroup>  
  85.   
  86.     <Button  
  87.         android:layout_width=”wrap_content”  
  88.         android:layout_height=”wrap_content”  
  89.         android:text=”Show Selected”  
  90.         android:id=”@+id/button”  
  91.         android:onClick=”onclickbuttonMethod”  
  92.         android:layout_gravity=”center_horizontal” />  
  93.   
  94.   
  95. </LinearLayout>  

Activity class

File: MainActivity.java

  1. package example.javatpoint.com.radiobutton;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.RadioButton;  
  8. import android.widget.RadioGroup;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.     Button button;  
  13.     RadioButton genderradioButton;  
  14.     RadioGroup radioGroup;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         radioGroup=(RadioGroup)findViewById(R.id.radioGroup);  
  20.     }  
  21.     public void onclickbuttonMethod(View v){  
  22.         int selectedId = radioGroup.getCheckedRadioButtonId();  
  23.         genderradioButton = (RadioButton) findViewById(selectedId);  
  24.         if(selectedId==-1){  
  25.             Toast.makeText(MainActivity.this,”Nothing selected”, Toast.LENGTH_SHORT).show();  
  26.         }  
  27.         else{  
  28.             Toast.makeText(MainActivity.this,genderradioButton.getText(), Toast.LENGTH_SHORT).show();  
  29.         }  
  30.   
  31.     }  
  32. }  

Output

android Radio Button 1
android Radio Button 2

Android AlertDialog Example

android alert dialog

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

MethodDescription
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

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.     xmlns:app=”http://schemas.android.com/apk/res-auto”  
  4.     xmlns:tools=”http://schemas.android.com/tools”  
  5.     android:layout_width=”match_parent”  
  6.     android:layout_height=”match_parent”  
  7.     tools:context=”example.javatpoint.com.alertdialog.MainActivity”>  
  8.   
  9.     <Button  
  10.         android:layout_width=”wrap_content”  
  11.         android:layout_height=”wrap_content”  
  12.         android:id=”@+id/button”  
  13.         android:text=”Close app”  
  14.         app:layout_constraintBottom_toBottomOf=”parent”  
  15.         app:layout_constraintLeft_toLeftOf=”parent”  
  16.         app:layout_constraintRight_toRightOf=”parent”  
  17.         app:layout_constraintTop_toTopOf=”parent” />  
  18.   
  19. </android.support.constraint.ConstraintLayout>  

strings.xml

Optionally, you can store the dialog message and title in the strings.xml file.File: strings.xml

  1. <resources>  
  2.     <string name=”app_name”>AlertDialog</string>  
  3.     <string name=”dialog_message”>Welcome to Alert Dialog</string>  
  4.     <string name=”dialog_title”>Javatpoint Alert Dialog</string>  
  5. </resources>  

Activity class

Let’s write the code to create and show the AlertDialog.File: MainActivity.java

  1. package example.javatpoint.com.alertdialog;  
  2.   
  3. import android.content.DialogInterface;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.app.AlertDialog;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.     Button closeButton;  
  13.     AlertDialog.Builder builder;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         closeButton = (Button) findViewById(R.id.button);  
  20.         builder = new AlertDialog.Builder(this);  
  21.         closeButton.setOnClickListener(new View.OnClickListener() {  
  22.             @Override  
  23.             public void onClick(View v) {  
  24.   
  25.                 //Uncomment the below code to Set the message and title from the strings.xml file  
  26.                 builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);  
  27.   
  28.                 //Setting message manually and performing action on button click  
  29.                 builder.setMessage(“Do you want to close this application ?”)  
  30.                         .setCancelable(false)  
  31.                         .setPositiveButton(“Yes”, new DialogInterface.OnClickListener() {  
  32.                             public void onClick(DialogInterface dialog, int id) {  
  33.                                 finish();  
  34.                                 Toast.makeText(getApplicationContext(),”you choose yes action for alertbox”,  
  35.                                 Toast.LENGTH_SHORT).show();  
  36.                             }  
  37.                         })  
  38.                         .setNegativeButton(“No”, new DialogInterface.OnClickListener() {  
  39.                             public void onClick(DialogInterface dialog, int id) {  
  40.                                 //  Action for ‘NO’ Button  
  41.                                 dialog.cancel();  
  42.                                 Toast.makeText(getApplicationContext(),”you choose no action for alertbox”,  
  43.                                 Toast.LENGTH_SHORT).show();  
  44.                             }  
  45.                         });  
  46.                 //Creating dialog box  
  47.                 AlertDialog alert = builder.create();  
  48.                 //Setting the title manually  
  49.                 alert.setTitle(“AlertDialogExample”);  
  50.                 alert.show();  
  51.             }  
  52.         });  
  53.     }  
  54. }  

Output:

android alert dialog example output 1
android alert dialog example output 2

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

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <android.support.design.widget.CoordinatorLayout xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.     xmlns:app=”http://schemas.android.com/apk/res-auto”  
  4.     xmlns:tools=”http://schemas.android.com/tools”  
  5.     android:layout_width=”match_parent”  
  6.     android:layout_height=”match_parent”  
  7.     android:fitsSystemWindows=”true”  
  8.     tools:context=”com.example.test.imageswitcher.MainActivity”>  
  9.   
  10.     <android.support.design.widget.AppBarLayout  
  11.         android:layout_width=”match_parent”  
  12.         android:layout_height=”wrap_content”  
  13.         android:theme=”@style/AppTheme.AppBarOverlay”>  
  14.   
  15.         <android.support.v7.widget.Toolbar  
  16.             android:id=”@+id/toolbar”  
  17.             android:layout_width=”match_parent”  
  18.             android:layout_height=”?attr/actionBarSize”  
  19.             android:background=”?attr/colorPrimary”  
  20.             app:popupTheme=”@style/AppTheme.PopupOverlay” />  
  21.   
  22.     </android.support.design.widget.AppBarLayout>  
  23.     <include layout=”@layout/content_main” />  
  24.   
  25. </android.support.design.widget.CoordinatorLayout>  

content_main.xml

File: content_main.xml

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.     xmlns:app=”http://schemas.android.com/apk/res-auto”  
  4.     xmlns:tools=”http://schemas.android.com/tools”  
  5.     android:layout_width=”match_parent”  
  6.     android:layout_height=”match_parent”  
  7.     android:paddingBottom=”@dimen/activity_vertical_margin”  
  8.     android:paddingLeft=”@dimen/activity_horizontal_margin”  
  9.     android:paddingRight=”@dimen/activity_horizontal_margin”  
  10.     android:paddingTop=”@dimen/activity_vertical_margin”  
  11.     app:layout_behavior=”@string/appbar_scrolling_view_behavior”  
  12.     tools:context=”com.example.test.imageswitcher.MainActivity”  
  13.     tools:showIn=”@layout/activity_main”>  
  14.   
  15.     <TextView  
  16.         android:layout_width=”wrap_content”  
  17.         android:layout_height=”wrap_content”  
  18.         android:text=”Image Switcher Example”  
  19.   
  20.         android:id=”@+id/textView”  
  21.         android:layout_alignParentTop=”true”  
  22.         android:layout_centerHorizontal=”true” />  
  23.   
  24.     <ImageSwitcher  
  25.         android:id=”@+id/imageSwitcher”  
  26.         android:layout_width=”match_parent”  
  27.         android:layout_height=”250dp”  
  28.         android:layout_marginBottom=”28dp”  
  29.         android:layout_marginTop=”40dp” />  
  30.   
  31.     <Button  
  32.         android:layout_width=”wrap_content”  
  33.         android:layout_height=”wrap_content”  
  34.         android:text=”Next”  
  35.         android:id=”@+id/button”  
  36.         android:layout_marginBottom=”47dp”  
  37.         android:layout_alignParentBottom=”true”  
  38.         android:layout_centerHorizontal=”true” />  
  39. </RelativeLayout>  

Activity class

File: MainActivity.java

  1. package com.example.test.imageswitcher;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.support.v7.widget.Toolbar;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.ImageSwitcher;  
  9. import android.widget.ImageView;  
  10. import android.widget.ViewSwitcher;  
  11.   
  12. import android.app.ActionBar;  
  13. import android.view.animation.Animation;  
  14. import android.view.animation.AnimationUtils;  
  15.   
  16.   
  17. public class MainActivity extends AppCompatActivity {  
  18.     ImageSwitcher imageSwitcher;  
  19.     Button nextButton;  
  20.   
  21.     int imageSwitcherImages[] =   
  22.       {R.drawable.cpp, R.drawable.c_sarp, R.drawable.jsp, R.drawable.mysql, R.drawable.hadoop};  
  23.   
  24.     int switcherImageLength = imageSwitcherImages.length;  
  25.     int counter = -1;  
  26.   
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_main);  
  31.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
  32.         setSupportActionBar(toolbar);  
  33.   
  34.         imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);  
  35.         nextButton = (Button) findViewById(R.id.button);  
  36.   
  37.         imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {  
  38.             @Override  
  39.             public View makeView() {  
  40.                 ImageView switcherImageView = new ImageView(getApplicationContext());  
  41.                 switcherImageView.setLayoutParams(new ImageSwitcher.LayoutParams(  
  42.                         ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.FILL_PARENT  
  43.                 ));  
  44.                 switcherImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);  
  45.                 switcherImageView.setImageResource(R.drawable.hadoop);  
  46.                 //switcherImageView.setMaxHeight(100);  
  47.                 return switcherImageView;  
  48.             }  
  49.         });  
  50.   
  51.         Animation aniOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);  
  52.         Animation aniIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);  
  53.   
  54.        imageSwitcher.setOutAnimation(aniOut);  
  55.        imageSwitcher.setInAnimation(aniIn);  
  56.   
  57.         nextButton.setOnClickListener(new View.OnClickListener() {  
  58.             @Override  
  59.             public void onClick(View v) {  
  60.                 counter++;  
  61.                 if (counter == switcherImageLength){  
  62.                     counter = 0;  
  63.                     imageSwitcher.setImageResource(imageSwitcherImages[counter]);  
  64.                 }  
  65.                 else{  
  66.                     imageSwitcher.setImageResource(imageSwitcherImages[counter]);  
  67.                 }  
  68.             }  
  69.         });  
  70.     }  
  71.   
  72. }  

Output

android Image Switcher 1
android Image Switcher 2