silver_rider
silver_rider

Reputation: 65

Extending Button class

Please help me to solve my problem. I'm trying to create a button with some extra functionality. But when I extend class Button, a rendering problem appears :"Custom view GameButton is not using the 2- or 3-argument View constructors; XML attributes will not work". I really have no idea where the problem is, because I add all constructors to my class GameButton:

package ua.oksana.sendev.tictactoe
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.*;

public class GameButton extends Button {
private int parentId;
private int cellId;


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

@TargetApi(Build.VERSION_CODES.L)
public GameButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

public void setParentId(int parentId){
    this.parentId=parentId;
}

public void setCellId(int cellId){
    this.cellId=cellId;
}

public int getParentId(){
    return this.parentId;
}

public int getCellId(){
   return this.cellId;
}

}

And XML:

 <ua.oksana.sendev.tictactoe.tools.GameButton
                    style="@style/cellStyle"
                    android:id="@+id/button99"
                    android:layout_column="3" />

style:

   <style name="cellStyle">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:buttonStyle">?android:attr/buttonStyleSmall</item>
    <item name="android:layout_weight">1</item>
</style>

Upvotes: 1

Views: 6514

Answers (1)

Prem
Prem

Reputation: 4821

Add this line in your button xml

xmlns:android="http://schemas.android.com/apk/res/android"

As below and Try if that works

<ua.oksana.sendev.tictactoe.tools.GameButton
xmlns:android="http://schemas.android.com/apk/res/android"
                    style="@style/cellStyle"
                    android:id="@+id/button99"
                    android:layout_column="3" />

Upvotes: 2

Related Questions