android - Show Edit text on spinner item selected -
i have spinner
in class items. have display edittext
visibility have set "invisible". need show edittext
when "others" item selected in spinner.
here code:-
activity class:- protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.maintenance); etservicestation = (edittext)findviewbyid(r.id.etservicestation); etservicetype = (edittext)findviewbyid(r.id.etservicetype); etservicecost= (edittext)findviewbyid(r.id.etservicecost); spinservicetype = (spinner)findviewbyid(r.id.spinservicetype); btnmaintenencesave= (imagebutton)findviewbyid(r.id.btnmaintenencesave); btnmaintenanceclear = (imagebutton)findviewbyid(r.id.btnmaintenenceclear); spinservicetype.setonitemselectedlistener(this); btnmaintenanceclear.setonclicklistener(this); btnmaintenencesave.setonclicklistener(this); arrayadapter<string> servicetypeadapter = new arrayadapter<string>(getapplicationcontext(), r.array.service_type_array, android.r.layout.simple_spinner_item); servicetypeadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinservicetype.setadapter(servicetypeadapter); } public void onitemselected(adapterview<?> parent, view view, int servicepos, long id) { // todo auto-generated method stub spinnerservicetype = spinservicetype.getitematposition(servicepos).tostring(); if(spinnerservicetype.matches("others")) { etservicetype = (edittext)findviewbyid(r.id.etservicetype); etservicetype.setvisibility(view.visible); spinnerservicetype = etservicetype.gettext().tostring(); } servicetype = spinnerservicetype; } } resource:- <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="service_type_array"> <item>full service</item> <item value="serviceother">other</item> </string-array> </resources> maintenance.xml <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="30dp"> <textview android:id="@+id/txtmtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="maintenance" android:layout_gravity="center_horizontal" android:textsize="25sp" android:textcolor="@color/steelblue" android:typeface="sans"/> <textview android:layout_margintop="60dp" android:id="@+id/txtservicestation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="service station name:" /> <edittext android:id="@+id/etservicestation" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestfocus /> </edittext> <textview android:id="@+id/txtservicetype" android:layout_margintop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="service type: "/> <spinner android:id="@+id/spinservicetype" android:layout_margintop="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <edittext android:id="@+id/etservicetype" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="numberdecimal" android:visibility="invisible" android:ems="10" /> <textview android:id="@+id/txtservicecost" android:layout_margintop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="servicing expence:" /> <edittext android:id="@+id/etservicecost" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="numberdecimal" android:ems="10" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="40dp" android:layout_marginleft="50dp" android:orientation="horizontal"> <imagebutton android:id="@+id/btnmaintenencesave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/white" android:src="@drawable/fuelbtnsaveclick" /> <imagebutton android:id="@+id/btnmaintenenceclear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:background="@color/white" android:src="@drawable/fuelbtnclearclick" /> </linearlayout> </linearlayout> database: public long createmainentry(maintenanceexpense me) { contentvalues mcv = new contentvalues(); mcv.put(key_service_station,me.getservicestation()); mcv.put(key_service_type,me.getservicetype()); mcv.put(key_service_cost,me.getservicecost()); return otherinfodatabase.insert(maintenance_database_table, null, mcv); }
i have display etservicetype when "others" item clicked. help.
public void onitemselected(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub spinnerservicetype = spinservicetype.getitematposition(position).tostring(); //if spinnerservicetype 's value other //then set visibility of textview visible put text watcher on u can text when user finished ..if u dont want u can use simple gettext change visibility //as u setting array value resource cant update programmatically //so first of u have take array items in list . u can update list after user enter value in edit text }
Comments
Post a Comment