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?
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);
Define the view in your layout, then in code, show and hide it with
myView.setVisibility(View.VISIBLE)
and myView.setVisibility(View.GONE)
.
//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);
}
});
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);
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With