Create Instruction Dialog in Android
Last Updated :
24 Mar, 2022
In most android applications, you must have seen that when you open a new app it shows some instructions to users about the features of their application. Here, we are going to implement the same. Here is a sample video of what we are going to build in this application. Note that we will be using Java language to make this project.
Step by Step Implementation
Step 1: Create a New Project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- Name the application at your convenience.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2. Working on XML files
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:gravity="center_horizontal"
android:padding="16dp"
tools:context=".MainActivity">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button"
android:id="@+id/radio_button"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Box"
android:id="@+id/checkbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"
android:textSize="32sp"
android:layout_marginTop="16dp"/>
</LinearLayout>
Navigate to app > res > layout > right-click > new > layout resource file and name it as dialog_instruction. Use the following code in dialog_instruction.xml file-
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center_horizontal"
android:layout_height="match_parent">
<View
android:layout_width="110dp"
android:layout_height="50dp"
android:id="@+id/view1"
android:layout_marginTop="?actionBarSize"
android:background="@drawable/outline"/>
<View
android:layout_width="160dp"
android:layout_height="50dp"
android:id="@+id/view2"
android:visibility="gone"
android:layout_marginTop="12dp"
android:background="@drawable/outline"/>
<View
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/view3"
android:visibility="gone"
android:layout_marginTop="8dp"
android:background="@drawable/outline"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_marginTop="32dp"/>
</LinearLayout>
Navigate to app > right-click > new > android resource file and name it as outline.xml. Use the following code in the outline.xml file-
XML
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="@android:color/holo_red_light"
android:width="1dp"/>
<corners android:radius="8dp"/>
</shape>
Step 3. Working on Java files
Navigate to the MainActivity.java file and use the following code in it. Comments are added to the code to have a better understanding.
Java
package com.example.instructiondialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create method
displaydialog();
}
private void displaydialog() {
// Initialize dialog
Dialog dialog=new Dialog(this);
// set view
dialog.setContentView(R.layout.dialog_instruction);
// set layout
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT
,WindowManager.LayoutParams.MATCH_PARENT);
// Set background
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// show dialog
dialog.show();
// Initialize and assign variable
View view1=dialog.findViewById(R.id.view1);
View view2=dialog.findViewById(R.id.view2);
View view3=dialog.findViewById(R.id.text_view);
TextView textView=dialog.findViewById(R.id.text_view);
// set text from button instruction
textView.setText("This is Radio Button");
view1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// hide view1
view1.setVisibility(View.INVISIBLE);
// Visible view 2
view2.setVisibility(View.VISIBLE);
// set text for text view instruction
textView.setText("This is Check box");
}
});
view2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// hide view2
view2.setVisibility(View.INVISIBLE);
// Visible view 3
view3.setVisibility(View.VISIBLE);
// set text for text view instruction
textView.setText("This is text view");
}
});
view3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// dismiss dialog
dialog.dismiss();
}
});
}
}
Here is the final output of our application.
Output:
Similar Reads
How to Create an Alert Dialog Box in Android? An Android Alert Dialog is a UI element that displays a warning or notification message and asks the user to respond with options such as Yes or No. Based on the user's response, appropriate actions are executed. Android Alert Dialog is built with the use of three fields: Title, Message area, and Ac
4 min read
How to Create Google Sign In UI using Android Studio? Nowadays, android apps are very popular. This UI has generally seen in the âGoogle Sign Inâ App. In this article, we will create a Google Sign UI in Android. Below are the various steps on how to do it. This will help the beginner to build some awesome UI in android by referring to this article. St
3 min read
How to Create Dialog with Custom Layout in Android? In Android, A dialog is a small window that prompts the user to make a decision, provide some additional information, and inform the user about some particular task. The following are the main purposes or goals of a dialog To warn the user about any activity.To inform the user about any activity.To
3 min read
Progress Dialog in Android In Android, a Progress bar is a UI element used to display the progress of any running task or an operation. An Alert Dialog is a type of alert message displayed over the screen that lets users choose between options to respond to the message of the alert. Both of these elements are different from e
3 min read
Automatic Dialog Dismiss in Android Imagine a scenario where a dialog appears, but the user doesn't interact with it. The dialog could potentially block other actions or disrupt the user experience. To address this issue, developers can employ a technique known as automating dialog dismissal using timers. In this article, we will expl
2 min read