Skip to content

Instantly share code, notes, and snippets.

@rajivnarayana
Last active December 14, 2015 10:49
Show Gist options
  • Save rajivnarayana/5075019 to your computer and use it in GitHub Desktop.
Save rajivnarayana/5075019 to your computer and use it in GitHub Desktop.
An android widget to display date as calendar.
package com.webileapps.dateiconwidget;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DateIconWidget extends LinearLayout {
private TextView monthTextView;
private TextView dayTextView;
// private TextView yearTextView;
public DateIconWidget(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DateIconWidget(Context context) {
super(context);
init(context);
}
private int toPx(int dp) {
return Math.max(1, (int)(multiplicationFactor*dp));
}
private float multiplicationFactor = 1.0f;
private void init(Context context) {
this.setOrientation(LinearLayout.VERTICAL);
this.setBackgroundColor(Color.BLACK);
Resources r = context.getResources();
multiplicationFactor = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());
this.setPadding(toPx(1), toPx(1), toPx(1), toPx(2));
monthTextView = new TextView(context);
monthTextView.setLines(1);
monthTextView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
monthTextView.setTextColor(Color.WHITE);
monthTextView.setBackgroundColor(Color.RED);
monthTextView.setTextSize(13.f);
monthTextView.setTypeface(Typeface.DEFAULT_BOLD);
LinearLayout.LayoutParams lp =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,1);
// monthTextView.setPadding(0, 5, 0, 0);
this.addView(monthTextView, lp);
dayTextView = new TextView(context);
dayTextView.setGravity(Gravity.CENTER);
dayTextView.setTextColor(Color.BLACK);
dayTextView.setBackgroundColor(Color.WHITE);
dayTextView.setTextSize(20.f);
dayTextView.setTypeface(Typeface.DEFAULT_BOLD);
this.addView(dayTextView, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,0));
// yearTextView = new TextView(context);
// yearTextView.setGravity(Gravity.CENTER);
// yearTextView.setTextColor(Color.WHITE);
// yearTextView.setBackgroundColor(Color.RED);
// yearTextView.setTextSize(10.f);
// this.addView(yearTextView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setDate(new Date());
}
@SuppressLint("SimpleDateFormat")
public void setDate(Date date) {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yy-MMM-dd");
String formattedDate = dateFormatter.format(date);
String[] tokens = formattedDate.split("-");
if (tokens.length != 3) {
return;
}
// yearTextView.setText();
monthTextView.setText(tokens[1]+" '"+tokens[0]);
dayTextView.setText(tokens[2]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment