TextView & Button Not Showing After Add DatePicker In Android -
i new in android. have app picks user date of birth using datepicker . when add feature xml elements textview & button not showing. how's possible? may know correct way achieve objective? maybe question basic, did't find suitable solution.please me out
here code :
public class userdobactivity extends activity { datepicker datepickerbirthday; textview textviewuserdate; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_user_dob); actionbar ab = getactionbar(); ab.setdisplayuselogoenabled(false); ab.setdisplayshowhomeenabled(true); ab.setdisplayoptions(actionbar.display_show_custom); ab.setcustomview(r.layout.actionbar); ab.setdisplayhomeasupenabled(true); colordrawable colordrawable = new colordrawable(color.parsecolor("#33ccff")); ab.setbackgrounddrawable(colordrawable); // create date picker datepickerbirthday = new datepicker(this); // hide whole calendar view (works in api 11 or greater) int currentapiversion = android.os.build.version.sdk_int; if (currentapiversion >= 11) { datepickerbirthday.setcalendarviewshown(false); } // create textview textviewuserdate = new textview(this); textviewuserdate.setgravity(gravity.center); // initialize date current date simpledateformat sdfdatetime = new simpledateformat("yyyy-mm-dd", locale.us); string datestr = sdfdatetime.format(new date(system.currenttimemillis())); string[] datesplit = datestr.split("-"); int currentyear = integer.parseint(datesplit[0]); int currentmonth = integer.parseint(datesplit[1]); int currentday = integer.parseint(datesplit[2]); // show date , day of week in textview sethumanreadabledate(currentyear, currentmonth, currentday); // initialize date picker listener // currentmonth - 1, because on picker, 0 january datepickerbirthday.init(currentyear, currentmonth - 1, currentday, birthdaylistener); // add container linearlayout linearlayoutcaltvcontainer = new linearlayout(this); linearlayoutcaltvcontainer.setorientation(linearlayout.vertical); linearlayoutcaltvcontainer.addview(datepickerbirthday); linearlayoutcaltvcontainer.addview(textviewuserdate); // set views activity setcontentview(linearlayoutcaltvcontainer); relativelayout layoutcaltvcontainer = (relativelayout) findviewbyid(r.id.layout); layoutcaltvcontainer.addview(datepickerbirthday); } // date picker listener ondatechangedlistener birthdaylistener = new ondatechangedlistener() { public void ondatechanged(datepicker birthdaydatepicker, int newyear, int newmonth, int newday) { try{ // currentmonth + 1, retrieve proper month sethumanreadabledate(newyear, newmonth + 1, newday); } catch (nullpointerexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } }; // show user better date format @suppresslint("simpledateformat") public void sethumanreadabledate(int newyear, int newmonth, int newday){ try { string clickeddate = newyear + "-" + newmonth + "-" + newday; simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); date d = null; try { d = sdf.parse(clickeddate); } catch (java.text.parseexception e) { // todo auto-generated catch block e.printstacktrace(); } simpledateformat sdfdatetime = new simpledateformat("mmmm dd, yyyy 'is' eeee", locale.us); string datestr = sdfdatetime.format(d); textviewuserdate.settext(datestr); } catch (parseexception e) { e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.user_dob, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: // app icon in action bar clicked; go home intent intent = new intent(this, userdetailsactivity.class); intent.addflags(intent.flag_activity_clear_top); startactivity(intent); return true; default: return super.onoptionsitemselected(item); } } @override public void onbackpressed() { movetasktoback(true); userdobactivity.this.finish(); }}
here xml :
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@android:color/white"> <textview android:id="@+id/text_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/dobdetails" android:textsize="30sp" android:layout_margintop="50dp" android:gravity="center_horizontal" /> <relativelayout android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" > </relativelayout> <button android:id="@+id/buttonuserdob" android:layout_width="match_parent" android:layout_height="60dp" android:text="@string/next" android:background="@drawable/customised_button_click" android:textsize="20sp" /> </linearlayout>
you calling setcontentview(linearlayoutcaltvcontainer);
again after setting layout r.layout.activity_user_dob
content view. override set layout , contained in layout disappear. why don't create complete layout solely in xml? try this:
put datepicker , textview layout wherever want them be:
<datepicker android:id="@+id/datepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/textviewuserdate" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
and after setting layout setcontentview(r.layout.activity_user_dob);
can retrieve instances of datepicker
, textview
this:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_user_dob); // instances of datepicker , textview layout datepicker datepickerbirthday = findviewbyid(r.id.datepicker); textview textviewuserdate = findviewbyid(r.id.textviewuserdate); // continue rest of setup int currentapiversion = android.os.build.version.sdk_int; if (currentapiversion >= 11) { datepickerbirthday.setcalendarviewshown(false); } ... }
but sure never again call setcontentview(...);
after set layout r.layout.activity_user_dob
.
Comments
Post a Comment