java - android add TextView on click of (+) button with setText as 1and in next 2 and so on -
i have 1 textview , 2 edittext when clicking on plus button textview , 2 edittext should added ,and able .my problem when adding textview text should set 1 when textview added again text should set 2 , on .i not able set text using t1.settext(i).here code-
mainactivity.java import android.os.bundle; import android.app.activity; import android.view.menu; import android.widget.button; import android.widget.imagebutton; public class mainactivity extends activity { button btndisplay; imagebutton btnadd; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btnadd = (imagebutton) findviewbyid(r.id.btnadd); btndisplay = (button) findviewbyid(r.id.btndisplay); mylayoutoperation.add(this, btnadd); mylayoutoperation.display(this, btndisplay); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } } mylayoutoperation.java import android.app.activity; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.view.viewgroup.layoutparams; import android.widget.button; import android.widget.edittext; import android.widget.imagebutton; import android.widget.linearlayout; import android.widget.textview; import android.widget.toast; import java.util.arraylist; public class mylayoutoperation { public static void display(final activity activity, button btn) { btn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { linearlayout scrollviewlinerlayout = (linearlayout) activity.findviewbyid(r.id.linearlayoutform); java.util.arraylist<string> msg = new arraylist<string>(); (int = 0; < scrollviewlinerlayout.getchildcount(); i++) { linearlayout innerlayout = (linearlayout) scrollviewlinerlayout.getchildat(i); textview t2=(textview)innerlayout.findviewbyid(r.id.t1); t2.settext(i); edittext e1=(edittext)innerlayout.findviewbyid(r.id.e1); edittext edit = (edittext) innerlayout.findviewbyid(r.id.editdes); msg.add(t2.gettext().tostring()); msg.add(e1.gettext().tostring()); msg.add(edit.gettext().tostring()); } toast t = toast.maketext(activity.getapplicationcontext(), msg.tostring(), toast.length_short); t.show(); } }); } public static void add(final activity activity, imagebutton btn) { final linearlayout linearlayoutform = (linearlayout) activity.findviewbyid(r.id.linearlayoutform); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { final linearlayout newview = (linearlayout)activity.getlayoutinflater().inflate(r.layout.rowdetail, null); newview.setlayoutparams(new layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content)); imagebutton btnremove = (imagebutton) newview.findviewbyid(r.id.btnremove); btnremove.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { linearlayoutform.removeview(newview); } }); linearlayoutform.addview(newview); } }); } }
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layoutteste" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".mainactivity"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:id="@+id/textview1" android:layout_width="95dp" android:layout_height="fill_parent" android:layout_alignbottom="@+id/btnadd" android:layout_alignparentleft="true" android:layout_toleftof="@+id/btnadd" android:gravity="center_vertical|center_horizontal" android:text="@string/titletecnologies" android:textappearance="?android:attr/textappearancelarge" /> <imagebutton android:id="@+id/btnadd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:contentdescription="@string/btnadd" android:src="@android:drawable/ic_input_add" /> </relativelayout> <scrollview android:id="@+id/scrollview1" android:layout_width="match_parent" android:layout_height="275dp"> <linearlayout android:id="@+id/linearlayoutform" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> </linearlayout> </scrollview> <button android:id="@+id/btndisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btndisplay" /> </linearlayout>
rowdetails.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rowdetail" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:layout_marginleft="10dp" android:id="@+id/t1"/> <requestfocus /> <edittext android:id="@+id/e1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.62" android:ems="10" android:inputtype="text"> <requestfocus /> </edittext> <edittext android:id="@+id/editdes" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.62" android:ems="10" android:inputtype="text"> <requestfocus /> </edittext> <imagebutton android:id="@+id/btnremove" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentdescription="@string/btnremove" android:src="@android:drawable/ic_delete" /> </linearlayout>
got answer
t2.settext(i+".");
Comments
Post a Comment