first of all thanks for reading this :)
Here is my problem.
I got an EditText (User date input) and i want to get a colored background on my CustomCalander for that day using DayDecorator.
//EditText I want to get the date
huablauf = (EditText)getActivity().findViewById(R.id.huablauf);
//adding calendar day decorators
List<DayDecorator> decorators = new ArrayList<>();
decorators.add(new Markierung());
calendarView.setDecorators(decorators);
calendarView.refreshCalendar(currentCalendar);
//class Markierung
private class Markierung implements DayDecorator {
EditText huablauf=(EditText)getActivity().findViewById(R.id.huablauf);
#Override
public void decorate(DayView dayView) {
// I know I have to get my String to a date and then get it to
// dayView.getDate()! But my code didnĀ“t work
dayView.getDate();
int color = Color.parseColor("#f9acb8");
dayView.setBackgroundColor(color);
}
Hope you can understand my problem and anybody can help :)
Thx from Bavaria
Try this:
#Override
public void decorate(DayView dayView) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault());
try{
if(dayView.getDate().equals(dateFormat.parse(huablauf.getText().toString()))){
int color = Color.parseColor("#f9acb8");
dayView.setBackgroundColor(color);
}
} catch (Exception e){
//Handle exception.
}
Related
How can I customize the X axis to show days (or months or years, based on the selected range) where a new day/month/year begins? I am using CategoryDateAxis (CreateMultiPaneStockChartsFragment example).
What I want:
Larger ranges:
Smaller ranges (easily see where new day begins):
What I have:
Right now I am using default label provider and it is hard to see when new day/month/year begins. E.g. for 7 day range:
Axis is construced like this:
final CategoryDateAxis xAxis = sciChartBuilder.newCategoryDateAxis()
.withVisibility(isMainPane ? View.VISIBLE : View.GONE)
.withVisibleRange(sharedXRange)
//.withLabelProvider(new TradeChartAxisLabelProviderDateTime())
.withGrowBy(0.01d, 0.01d)
.build();
How do I achieve this?
public static class TradeChartAxisLabelProviderDateTime extends TradeChartAxisLabelProvider {
public TradeChartAxisLabelProviderDateTime() {
super();
}
#Override
public String formatLabel(Comparable dataValue) {
if(currentRange == RANGE_1_YEAR) {
} else if(currentRange == RANGE_1_MONTH) {
} else if(currentRange == RANGE_1_DAY) {
}
String text = super.formatLabel(dataValue).toString();
return text;
}
}
To implement selection of label based on VisibleRange you can use code like this:
public static class TradeChartAxisLabelProviderDateTime extends TradeChartAxisLabelProvider {
public TradeChartAxisLabelProviderDateTime() {
super(new TradeChartAxisLabelFormatterDateTime());
}
private static class TradeChartAxisLabelFormatterDateTime implements ILabelFormatter<CategoryDateAxis> {
private final SimpleDateFormat labelFormat, cursorLabelFormat;
private TradeChartAxisLabelFormatterDateTime() {
labelFormat = new SimpleDateFormat(CategoryDateAxis.DEFAULT_TEXT_FORMATTING, Locale.getDefault());
cursorLabelFormat = new SimpleDateFormat(CategoryDateAxis.DEFAULT_TEXT_FORMATTING, Locale.getDefault());
}
#Override
public void update(CategoryDateAxis axis) {
final ICategoryLabelProvider labelProvider = Guard.instanceOfAndNotNull(axis.getLabelProvider(), ICategoryLabelProvider.class);
// this is range of indices which are drawn by CategoryDateAxis
final IRange<Double> visibleRange = axis.getVisibleRange();
// convert indicies to range of dates
final DateRange dateRange = new DateRange(
ComparableUtil.toDate(labelProvider.transformIndexToData((int) NumberUtil.constrain(Math.floor(visibleRange.getMin()), 0, Integer.MAX_VALUE))),
ComparableUtil.toDate(labelProvider.transformIndexToData((int) NumberUtil.constrain(Math.ceil(visibleRange.getMax()), 0, Integer.MAX_VALUE))));
if (dateRange.getIsDefined()) {
long ticksInViewport = dateRange.getDiff().getTime();
// select formatting based on diff in time between Min and Max
if (ticksInViewport > DateIntervalUtil.fromYears(1)) {
// apply year formatting
labelFormat.applyPattern("");
cursorLabelFormat.applyPattern("");
} else if (ticksInViewport > DateIntervalUtil.fromMonths(1)) {
// apply month formatting
labelFormat.applyPattern("");
cursorLabelFormat.applyPattern("");
} else if (ticksInViewport > DateIntervalUtil.fromMonths(1)) {
// apply day formatting
labelFormat.applyPattern("");
cursorLabelFormat.applyPattern("");
}
}
}
#Override
public CharSequence formatLabel(Comparable dataValue) {
final Date valueToFormat = ComparableUtil.toDate(dataValue);
return labelFormat.format(valueToFormat);
}
#Override
public CharSequence formatCursorLabel(Comparable dataValue) {
final Date valueToFormat = ComparableUtil.toDate(dataValue);
return cursorLabelFormat.format(valueToFormat);
}
}
}
In update() you can get access to VisibleRange of axis and based on it select label formatting and then use SimpleDateFormat to format Dates.
But as I understand your case is more complex than this because you can't get labels which allow to see when new day/month/year begins based on current VisibleRange. For this case you'll need to select format string based on previously formatted values and track when day/month/year changes.
I set time/date as a x value in my real-time graph, but the time didn't match with my android's time, can someone check my code
final DateFormat sdf = android.text.format.DateFormat.getTimeFormat(this);
graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
#Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX) {
long Valuemilis = (new Double(value)).longValue();
return sdf.format(Valuemilis*1000);
}
else {
return null;
}
}
});
app's picture
To get your system time in milliseconds you can use Date.java class.
The below snippet of code may solve your problem.
long Valuemilis = new Date().getTime();
I am trying to display a date in descending order in listview in android... I have written a program... It is showing it correctly, but when the first date of the month coming, the last month date are not displaying only one date is showing... What is the reason? How do I improve my code? Please guide me..
my code is here...
public void datesadd()
{
listview.setAdapter(new ListAdapter(this));
cc1=Calendar.getInstance();
int mon1=cc1.getTime().getDate();
Date dd=new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
while(mon1>=count)
{ pos=0;
line=new HashMap<String,String>();
String cdat=String.valueOf(cc1.get(Calendar.DATE));
#SuppressWarnings("deprecation")
String mons=String.valueOf(cc1.get(Calendar.MONTH));
String day1=String.valueOf(cc1.getTime().getDay());
#SuppressWarnings("deprecation")
String year1=String.valueOf(cc1.get(Calendar.YEAR));
try {
dd=format.parse(year1+"-"+mons+"-"+cdat);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
line.put("yeari", year1);
line.put("mont",mons);
line.put("dayi",dd.toString().substring(0, 3));
line.put("datei",cdat);
mon1--;
cc1.add(Calendar.DATE, -1);
Log.v("", "line");
disp.add(line);
// here disp is ArrayList<Hashmap<String,String>> object, i was declared it on top of my main program
}
}
I don't think you're going about date iteration right -- you're just iterating over the value in miliseconds.
Consider this:
/* From your code I'm not sure what count is, but you get the idea, you need
* a Date object here or you can just use a for() loop if you know the number
* |
* V */
while (cc1.getTime().after(count)) {
cc1.add(Calendar.DAY_OF_YEAR /*or month, year, whatever*/, -1);
// and then continue with your own code
line = new HashMap<String,String>();
String cdat = String.valueOf(cc1.get(Calendar.DATE));
// ...
}
I am trying to display the time that a user enters, either in a toast or using a basic println command. If the time is in the correct format, I want to enter it in a database along with the current time.
Here is what I have so far:
public void addListenerOnButton ()
{
spinner_hours = (Spinner)findViewById(R.id.hours);
spinner_minutes = (Spinner)findViewById(R.id.minutes);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Date time;
String enteredTime= String.valueOf(spinner_hours.getSelectedItem())+String.valueOf(spinner_minutes.getSelectedItem())+"00";
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
try {
time= format.parse(enteredTime);
System.out.println(enteredTime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Toast.makeText(MainActivity.this,
// "Your alarm has been set for : " + time,
// Toast.LENGTH_SHORT).show();
}
});
}
If I put the toast (commented below) inside the try block, it doesnt show up when I run the app. If I put a system.out.println it does not print anything. How do I print the entered time?
Also, I want to enter the current time in the same format ("HH:mm:ss") in the database. Any ideas on how to do that?
It's probably throwing an exception, since System.out.println should work. Try replacing this:
String enteredTime= String.valueOf(spinner_hours.getSelectedItem())+String.valueOf(spinner_minutes.getSelectedItem())+"00";
by this:
String enteredTime= String.valueOf(spinner_hours.getSelectedItem())+ ":" + String.valueOf(spinner_minutes.getSelectedItem())+":00";
It seems you're missing the : between hour and minutes and before 00 and the parsing is failing.
First try to print the value got from the Spinner
System.out.println(spinner_hours.getSelectedItem().toString());
System.out.println(spinner_minutes.getSelectedItem().toString());
If this doesn't has any value then the problem is here.
folks!
I've got such date entry:
<pubDate>23/06/2011 11:57</pubDate>
I try to parse it:
mDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
item_tag.getChild("pubDate").setEndTextElementListener(new EndTextElementListener() {
#Override
public void end(String body) {
date= new GregorianCalendar(0,0,0).getTime();
try {
date=mDateFormat.parse(body);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
But I get something like that: 23/57/2011 11:57 I get minutes instead of month value
What's wrong with my code?
Your code looks OK. Check out what do you really pass to this method, or just check what will happen if you insert line like:
body = "23/06/2011 11:57";
As shown in examples here, you should use '-' instead of '/' when instantiating a mask.