Wednesday, April 1, 2020

Alert Dialog Box


Alert Dialog Box
Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.
Android AlertDialog is composed of three regions: title, content area and action buttons.
Android AlertDialog is the subclass of Dialog class.

e.g
  
    @Override  
protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
                  setContentView(R.layout.activity_main);  
   AlertDialog.Builder builder = new AlertDialog.Builder(this); //this will build a alert dialog 
        builder.setMessage("Do you want to close this application ?")   //this will show the msg on alert dialog
            .setCancelable(false)    //we cancel the alertbox
            .setPositiveButton("Yes"new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int id) {  
                finish();                        //what will happen if we click on YES, define in this
                }  
            })  
            .setNegativeButton("No"new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int id) {  
                //  Action for 'NO' Button  
                dialog.cancel();             // what will happen if we click on NO, define in this
             }  
            });  
  
        //Creating dialog box  
        AlertDialog alert = builder.create();  
        //Setting the title manually  
        alert.setTitle("AlertDialogExample");  
        alert.show();  
}

   

Wednesday, February 19, 2020

Convert Multiple Date Format by Single Method #android #java #date

Create a method in a Global Class From where you can access this method in every class in Android(Java). 

public static String convertDateFormat(String inputFormat, String outPutFormat) {

List<SimpleDateFormat> knownPatterns = new ArrayList<SimpleDateFormat>();
knownPatterns.add(new SimpleDateFormat("dd/MM/yyyy"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd HH:MM:SS"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
DateFormat outputFormat = new SimpleDateFormat(outPutFormat);
Date date = null;
for (int i = 0; i < knownPatterns.size(); i++) {
try {
date = knownPatterns.get(i).parse(inputFormat);
} catch (ParseException e) {
e.printStackTrace();
}
}
return outputFormat.format(date);
}


As I have Created method convertDateFormat in Java class Utility.java as shown above.

Now Call this method in the main class where you want to format date

Utility.convertDateFormat(userProfile.getDateOfBirth(), "dd/MM/yyyy")

Now, here  Utility is java class where i have made method and i am calling that method in class where i want to convert Date Format.

Note:-where (userProfile.getDateOfBirth()) is input date which we want to convert and ( "dd/MM/yyyy") is output format date which you can give any format which is shown in method above.for another format which is not in method you can add that format in method.

Alert Dialog Box

Alert Dialog Box Android AlertDialog   can be used to display the dialog message with OK and Cancel buttons. It can be used to inter...