Reputation:
I have a layout with a TextView
inside a Linearlayout
which is inflating on selecting a item from a spinner.
The result is like this:
What I want it to be is like this:
This is my tag_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tag_background"
android:gravity="center"
android:layout_marginLeft="5dp"
android:paddingBottom="6dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="6dp">
<TextView
android:id="@+id/chip_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#Tag2"
android:textColor="@android:color/white"
android:textSize="14sp" />
</LinearLayout>
This is my Linear Layout in which this view will be inflated:
<LinearLayout
android:id="@+id/tag_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tag_background"
android:orientation="horizontal"
android:gravity="center"
android:layout_below="@+id/issuesStrengthsBrandspinner"
android:paddingBottom="4dp"
android:layout_marginLeft="20dp"
android:paddingTop="4dp" />
This is my background:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorPrimaryDark"/>
<corners android:radius="20dp"/>
</shape>
and this is how I'm infalting the view:
LinearLayout linearLayout = (LinearLayout) dialog2.findViewById(R.id.tag_container);
for (HashMap.Entry<Integer, String> entry : issuesStrengthsbrandNameIDMap.entrySet()){
View view2 = getLayoutInflater().inflate(R.layout.tag_layout, null);
Log.i("NAME", "onItemClick: "+entry.getValue());
((TextView) view2.findViewById(R.id.chip_text)).setText(entry.getValue());
linearLayout.addView(view2);
}
Upvotes: 0
Views: 1129
Reputation: 69689
Try this set Margin Programatically
for (HashMap.Entry<Integer, String> entry : issuesStrengthsbrandNameIDMap.entrySet()){
View view2 = getLayoutInflater().inflate(R.layout.tag_layout, null);
Log.i("NAME", "onItemClick: "+entry.getValue());
TextView tv= (TextView) view2.findViewById(R.id.chip_text)).setText(entry.getValue();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(10,0,10,0); //setMargins(left, top, right, bottom);
tv.setLayoutParams(params);
linearLayout.addView(tv);
}
Upvotes: 1
Reputation: 3976
Remove background attribute from root linear layout and add in text-view. this will create separate text.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="5dp"
android:paddingBottom="6dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="6dp">
<TextView
android:id="@+id/chip_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#Tag2"
android:background="@drawable/tag_background"
android:marginLeft="8dp"
android:marginRight="8dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
</LinearLayout>
Happy coding!!
Upvotes: 0
Reputation: 16399
Give margin to your TextView
android:marginLeft="8dp"
android:marginRight="8dp"
Eg.
<TextView
android:id="@+id/chip_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:marginLeft="8dp"
android:marginRight="8dp"
android:text="#Tag2"
android:textColor="@android:color/white"
android:textSize="14sp" />
Upvotes: 0