How can I make the Views defined in Android XML customs view file visible -
i've below code, having 1 element shown in xml file below it, , call of custom view
named card
package tk.zillion.app1; import tk.zillion.app1.customviews.card; public class emptyactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_empty); relativelayout rl; rl = (relativelayout) findviewbyid(r.id.activity_empty); rl.addview(new card(this)); } }
below xml file, , how layout appear in android studio:
the card
custom view below code , xml file:
package tk.zillion.app1.customviews; import tk.zillion.app1.r; public class card extends relativelayout { public card(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(context); } public card(context context, attributeset attrs){ super(context, attrs); init(context); } public card(context context) { super(context); init(context); } private void init(context context) { windowmanager windowmanager = (windowmanager) this.getcontext() .getsystemservice(context.window_service); display display = windowmanager.getdefaultdisplay(); point size = new point(); display.getsize(size); int width = size.x; int height = size.y; relativelayout.layoutparams lp = new relativelayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content);; relativelayout rl = new relativelayout(this.getcontext()); rl.setbackground(context.getdrawable(r.drawable.layer_card_background)); rl.setminimumwidth(width); button btn = new button(this.getcontext()); // btn.setid(r.id.titleid); btn.setid(view.generateviewid()); textview 1 = new textview(this.getcontext()); one.settext("device width is: "+string.valueof(width)+", device height is: "+string.valueof(height)); one.settextalignment(view.text_alignment_center); btn.settext(r.string.custom); btn.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { int duration = toast.length_short; charsequence text = "hello toast!"; toast toast = toast.maketext(v.getcontext(), text, duration); toast.show(); } }); rl.addview(btn); lp.addrule(relativelayout.below, btn.getid()); rl.addview(one, lp); // or one.setlayoutparams(lp); rl.addview(one); // lp.removerule(relativelayout.below); this.addview(rl); } }
and xml file is:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_view" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <tk.zillion.app1.customviews.card android:id="@+id/custom_card" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" /> </tk.zillion.app1.customviews.card> </relativelayout>
and appeared in android studio as:
below how app looks @ execution (right side), , how should (left side). view defined in xml file, my first app
textview, not appearing @ execution, while other elements defined pragmatically visible.
i tried use inflate
did not work me
my question how can view, ones defined in xml file visible?
try xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_view" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <textview android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" /> <tk.zillion.app1.customviews.card android:id="@+id/custom_card" android:layout_width="match_parent" android:layout_height="wrap_content"/> </linearlayout>
Comments
Post a Comment