android - How to add a TextView between Buttons -
i'm trying on button click dynamically add textview comments associated button. have button , b. want(when click on a) add text/textview in between button , b.
here xml file:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <button android:id="@+id/a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:text="a" /> <button android:id="@+id/b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/a" android:layout_margintop="5dp" android:text="b" /> <textview android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this test...." android:visibility="invisible" /> </relativelayout>
here code tried:
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final button a, b , c; final textview txt; final relativelayout layout; layout = (relativelayout) findviewbyid(r.id.layout); = (button) findviewbyid(r.id.a); b = (button) findviewbyid(r.id.b); txt = (textview) findviewbyid(r.id.txt); a.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { relativelayout.layoutparams p = new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); p.addrule(relativelayout.below, a.getid()); layout.addview(txt, p); setcontentview(layout); } }); } }
i have following error message: specified child has parent. must call removeview() on child's parent first.
you can try.
textview textview; a.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { relativelayout.layoutparams p = new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); p.addrule(relativelayout.below, a.getid()); textview = new textview(mainactivity.this); layout.addview(textview, p); } });
than can set text on it.
Comments
Post a Comment