setting padding for textview not working

15,062

I finally found the problem I have to set the background first before setting the padding. Setting the padding then setting the background doesn't 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);
Share:
15,062

Related videos on Youtube

Teong Leong
Author by

Teong Leong

Updated on August 31, 2022

Comments

  • Teong Leong
    Teong Leong over 1 year

    I'm trying to implement tags through textview. I type in something to a EditText and press a button to add a tag. I'm programmatically adding padding and rectangle background to the textview but the padding setting does not have any effect. How do I add the padding such that it works?

    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 goes like this

    <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>`
    
  • Teong Leong
    Teong Leong over 10 years
    That doesn't work. I do not see the padding. The background border is still very close to the text.
  • Guilherme Torres Castro
    Guilherme Torres Castro almost 10 years
    This has been fixed on Android KitKat (maybe in early version)
  • NSouth
    NSouth over 8 years
    Thanks! This had me stumped for a while, as my padding was not being applied on Android 4.3. Turns out it was because I was setting the color of the view after setting the padding.
  • Konstantin Berkov
    Konstantin Berkov over 7 years
    I've encountered this bug in Nougat emulator. Very weird.
  • Marilia
    Marilia over 7 years
    I had a similar issue testing on a Samsung Galaxy S3 with Android 4.2.2. Even tho the padding was already set in the xml layout, when later setting the background programmatically the padding was being ignored. I ended up fixing by setting both background and padding programmatically only and in the right order as suggested in this answer.