Setting add-ons for text viewing does not work

I am trying to implement tags through textview. I type something in EditText and click a button to add a tag. I programmatically add an add-on and a rectangle to the text box, but setting up add-ons has no effect. How to add an add-on to make it work?

public void tag_it_callback (View view) { Log.v("actv", "tag it callback"); LinearLayout main_layout = (LinearLayout) findViewById(R.id.main_layout); TextView tv = new TextView(this); tv.setText( ((EditText) findViewById(R.id.textfield)).getText() ); main_layout.addView(tv); transform_tag(tv); } public void transform_tag(TextView tv) { // set padding and background int padding = 10; tv.setPaddingRelative(20, 20, 20, 20); tv.setPadding(20, 20, 20, 20); tv.setBackgroundResource(R.drawable.border); // set layout width and height LayoutParams params= (LayoutParams) tv.getLayoutParams(); Log.v("actv", "params "+params); if ( params != null ) { params.width=LayoutParams.WRAP_CONTENT; params.height=LayoutParams.WRAP_CONTENT; } else { params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } tv.setLayoutParams(params) } 

border.xml is as follows

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="#FF0000" /> <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" android:topLeftRadius="8dp" android:topRightRadius="8dp" /> </shape> </item> <item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp"> <shape android:shape="rectangle"> <solid android:color="#000000" /> </shape> </item> </layer-list>"`<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="#FF0000" /> <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" android:topLeftRadius="8dp" android:topRightRadius="8dp" /> </shape> </item> <item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp"> <shape android:shape="rectangle"> <solid android:color="#000000" /> </shape> </item> </layer-list>` 
+4
source share
2 answers

I finally found the problem First I have to set the background before setting up the add-on. Installing a pad and then setting the background does not work.

 // does not work tv.setPadding(20, 20, 20, 20); tv.setBackgroundResource(R.drawable.border); // works tv.setBackgroundResource(R.drawable.border); tv.setPadding(20, 20, 20, 20); 
+62
source
 tv.setPadding(20, 20, 20, 20); tv.setBackgroundResource(R.drawable.border); 
0
source

Source: https://habr.com/ru/post/1497919/


All Articles