I am trying to add an editText
in between two already existing editTexts
programmatically on the click of a button
in android. I am just wondering if this is possible, as i have been unable to find any related questions?

- 2,555
- 20
- 29

- 23
- 4
-
check out: http://stackoverflow.com/questions/20260126/relativelayout-insert-a-view-between-two-others . You can use LayoutParams for other viewgroups(LinearLayout.LayoutParams etc...) – Tudor Jan 31 '17 at 10:07
-
Which layout your using? and post the xml code – Gokul Sreenivasan Jan 31 '17 at 10:08
-
_I am just wondering if this is possible_ This should be find in the documentation of the layout classes. If you didn't tried anything, you are simply asking us to find for you the external resource (documentation) telling if it is possible or not. This is not on-topic. – AxelH Jan 31 '17 at 10:17
-
I have tried something and read the documentation it just wasn't working as I expected it to thats why I asked the question. So _I am just wondering if this is possible_ was probably the wrong phase to use. – BBill Jan 31 '17 at 12:52
5 Answers
you can add 3rd EditText on 2nd position.
For that first you should have reference of the parent layout nad then do like this.
if you have done :
parent.addView(editText1);
parent.addView(editText2);
So now your parent have two child views.
now to add 3rd EditText i.e. editText3
then do this like:
parent.addView(editText3, 1);// addView(<childview>, <index>);
Like this your 3rd EditText will be in 2nd position.

- 132
- 3
- 9
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)yourEditBox.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
params.addRule(RelativeLayout.RIGHT_OF, R.id.id_to_be_right_of);
yourEditBox.setLayoutParams(params);
Use above code to add and align you're editbox between another two editboxes.

- 229
- 2
- 9
Inside Activity class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(linearLayout);
EditText editText = new EditText(getBaseContext());
editText.setHint("Programmatically Added EditText");
linearLayout.addView(editText, 1);
}}
Layout file structure
<?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"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 2" />
</LinearLayout>
Hope this helps. Cheers!

- 97
- 1
- 2
- 9
You can just add this editText between the two in your xml file and controle visibility on your xml and the moment you click on your button.
In xml file :Set visibility to gone or invisible depending on what you actually want : android:visibility="invisible" it won't be visible but it's going to take place in your view android:visibility="gone" it won't be visible and it's not taking place in your view
In your code :
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yourButton.setVisibility(View.VISIBLE);
});

- 23
- 1
- 8
What do you want to acomplish with runtime element adding? Whats the purpose? Maybe isn't enough to show/hide the element on specific actions?
I mean you can make it gone (it will be invisible but also won't use space on the layout) in xml:
android:visibility="gone"
or in java code in the onCreate() method:
specificElement.setVisibility(View.GONE)
Then when you normally would add the element you rather just set the visibility to visible:
specificElement.setVisibility(View.VISIBLE)
What about that?

- 439
- 3
- 11
-
Thanks that will actually work perfectly. I was just trying to save time in terms of having to create more elements manually. – BBill Jan 31 '17 at 12:51