0

I'm inflating a view which just has a TextView in it. The parent view is a LinearLayout. My parent view looks like this:

        <LinearLayout
            android:id="@+id/posmSelectedBrandsLL"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_below="@+id/brandPOSMspinner"/>

and my child view looks like this:

<TextView
    android:id="@+id/chip12345"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/tag_background"
    android:text="TAG"
    android:layout_marginTop="50dp"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:textColor="@color/textcolorLogin"
    android:textSize="22sp" />

This is how I'm inflating view:

        final LinearLayout editParentLL = (LinearLayout) findViewById(R.id.posmSelectedBrandsLL);
        final View editChildView = getLayoutInflater().inflate(R.layout.tag_layout, null);
        editParentLL.addView(editChildView);

        TextView tvChip = editChildView.findViewById(R.id.chip12345);
        tvChip.setText(p.getProductName());

And the result that comes out is like this:

enter image description here

What I want is to separate the three TextViews from one another. So instead of a single box, it should come out as three different boxes.

I need your help to do it. Thankyou.

3iL
  • 2,146
  • 2
  • 23
  • 47
  • you are inflating single textview ? and you get different values in the same one or you are using three textviews for 3 different values ? – Umair Nov 07 '17 at 06:55
  • @Umair, I'm inflating a single textview. Actually I'm doing inflating the child view on a button click so as many times user clicks the button the child view is inflated with different values depending on the item clicked. – 3iL Nov 07 '17 at 07:02
  • You can inflate multiple child views in a single parent and set the margins programmatically. See the marked answer below. – 3iL Nov 07 '17 at 07:16
  • ah sorry I misunderstood your question completely ... :) – Umair Nov 07 '17 at 07:18
  • It seems that since you're using `null` for the `viewRoot` parameter in the call to `inflate` the layout parameters are being ignored. See https://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int, android.view.ViewGroup) – frozenkoi Nov 07 '17 at 07:26
  • 1
    Related question: https://stackoverflow.com/questions/25632756/cardview-has-lost-margin-when-inflating and see also https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/ – frozenkoi Nov 07 '17 at 07:32

2 Answers2

2

try this

final View editChildView = getLayoutInflater().inflate(R.layout.view_add_item_with_details, null);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10,2,10,2);
        editChildView.setLayoutParams(layoutParams);
        editParentLL.addView(editChildView);

        TextView tvChip = editChildView.findViewById(R.id.chip12345);
        tvChip.setText(p.getProductName());

you can set any values in .setMargin method

A.s.ALI
  • 1,992
  • 3
  • 22
  • 54
1

You should use LayoutParams to set your editChildView margins:

LayoutParams params = new LayoutParams(
        LayoutParams.WRAP_CONTENT,      
        LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
editChildView.setLayoutParams(params);

more details reffer here: https://android--code.blogspot.in/2015/05/android-textview-layout-margin.html

if you need TextView in One by one, You have to use like Orientation

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
Raja
  • 2,775
  • 2
  • 19
  • 31