java - Android dynamically add custom component to LinearLayout -
i want add custom component on linearlayout whenever touch button.
this code:
layoutinflater vi = (layoutinflater) getapplicationcontext().getsystemservice(context.layout_inflater_service); linearlayout root = (linearlayout) findviewbyid(r.id.layout_cartelle_immagini); view custom = vi.inflate(r.layout.custom_list_folder, null); textview textview = (textview) custom.findviewbyid(r.id.label_pathfolder); textview.settext(pathdirectory); spinner spinnerlevel = (spinner) custom.findviewbyid(r.id.spinner_level); try{ root.addview(custom); }catch(throwable e) { log.e("burgerclub", "mex: " + e.getmessage()); e.printstacktrace(); }
in way, first custom component added, why?
thanks
edit: i've modified code:
linearlayout.layoutparams params = new linearlayout.layoutparams( linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content); ((viewgroup) root).addview(custom, mycount, params);
this custom component:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/border_bottom" android:layout_marginbottom="2dp" android:paddingright="20dp" android:paddingleft="20dp" style="@style/theme.burgerclubstyle" > <textview android:id="@+id/label_pathfolder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:gravity="center" android:padding="20dp" android:textsize="25sp" style="@style/customtext" /> <spinner android:id="@+id/spinner_level" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_toleftof="@+id/btn_show_desc_img" android:entries="@array/n_level_array" android:padding="20dp" android:prompt="@string/n_level_prompt" android:textalignment="3" style="@style/customtext" /> <imagebutton android:id="@+id/btn_show_desc_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toleftof="@+id/btn_remove_folder" android:layout_marginleft="20dp" android:layout_centervertical="true" android:background="@drawable/button_press" android:contentdescription="@string/showdescimg" android:src="@drawable/ic_desc" /> <imagebutton android:id="@+id/btn_remove_folder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_alignparentright="true" android:layout_centervertical="true" android:background="@drawable/button_press" android:contentdescription="@string/showdescimg" android:src="@drawable/ic_delete" /> </relativelayout>
the first time press button, custom component added, other times not. first custom element not overwrite, it's visible!
where's layout_cartelle_immagini declared?
probably it's horizontal linearlayout, set orientation vertical , code should work (considering code add custom view inside onclicklistener).
Comments
Post a Comment