4

I want to have a custom TextView called MyTextView and i want to set a default TextSize in it's constructor only if no TextSize has been set in it's XML definition. How can i detect if a TextSize has been set in it's XML definition?

public class MyTextView extends TextView{    
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //How can i read TextSize from AttribureSet??
        //if no TextSize has been set then SetTextSize(defaultTextSize);
    } 
}

Can any one help me please?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Mansour
  • 445
  • 1
  • 4
  • 14
  • This Question (and Answer) should be just the same as http://stackoverflow.com/questions/8302229/accessing-attrs-in-attributeset-for-custom-components – hata Jan 19 '15 at 13:35

3 Answers3

2

You can obtain style attributes like

public class MyTextView extends TextView{    
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
         String size = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");
    } 
}
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • Thank you, this returns exactly the string which has been set in xml definition like "12.0sp" or null or something like this. Thank you :) – Mansour Jan 20 '15 at 11:20
0

Here's example:

final Resources.Theme theme = context.getTheme();
TypedArray array = theme.obtainStyledAttributes(attrs, R.styleable.YourStylable, R.attr.YourDefStyleAttr, 0);
Fedor Kazakov
  • 601
  • 3
  • 12
0

Try this float size = new TextView(this).getTextSize();

QArea
  • 4,955
  • 1
  • 12
  • 22
  • I think this returns the textSize and cannot recognize it is the default textSize or it has been set in the xml layout file. – Mansour Jan 20 '15 at 11:25