Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom view constructor does not get called on android 5.0.2

Tags:

I created a custom view:

public class SomeView extends View 

The custom view constructors:

public SomeView (Context context) {     super(context); } // Called when view is inflated from xml public SomeView (Context context, AttributeSet attrs) {     super(context, attrs); } // Perform inflation from XML and apply a class-specific base style from a theme attribute. public SomeView (Context context, AttributeSet attrs, int defStyle) {     super(context, attrs, defStyle); } 

I also tried the 4th constructor from api 21 with no luck:

public VeediView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {     super(context, attrs,defStyleAttr, defStyleRes); } 

In the xml layout i am defining this view and things work fine.

Testing on Galaxy S2 works fine and the view constructor are called but when running the app on Nexus-7 android 5.0.2 the constructors do not get called at all.

Any idea why?

Could it be related to rooted devices?

The related xml view:

<com.package.name          android:id="@+id/scene"         android:onClick="startx"         style="@style/txt_money_style"         android:layout_width="72dp"         android:layout_height="72dp"         android:background="@drawable/wtbtn"         android:layout_gravity="right"         android:gravity="center_vertical|right"         /> 
like image 773
Michael A Avatar asked Apr 20 '15 08:04

Michael A


2 Answers

I think you should use this constructor for bestway:

public SomeView (Context context) {     this(context , null); } // Called when view is inflated from xml public SomeView (Context context, AttributeSet attrs) {     this(context, attrs , 0); } // Perform inflation from XML and apply a class-specific base style from a theme attribute. public SomeView (Context context, AttributeSet attrs, int defStyle) {     super(context, attrs, defStyle);     // Initialize customize constructor here } 
like image 88
Hoài Bá Đình Avatar answered Oct 27 '22 01:10

Hoài Bá Đình


In API 21 theres now a 4th constructor it could be that your XML is calling this.

From the docs:

public View (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Added in API level 21

Perform inflation from XML and apply a class-specific base style from a theme attribute or style resource. This constructor of View allows subclasses to use their own base style when they are inflating.

When determining the final value of a particular attribute, there are four inputs that come into play:

  1. Any attribute values in the given AttributeSet.
  2. The style resource specified in the AttributeSet (named "style").
  3. The default style specified by defStyleAttr.
  4. The default style specified by defStyleRes.
  5. The base values in this theme.

Each of these inputs is considered in-order, with the first listed taking precedence over the following ones. In other words, if in the AttributeSet you have supplied , then the button's text will always be black, regardless of what is specified in any of the styles.

Parameters

context The Context the view is running in, through which it can access the current theme, resources, etc. attrs The attributes of the XML tag that is inflating the view. defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults. defStyleRes A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.

like image 20
Numan1617 Avatar answered Oct 27 '22 01:10

Numan1617