create Date TextView - Android User Interface

Android examples for User Interface:TextView

Description

create Date TextView

Demo Code


import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Main{
    private static boolean dialogResult = false;
    private static String dateString = null;
    /**/*from  w ww .j av a  2 s.  c o m*/
     * @fn public static TextView createDateTextView(Context context,String message,int size,int textColor,int backgroundColor)
     * @brief Creates a textview object that displays a datepicker on click.
     * @param context 
     * @param message Message to be displayed in the text view
     * @param size Text size
     * @param textColor Numerical representation of color. Use android.graphics.Color.rgb(red,green,blue)
     * @param backgroundColor Numerical representation of color. Use android.graphics.Color.rgb(red,green,blue)
     * @return Created TextView Object.
     */

    public static TextView createDateTextView(final Context context,
            String message, int size, int textColor, int backgroundColor) {
        /// http://stackoverflow.com/questions/11504635/layout-margin-for-text-view-programmatically
        TextView textView = new TextView(context);
        TableRow.LayoutParams tvlp = new TableRow.LayoutParams(
                TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.MATCH_PARENT);
        textView.setLayoutParams(tvlp);
        tvlp.setMargins(2, 2, 2, 2);
        textView.setText(message);
        textView.setTextSize(size);
        textView.setBackgroundColor(backgroundColor);
        textView.setTextColor(textColor);
        /// http://stackoverflow.com/questions/432037/how-do-i-center-text-horizontally-and-vertical-in-a-textview-in-android
        textView.setGravity(Gravity.CENTER_VERTICAL
                | Gravity.CENTER_HORIZONTAL);
        textView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // Use the current date as the default date in the picker
                final Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);

                LayoutUtils.displayDatePickerDialog(context, v, year,
                        month, day);

            }
        });

        return textView;
    }
    public static void displayDatePickerDialog(final Context context,
            final View v, int year, int month, int day) {

        /// Date picker dialog = http://www.learn-android-easily.com/2013/06/datepicker-and-timepicker-dialog-in.html
        String title = "Select Date";

        // Register  DatePickerDialog listener
        DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            // the callback received when the user "sets" the Date in the DatePickerDialog
            public void onDateSet(DatePicker view, int yearSelected,
                    int monthOfYear, int dayOfMonth) {

                //  LayoutUtils.displayToast(context, "Setting date");

                // Set the Selected Date in Select date Button
                if (LayoutUtils.getDialogResult()) {
                    LayoutUtils.setDate(yearSelected + "-"
                            + Integer.toString(monthOfYear + 1) + "-"
                            + dayOfMonth);
                    TextView tv = (TextView) v;
                    tv.setText(LayoutUtils.getDate());
                }

            }
        };

        DatePickerDialog dpd = new DatePickerDialog(context,
                mDateSetListener, year, month, day);

        /// Cancel date picker dialog = http://stackoverflow.com/questions/11444238/jelly-bean-datepickerdialog-is-there-a-way-to-cancel
        dpd.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        LayoutUtils.setDialogResult(true);
                        dialog.cancel();

                    }
                });
        dpd.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LayoutUtils.setDialogResult(false);
                        dialog.cancel();
                    }
                });

        dpd.show();

    }
    public static boolean getDialogResult() {
        return dialogResult;
    }
    public static void setDate(String date) {
        dateString = date;
    }
    public static String getDate() {
        return dateString;
    }
    public static void setDialogResult(boolean result) {
        dialogResult = result;
    }
}

Related Tutorials