Friday 27 February 2015

Helper Methods in JAVA/Android


 Helper Methods in JAVA/Android. I will add more to this Post..



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.jsp.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

public class HelperMethods {

 // 1) Convert datetime from one format to other format

 public static String changeDateFormat(String inputDateString) {

  String formatedDate = "";

  // yyyy-dd-MM HH:mm
  SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-dd-MM HH:mm",
    Locale.getDefault());

  // MMM d yyyy HH:mm
  // MMM d yy HH:mm
  SimpleDateFormat myFormat = new SimpleDateFormat("MMM d yyyy HH:mm",
    Locale.getDefault());

  try {
   formatedDate = myFormat.format(inputFormat.parse(inputDateString));

   System.out.print(formatedDate);

  } catch (java.text.ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  return formatedDate;
 }

 // 2) Get Present DateTime

 public String getPresentDatetime() {
  Calendar c = Calendar.getInstance();

  // dd-MM-yyyy KK:mm:ss a
  SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy KK:mm:ss a",
    Locale.getDefault());
  String formattedDate = df.format(c.getTime());
  return formattedDate;

 }

 // 3) Toast Helper Methods

 public static void showToastAtCenter(Context context, String text) {
  Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
  toast.setGravity(Gravity.CENTER, 0, 0);
  Log.i("Seltis-showToast", "" + text);
  toast.show();
 }

 public static void showToastAtBottom(Context context, String text) {
  Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
  toast.setGravity(Gravity.BOTTOM, 0, 0);
  Log.i("Seltis-showToastBotom", "" + text);
  toast.show();
 }

 // 4) Alert Dialog
 public static void showAlertDialog(Context context, String title,
   String message) {
  AlertDialog.Builder builder = new AlertDialog.Builder(context);
  if (title != null)
   builder.setTitle(title);
  builder.setMessage(message);
  builder.setNegativeButton("OK", null);
  builder.show();
 }

}

No comments:

Post a Comment