Hello all,
It is so strange that there is problem when I implement the same Datepicker method on Java code not XML.
XML CODE approach:
public class DateActivity extends Activity implements OnClickListener
{
private static final int DATE_ID=0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
dateButton = (Button) findViewById(R.id.dateButton);
dateButton.setOnClickListener(this)
}
public void onClick(View v)
{
case R.id.dateButton:
showDialog(DATE_ID);
break;
}
@Override
protected Dialog onCreateDialog(int id)
{
Calendar cal = Calendar.getInstance();
int calyear = cal.get(Calendar.YEAR);
int calmonth = cal.get(Calendar.MONTH);
int calday = cal.get(Calendar.DAY_OF_MONTH);
switch (id)
{
case DATE_ID:
return new DatePickerDialog(this, mDateSetListener, calyear, calmonth, calday);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
chosenDate = incomehelper.getSelectedDate(dayOfMonth,monthOfYear,year);
Toast.makeText(DateActivity.this, "Date Selected is ="+chosenDate, Toast.LENGTH_SHORT).show();
}
};
}
JAVA code approach:
public class IncomeLayout extends Activity implements TabHost.TabContentFactory, OnClickListener
{
private Activity activity;
private LayoutInflater inflaterHelper;
private LinearLayout layout;
private static final String tab1 = "something";
private static final String tab2 = "otherthing";
private Button dateButton = null;
private static final int DATE_ID = 0;
public IncomeLayout(Activity a)
{
activity = a;
inflaterHelper = a.getLayoutInflater();
}
public View createTabContent(String tag)
{
return addCustomView(tag);
}
public View addCustomView(String id)
{
layout = new LinearLayout(activity);
layout.setOrientation(LinearLayout.VERTICAL);
if(id.equals(tab1))
{
dateButton = new Button(activity);
dateButton.setText("Choose Date");
dateButton.setId(R.id.dateButton);
layout.addView(dateButton,
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
dateButton.setOnClickListener(this);
}
else if (id.equals(tab2))
{
}
return layout;
}
public void onNothingSelected(AdapterView<?> parentView)
{
}
public void onClick(View v)
{
switch (v.getId())
case R.id.dateButton:
showDialog(DATE_ID);
break;
}
@Override
protected Dialog onCreateDialog(int id)
{
Calendar cal = Calendar.getInstance();
int calyear = cal.get(Calendar.YEAR);
int calmonth = cal.get(Calendar.MONTH);
int calday = cal.get(Calendar.DAY_OF_MONTH);
switch (id)
{
case DATE_ID:
return new DatePickerDialog(activity, mDateSetListener, calyear, calmonth, calday);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
chosenDate = incomehelper.getSelectedDate(dayOfMonth,monthOfYear,year);
Toast.makeText(IncomeLayout.this, "Date Selected is ="+chosenDate, Toast.LENGTH_SHORT).show();
}
};
}
I have xml to define the id, so I obtain those id and setid to widget on the fly. The problem is on Datepicker method, but if I use xml to create widget, that is no problem. But my program requires widget creation dynamically.
Anyone has similar situation and problem ? It seems that it is unable to get the Calendar year/month/date for Java code method.
Thanks for sharing !!
Tom