D
D
Dima13X2014-01-28 13:42:37
Java
Dima13X, 2014-01-28 13:42:37

How to properly extend Android widget classes? How does this affect the XML layout?

I was not satisfied with the standard functionality of the Button widget and I decided to add one field to it:

package com.some.thing;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;

public class ButtonVar extends Button{
  protected boolean correct = false;
  ButtonVar (Context context){
    super(context);
  }
  ButtonVar (Context context, AttributeSet attrs){
    super(context, attrs);
  }
  ButtonVar (Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);
  }
  public void setCorrect (boolean correct){
    this.correct = correct;
  }
  public boolean getCorrect (){
    return this.correct;
  }
}

After that I tweaked the xml-layout:
<com.some.thing.ButtonVar
        android:id="@+id/buttonVar1"
        style="@style/ButtonVars"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="@string/button_var1_default"
        />

But an exception is thrown android.view.InflateException: Binary XML file line on the line: Please help!
<com.some.thing.ButtonVar

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bimeg, 2014-01-28
@Dima13X

Constructors must be public.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question