Skip to main content

how to make an alert message in android

  Alert box code-activity.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"     app :layoutDescription ="@xml/activity_main_scene"     tools :context =".MainActivity" >     <TextView         android :id ="@+id/textView"         android :layout_width ="wrap_content"         android :layout_height ="wrap_content"          />     <Button         android :id ="@...

how to make an alert message in android

 

Alert box code-activity.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"
   
app:layoutDescription="@xml/activity_main_scene"
   
tools:context=".MainActivity">

    <TextView
       
android:id="@+id/textView"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
        
/>

    <Button
       
android:id="@+id/button2"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginBottom="43dp"
       
android:onClick="showAlert"

       
android:text="Button" />


</RelativeLayout>

Java coding

package com.example.test;

import
androidx.appcompat.app.AppCompatActivity;
import
android.app.AlertDialog;
import
android.content.DialogInterface;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;

import
android.os.Bundle;

public class
MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_main);
       
Button alertButton = findViewById(R.id.button2);
       
alertButton.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                showAlert()
;
           
}
        })
;
   
}


   
public void showAlert() {
        AlertDialog.Builder builder =
new AlertDialog.Builder(this);
       
builder.setTitle("Alert");
       
builder.setMessage("This is an alert message");
       
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
           
@Override
           
public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss()
;
            
}
        })
;

       
AlertDialog alertDialog = builder.create();
       
alertDialog.show();
   
}
}

 

Comments

Popular posts from this blog

How did the Indian subcontinent join to Asian Continent?

                                                    1                                      Indian subcontinent   Continent Drift Today Indian subcontinent is located in the Indian Ocean and Asian continent. But, the Indian subcontinent Once was attached between Africa and Madagascar. In 1912 Alfred Wegener invented continental drift. Before the twelfth century, people believed the continent had not moved. " The Origin of Ocean and continent"   was written by Alfred Wegener.       He explains present continents gathered in a single land mass that later drifted apart. So our world map shows a jigsaw puzzle shape with different continents and countries that appeared like they could fit each other. These countries are far apart, but their...

Indo-Soviet relationship

Mitrokhin Archive -Special Relationship with India(Indo-Soviet Relationship) Mitrokhin Archive -2 book's 17 and 18 chapter describes The special relationship with India. It represented the Indo-Soviet relationship during the cold war and KGB measurements in Indian politics.                                                       Indo-Soviet Relationship-Nehru period      During the cold war, KGB concentrated on the third-world country India. Stalin and his followers thought India was an Imperialist Puppet. The great Soviet encyclopedia dismissed Mohandas Mahathma Gandhi as a reactionary who betrayed the people and helped imperialism against them. Despite his distaste, the Stalinists attacked Jawaharlal Nehru. They regarded Nehru as a reactionary like Gandhi.KGB penetrated the Indian embassy during 1950-1960.         ...