Creating a Alert Message Box
Code Here :
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Alert Dialog");
builder.setMessage("Welcome to Android Application");
builder.setPositiveButton("OK", null);
builder.show();
builder.setTitle("Alert Dialog");
builder.setMessage("Welcome to Android Application");
builder.setPositiveButton("OK", null);
builder.show();
Code to Display Toast when OK button is pressed
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Alert Dialog");
builder.setMessage("Welcome to Android Application");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show();
}
Add a button to activity_main.xml
builder.setTitle("Alert Dialog");
builder.setMessage("Welcome to Android Application");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show();
}
example 2:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button oButton=(Button)findViewById(R.id.button); oButton.setOnClickListener( new Button.OnClickListener(){ public void onClick(View v){ new misc().alert_box("hi","hi",MainActivity.this); } } ); } }
-----------------------------------------
package com.example.dejayapr.test1; import android.app.AlertDialog; import android.content.Context; class misc { public void alert_box(String title,String message,Context c){ AlertDialog alertDialog = new AlertDialog.Builder(c).create(); alertDialog.setTitle(title); alertDialog.setMessage(message); alertDialog.show(); } }
-----------
It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving... very specific nice content.Android Training in chennai with placement | Android Training in velachery
ReplyDelete