-1

I have a relative layout and 'n' number of textviews and edittext . I want the alignment as

  TextView   EditText
       Button
  TextView   EditText
        Button
   TextView   EditText
         Button
  TextView   EditText
        Button
  ..........

I have tried using layout params but I am getting weird results. Here is my code,

 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.linearlayoutid);
    final TextView[] myTextViews = new TextView[41];
    for (int i = 0; i < 41; i++) {
        // create a new textview
        rowTextView = new TextView(this);
        RelativeLayout.LayoutParams paramsA = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        paramsA.addRule(RelativeLayout.RIGHT_OF, rowTextView.getId());
        rowTextView.setId(i);
        // add the textview to the layout
        relativeLayout.addView(rowTextView, paramsA);
        myTextViews[i] = rowTextView;

    }

For Edit Text..

    final EditText[] editTextarray = new EditText[41];
    for (int i = 0; i < 41; i++) {
        // create a new textview
        final EditText editText = new EditText(this); 
        RelativeLayout.LayoutParams paramsB = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
         editText.setText("edit");
        editText.setCursorVisible(true);
        relativeLayout.addView(editText, paramsB);
        editTextarray[i] = editText;

    }

I have applied layout_params like this but it didn't help. Any help would be great!! Thanks

Anusha
  • 939
  • 3
  • 13
  • 31

4 Answers4

1

add the layout

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/ll"
        >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="textview"
            />
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Edittex"
            />

    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll"
        android:layout_centerHorizontal="true"
        android:text="button"
        />

</RelativeLayout>

add this layout at run time:

    View view;
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   for (int i = 0; i < 40; i++) {

     view= inflater.inflate(R.layout.layout, null);
     linearLayout.addView(view);
    }
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
0

I would suggest you to use custom layout and inflate it programmatically.

create a layout consist of TextView , EditText and Button with your desired output and inflate it.

View v=LayoutInflater.from(this).inflate(R.layout.customLayout,null);

and use variable v to refer TextView , EditText and Button.

your customLayout.xml can be something like this :

<LinearLayout android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

Take LinearLayout in your xml having orientation vertical, and add this view in that layout to create different views.

for (int i = 0; i < 20; i++) {
    View v = inflater.inflate(R.layout.customLayout, null);

    ll.addView(v);
}
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • Can you modify my code and provide as a solution ? @Ravi Rupareliya – Anusha May 04 '16 at 10:38
  • sorry, cannot give you whole code, can give you hint about it. If people will downvote for this than also i will accept it. – Ravi May 04 '16 at 10:39
  • LinearLayout lv = (LinearLayout) findViewById(R.id.linearlayoutid); . Should we access linearlayout using id? Ll in your code is the reference right ? – Anusha May 04 '16 at 11:21
  • here ll is LinearLayout in my main layout, where you are going to add all those views. – Ravi May 04 '16 at 11:22
  • Its displaying only one edit text, text view and button but not all @Ravi Rupareliya – Anusha May 04 '16 at 11:37
0

Anusha try this with linearlayout

Still if your design is identical you should use listview best approach.

 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayoutid);
        final TextView[] myTextViews = new TextView[41];
        final EditText[] editTextarray = new EditText[41];
        for (int i = 0; i < 41; i++) {
            // create a new textview
            rowTextView = new TextView(this);
            myTextViews[i] = rowTextView;
            linearLayout.addView(myTextViews[i]);

            final EditText editText = new EditText(this);
            editText.setText("edit");
            editText.setCursorVisible(true);
            editTextarray[i] = editText;
            linearLayout.addView(editTextarray[i]);

        }
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

I suggest you to implement ListView for this requirement -

Per your current approach you will end-up setting multiple onClicklisteners for each of your buttons - if that button is going to pick data of the input associated with the textview/edittext - you will end up writing many array calls and stuff

Listview - onclick of any item you can fire one onClickListener where you differentiate

PS: Bad design to repeat button below textview/edittext if the button is expected to perform same/similar function say saving - just have a listview of textview/edittext - a common button action somewhere on the toolbar or below the form.

takrishna
  • 4,884
  • 3
  • 18
  • 35