Changing menu buttons to normal buttons - java

case R.id.Delete_Contact:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
How can i use this in an button click event ? This works when clicking menu button

//Initialize your button
//Set Click Listener to that like below
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Perform Some Action
//common method to be called
deleteConfirmation();
});
public void deleteConfirmation(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
}
});
}
You should use common method as you want same code to be executed multiple times. This is just a suggestion

Related

How to set Close button to the AlertDialog box in android studio

I want to add one more button that if someone presses the back button two times the alert dialog box should disappear or at least get a close dialog box X button on the top right side of the alert box. I have already used setpossitivebutton and setnegativebutton.
public void onBackPressed()
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.app_name);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("Wanna Exit?")
.setCancelable(false)
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("Share on Whatsapp", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"simple text");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Try this code :
AlertDialog alertDialog;
public void onBackPressed() {
if(alertDialog == null || !alertDialog.isShowing()){
alertDialog = = new AlertDialog.Builder(context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this
entry?")
.setPositiveButton(android.R.string.yes, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}else{
alertDialog.cancel();
}
}
Hope it will help you :)

How to set On back option for Navigation and Current activity

I have a created a app with List view when i click back button from home page it will toast a message and ask to exit or not. after i have implement a Navigation drawer and set open and close. but when i click back button from navigation go back and popup the toast which i set to hoe page.
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
super.onBackPressed();
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
AdminHome.super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
how to stop the toast message when i click back from navigation bar and set toast from home page.
hi You can change your code like this
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
AdminHome.super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Put the AlertDialog code in else block.
Put your alertdialog inside else of your condition
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}

Is Possible for on Back Pressed method with alert dialog in fragment

#Override
public void onBackPressed() {
Log.d("back button", "back button pressed");
AlertDialog.Builder ad1=new AlertDialog.Builder(getActivity());
ad1.setMessage("Are you sure you want to exit? ");
ad1.setCancelable(false);
ad1.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(getActivity(), LoginActivity.class);
startActivity(i);
}
});
ad1.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(getActivity(), FrndsearchFragment.class);
startActivity(i);
}
});
AlertDialog alert=ad1.create();
alert.show();
}
By using the code i getting the error in #Override and i write the super.onBackPressed i got onBackPressed in super.onBackPressed .
Yes you can do the same by overriding onKeyDown method.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Exit");
builder.setMessage("Do you want to exit the application?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
exit();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void exit() {
finish();
}

Inheriting classes from one main class

I am new in programming and i don't have much experience in inheritance. Now a days I am working in android development. This is my class code. I want to inherit all classes from this class. Only difference is inherited class should change AlertDialog message and dial number in call() method. Please tell me how to inherit this class to another what is the code should be?
public class PrepaidChineTown extends AppCompatActivity {
Button chinaButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prepaidchinatown);
chinaButton = (Button)findViewById(R.id.chinaTownBtn);
chinaButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(PrepaidChineTown.this);
builder.setTitle("China Town Offer");
builder.setMessage("Are you sure you want to activate China Town Offer? Terms and condition apply");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
call();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
public void call(){
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode("*2300#")));
try{
startActivity(in);
}catch (ActivityNotFoundException e){
Toast.makeText(getApplicationContext(), "Your Activity is not Found", Toast.LENGTH_LONG).show();
}
}
}
Create a Constant.class
private String number = "";
public class Constant {
public static void alertDialogShow(Context context,String title,String message,String _number)
{
this.number = _number;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//to do your stuff
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void call(){
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode(number)));
try{
startActivity(in);
}catch (ActivityNotFoundException e){
Toast.makeText(getApplicationContext(), "Your Activity is not Found", Toast.LENGTH_LONG).show();
}
}
}
And you can call this in any activity.
like this.
Constant.alertDialogShow(CurrentActivity.this,"Title","Message","*2300#");
public class PrepaidChineTown extends AppCompatActivity implements OnClickListener
{
Button chinaButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prepaidchinatown);
chinaButton = (Button)findViewById(R.id.chinaTownBtn);
chinaButton.setOnClickListener(this);
}
public void alertDialogue(String title,String message,Context context)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
call();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void call(){
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode("*2300#")));
try{
startActivity(in);
}catch (ActivityNotFoundException e){
Toast.makeText(getApplicationContext(), "Your Activity is not Found", Toast.LENGTH_LONG).show();
}
}
Now Create object of PrepaidChineTown and call alertDialogue() by passing required parameters or by extending this class you can use that method to show dialogue

Java - Android Dialog Box via Opening Button

So I have a button with an on click listener. It's supposed to open a dialog window with options... but it doesn't.
Anyone know what the problem is?
seeTexts.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
}
});
EDIT (UPDATED CODE):
seeTexts.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
You just don't call the show method of your AlertDialog object

Categories

Resources