3iL
3iL

Reputation: 2176

Assign margins to childviews

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.

Upvotes: 0

Views: 321

Answers (2)

A.s.ALI
A.s.ALI

Reputation: 2082

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

Upvotes: 2

Raja
Raja

Reputation: 2815

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);

Upvotes: 1

Related Questions