Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display an edit text box on click of a button

I am creating a user form in android. I want to display an edit text box on click of a button. below that button, while simultaneously the contents originally present below that button to move more down.
How can this be done?

like image 294
Ashwin Avatar asked Mar 06 '12 07:03

Ashwin


5 Answers

If you just want to "display an edit text box on click of a button" why don't you just..

Keep the EditText in your XML layout file for that activity below the Button where you want it..

XML set it's

android:visibility = "gone"

and making instance of that

EditText et=(EditText)findViewById(R.id.thatEditText);

in activity...in your button click event set

et.setVisibility(View.VISIBLE);

like image 57
MKJParekh Avatar answered Nov 16 '22 13:11

MKJParekh


Define the view in your layout, then in code, show and hide it with myView.setVisibility(View.VISIBLE) and myView.setVisibility(View.GONE).

like image 3
Sparky Avatar answered Nov 16 '22 14:11

Sparky


    //xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<Button 
    android:id="@+id/btn"
  android:layout_width="fill_parent"
    android:layout_height="wrap_content"  
    android:text="Button"
    />
<EditText 
   android:id="@+id/edtbox"
  android:layout_width="fill_parent"
    android:layout_height="wrap_content"    


    />
</LinearLayout>


//Activity

//oncreate

editText.setVisibility(View.GONE);

btn..setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    editText.setVisibility(View.VISIBLE);
                }
            });
like image 3
Pradeep Sodhi Avatar answered Nov 16 '22 13:11

Pradeep Sodhi


If your layout is relative then addView(yourView, index) doesn't work. Suppose you want to add view after some other control and reference to that control.

e.g.

<RelativeLayout android:id="@+id/templayout">
   <Button android:id="@+id/button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="add"/>

and you want to add edit text control after text View then on button click :

RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.templayout);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    params.addRule(RelativeLayout.BELOW, R.id.title);
    EditText yourEditText = new EditText(this);
    relativeLayout.addView(yourEditText, params);
like image 1
Hiren Dabhi Avatar answered Nov 16 '22 13:11

Hiren Dabhi


Define your EditText in your xml and hide it. On button click, change its visibility to View.Visible.

 YourEditText.setVisibility(View.GONE);

 btn.setOnClickListener(new View.OnClickListener() 
     {
        public void onClick(View v)
         {              
          YourEditText.setVisibility(View.VISIBLE);
          }
       });
like image 1
Rashmi.B Avatar answered Nov 16 '22 15:11

Rashmi.B