adding textfield on the fly

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

adding textfield on the fly

Postby xtravanta » Mon Jun 06, 2011 6:50 pm

Hi,

iam trying to add some new text fields after somebody pressed the button "add new item" is this possible?

Greets
xtravanta
Freshman
Freshman
 
Posts: 2
Joined: Mon Jun 06, 2011 6:47 pm

Top

Re: adding textfield on the fly

Postby klonk » Mon Jun 06, 2011 10:25 pm

klonk
Developer
Developer
 
Posts: 37
Joined: Fri May 06, 2011 4:41 pm

Re: adding textfield on the fly

Postby xtravanta » Tue Jun 07, 2011 9:04 am

But isnt this all done before rendering the views ?

Like addviews and then setcontentview() and i need to do it after the set content view.
xtravanta
Freshman
Freshman
 
Posts: 2
Joined: Mon Jun 06, 2011 6:47 pm

Re: adding textfield on the fly

Postby milde » Sat Jun 11, 2011 11:08 am

Hi, check out this piece of code, it adds a Textfield each time you press a button.
You need to call setContencView() before you can access the layout and the button in your .xml file.

Here is the content of my sap.xml file:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/myLayout"
  android:layout_width="match_parent" android:layout_height="match_parent">
    <Button android:text="Add Textfield" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


and the code...

Code: Select all
public class Sup extends Activity {
   EditText txt;
   LinearLayout ll;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.sap);
      ll = (LinearLayout) findViewById(R.id.myLayout);
      ll.setOrientation(LinearLayout.VERTICAL);
      Button b = (Button) findViewById(R.id.button1);
      b.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {
            txt = new EditText(getApplicationContext());
            txt.setLayoutParams(new LayoutParams(
                     LayoutParams.FILL_PARENT,
                     LayoutParams.WRAP_CONTENT));
            ll.addView(txt);
         }
      });
   }
}


hope it helps
milde
Freshman
Freshman
 
Posts: 2
Joined: Fri Jun 10, 2011 11:22 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests