alertdialogpractice.java package key.lab.alertdialogpractice; import java.nio.channels.AlreadyConnectedException; import android.app....
alertdialogpractice.java
package key.lab.alertdialogpractice;
import java.nio.channels.AlreadyConnectedException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainPracticeActivity extends Activity {
Button onebtn, twobtn, threebtn, multichoicebtn, singlechoicebtn, listbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_practice);
onebtn = (Button) findViewById(R.id.btnmessage);
twobtn = (Button) findViewById(R.id.btnTwoButton);
threebtn = (Button) findViewById(R.id.btnThreeButton);
multichoicebtn = (Button) findViewById(R.id.btnMultiChoice);
singlechoicebtn = (Button) findViewById(R.id.btnSingleChoice);
listbtn = (Button) findViewById(R.id.btnList);
onebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog adOne = new AlertDialog.Builder(
MainPracticeActivity.this).create();
adOne.setTitle("Show Message");
adOne.setMessage("Here Is The Message");
adOne.setIcon(R.drawable.ic_launcher);
adOne.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"you select ok ", Toast.LENGTH_SHORT).show();
}
});
adOne.show();
}
});
twobtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog adtwo = new AlertDialog.Builder(
MainPracticeActivity.this).create();
adtwo.setTitle("Delete");
adtwo.setMessage("Are you sure ??");
adtwo.setButton("Yes", new DialogInterface.OnClickListener() { //you can also use setPositiveButton() for YES
//and setNegativeButton() for NO
//it is use for lower android versions
//netural is used for the nothing setNeutralButton()
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"You clicked Yes", Toast.LENGTH_SHORT).show();
}
});
adtwo.setButton2("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"you selected NO ", Toast.LENGTH_SHORT).show();
}
});
adtwo.show();
}
});
threebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog adthree = new AlertDialog.Builder(
MainPracticeActivity.this).create();
adthree.setTitle("Contact Us");
adthree.setButton("call",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(getApplicationContext(),
"You selected CALL",
Toast.LENGTH_SHORT).show();
}
});
adthree.setButton2("Email", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "you selected Email ", Toast.LENGTH_SHORT).show();
}
});
adthree.setButton3("Text", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "you selected Text ", Toast.LENGTH_SHORT).show();
}
});
adthree.show();
}
});
multichoicebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog admulti =new AlertDialog.Builder(MainPracticeActivity.this).setIcon(R.drawable.ic_launcher).setTitle("Select").setMultiChoiceItems(R.array.fruits,new boolean[]{true,true,false,false,false,true} ,new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
} )
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int pos) {
String fruit[]=getResources().getStringArray(R.array.fruits);
// Toast.makeText(getApplicationContext(), "you selecte the "+pos+","+fruit[pos], Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
});
singlechoicebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog adsingle =new AlertDialog.Builder(MainPracticeActivity.this).setTitle("Select Any One")
.setSingleChoiceItems(R.array.fruits, 3, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
});
listbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog=new Dialog(MainPracticeActivity.this);
dialog.setContentView(R.layout.dialog_call);
dialog.setTitle("Contact Us");
final EditText edit=(EditText)dialog.findViewById(R.id.etCall);
Button btcall=(Button)dialog.findViewById(R.id.btnCall);
btcall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+edit.getText().toString()));
startActivity(intent);
}
});
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_practice, menu);
return true;
}
}
package key.lab.alertdialogpractice;
import java.nio.channels.AlreadyConnectedException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainPracticeActivity extends Activity {
Button onebtn, twobtn, threebtn, multichoicebtn, singlechoicebtn, listbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_practice);
onebtn = (Button) findViewById(R.id.btnmessage);
twobtn = (Button) findViewById(R.id.btnTwoButton);
threebtn = (Button) findViewById(R.id.btnThreeButton);
multichoicebtn = (Button) findViewById(R.id.btnMultiChoice);
singlechoicebtn = (Button) findViewById(R.id.btnSingleChoice);
listbtn = (Button) findViewById(R.id.btnList);
onebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog adOne = new AlertDialog.Builder(
MainPracticeActivity.this).create();
adOne.setTitle("Show Message");
adOne.setMessage("Here Is The Message");
adOne.setIcon(R.drawable.ic_launcher);
adOne.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"you select ok ", Toast.LENGTH_SHORT).show();
}
});
adOne.show();
}
});
twobtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog adtwo = new AlertDialog.Builder(
MainPracticeActivity.this).create();
adtwo.setTitle("Delete");
adtwo.setMessage("Are you sure ??");
adtwo.setButton("Yes", new DialogInterface.OnClickListener() { //you can also use setPositiveButton() for YES
//and setNegativeButton() for NO
//it is use for lower android versions
//netural is used for the nothing setNeutralButton()
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"You clicked Yes", Toast.LENGTH_SHORT).show();
}
});
adtwo.setButton2("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"you selected NO ", Toast.LENGTH_SHORT).show();
}
});
adtwo.show();
}
});
threebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog adthree = new AlertDialog.Builder(
MainPracticeActivity.this).create();
adthree.setTitle("Contact Us");
adthree.setButton("call",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(getApplicationContext(),
"You selected CALL",
Toast.LENGTH_SHORT).show();
}
});
adthree.setButton2("Email", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "you selected Email ", Toast.LENGTH_SHORT).show();
}
});
adthree.setButton3("Text", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "you selected Text ", Toast.LENGTH_SHORT).show();
}
});
adthree.show();
}
});
multichoicebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog admulti =new AlertDialog.Builder(MainPracticeActivity.this).setIcon(R.drawable.ic_launcher).setTitle("Select").setMultiChoiceItems(R.array.fruits,new boolean[]{true,true,false,false,false,true} ,new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
} )
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int pos) {
String fruit[]=getResources().getStringArray(R.array.fruits);
// Toast.makeText(getApplicationContext(), "you selecte the "+pos+","+fruit[pos], Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
});
singlechoicebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog adsingle =new AlertDialog.Builder(MainPracticeActivity.this).setTitle("Select Any One")
.setSingleChoiceItems(R.array.fruits, 3, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
});
listbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog=new Dialog(MainPracticeActivity.this);
dialog.setContentView(R.layout.dialog_call);
dialog.setTitle("Contact Us");
final EditText edit=(EditText)dialog.findViewById(R.id.etCall);
Button btcall=(Button)dialog.findViewById(R.id.btnCall);
btcall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+edit.getText().toString()));
startActivity(intent);
}
});
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_practice, menu);
return true;
}
}
acitvity_main_parctice.xml
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainPracticeActivity" >
<Button
android:id="@+id/btnmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Alert Message" />
<Button
android:id="@+id/btnTwoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnmessage"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="Two Button Alert" />
<Button
android:id="@+id/btnThreeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnTwoButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Three Button Alert" />
<Button
android:id="@+id/btnMultiChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnThreeButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="MultiChoice Button" />
<Button
android:id="@+id/btnSingleChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnMultiChoice"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="SingleChoice Button" />
<Button
android:id="@+id/btnList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSingleChoice"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="List Button" />
</RelativeLayout>
dialog_call.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/etCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Num"
android:inputType="numberPassword" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etCall"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="Call" />
</RelativeLayout>