The android.content.res.TypedArray.getDimensionPixelSize() method is used to retrieve the size of a dimension in pixels from a TypedArray. This method returns the pixel size that represents the value of the attribute. The package library for this method is android.content.res.
Example 1: In this example, we will retrieve the size of a dimension from the TypedArray using getDimensionPixelSize() method.
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView); int padding = ta.getDimensionPixelSize(R.styleable.CustomView_custom_padding, 0); ta.recycle();
In the above example, we obtain a TypedArray and get the value of the custom_padding attribute using the getDimensionPixelSize() method. The second parameter is used to set a default value in case the attribute is not defined.
Example 2: In this example, we will retrieve the text size from a TextView using getDimensionPixelSize() method.
int textSize = textView.getPaint().getTextSize(); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView); float customTextSize = ta.getDimensionPixelSize(R.styleable.CustomTextView_custom_textSize, (int)textSize); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, customTextSize); ta.recycle();
In the above example, we get the current text size of TextView and set it to textSize variable. After that, we obtain a TypedArray and get the value of the custom_textSize attribute using the getDimensionPixelSize() method. The third parameter is used to set a default value in case the attribute is not defined. Finally, we set the customTextSize value to TextView using setTextSize() method with TypedValue.COMPLEX_UNIT_PX unit.
Overall, the android.content.res.TypedArray.getDimensionPixelSize() method is useful when we need to get the size of a dimension in pixels from a TypedArray. We can use this method in many different situations, such as custom view or custom text view.
Java TypedArray.getDimensionPixelSize - 30 examples found. These are the top rated real world Java examples of android.content.res.TypedArray.getDimensionPixelSize extracted from open source projects. You can rate examples to help us improve the quality of examples.