How to create a Custom Dialog box on Android?

AndroidApps/ApplicationsMobile Development

This example demonstrates about how do I create a custom message in Android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
   tools:context=".MainActivity">
   <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="My Custom Message"
      android:textSize="22sp"
      android:textAlignment="center"
      android:layout_marginTop="30dp"
      android:id="@+id/myCustommessage"/>
   <Button
      android:layout_width="140dp"
      android:layout_height="80dp"
      android:layout_centerInParent="true"
      android:text="Show Message"
      android:backgroundTint="@color/colorPrimary"
      android:textSize="20dp"
      android:textColor="#ffffff"
      android:onClick="btn_showMessage" />
</RelativeLayout>

Step 3 − Click res from Project → Right click on layout → Select New → Layout resource file → Name the layout = “ custom_dialog, enter Linear layout in the “Root element” and click okay

Step 4 − Add the following code to custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="15dp">
   <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Enter your Message"
      android:textSize="22sp"
      android:textAlignment="center" />
   <EditText
      android:id="@+id/txt_input"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="10dp"
      android:hint="Enter your Message"
      android:textSize="20sp" />
   <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="15dp">
      <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:backgroundTint="@color/colorPrimary"
         android:text="Cancel"
         android:textColor="#ffffff"
         android:textSize="18sp"
         android:layout_weight="1"
         android:id="@+id/btn_cancel"/>
     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:backgroundTint="@color/colorPrimary"
         android:text="Okay"
         android:textColor="#ffffff"
         android:textSize="18sp"
         android:layout_weight="1"
         android:id="@+id/btn_okay"/>
   </LinearLayout>
</LinearLayout>

Step 5 − Add the following code to src/MainActivity.java

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   TextView myCustomMessage;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      myCustomMessage = (TextView)findViewById(R.id.myCustommessage);
   }
   public void btn_showMessage(View view){
      final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
      View mView = getLayoutInflater().inflate(R.layout.custom_dialog,null);
      final EditText txt_inputText = (EditText)mView.findViewById(R.id.txt_input);
      Button btn_cancel = (Button)mView.findViewById(R.id.btn_cancel);
      Button btn_okay = (Button)mView.findViewById(R.id.btn_okay);
      alert.setView(mView);
      final AlertDialog alertDialog = alert.create();
      alertDialog.setCanceledOnTouchOutside(false);
      btn_cancel.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            alertDialog.dismiss();
         }
      });
      btn_okay.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myCustomMessage.setText(txt_inputText.getText().toString());
            alertDialog.dismiss();
         }
      });
      alertDialog.show();
   }
}

Step 6 − Add the following code to androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −


Click here to download the project code.

raja
Published on 31-Jul-2019 11:16:12
Advertisements