Cody Cody - 9 days ago 5x
Java Question

Android: Cannot Resolve Method

I'm trying to program custom states for programatically generated buttons in my app using the process explained here, but i'm getting the "cannot resolve method" compiler error when I try to call a method in my custom button class. The method i'm trying to call is public so I'm not sure why it's not visible in the scope i'm trying to call it from. Thoughts?

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.menu_fragment, container, false);
typeOneArray = new boolean[21];
typeTwoArray = new int[21];
//programmatic button generation
buttonHolder = (LinearLayout) v.findViewById(;
for (int j = 0; j < 21; j++) {
final int iteration = j;
final Button btnTag = new CustomButton(getActivity()); //CUSTOM BUTTON INSTANTIATION
btnTag.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) { //button was clicked
typeTwoArray[iteration] = typeTwoArray[iteration] + 1;
Handler handler = new Handler();
Runnable singleClickLogic = new Runnable() {
public void run() {
if (typeTwoArray[iteration] !=0) {
Toast.makeText(getActivity(), "single click", Toast.LENGTH_SHORT).show();
typeTwoArray[iteration] = 0;
//Double click
if (typeTwoArray[iteration] == 2) {
btnTag.setButtonTypeOne(true); //THIS IS WHERE THE ERROR HAPPENS

Toast.makeText(getActivity(), "double click", Toast.LENGTH_SHORT).show();
typeTwoArray[iteration] = 0;
//Single click
else if (typeTwoArray[iteration] == 1) {
handler.postDelayed(singleClickLogic, 250);
} else if (event.getAction() == MotionEvent.ACTION_UP) {

return false;
return v;

public class CustomButton extends Button {
private boolean stateTwo = false;
private boolean stateOne = false;
private static final int[] STATE_ONE = {R.attr.state_one};
private static final int[] STATE_TWO = {R.attr.state_two};

public CustomButton(Context context) {

public void setButtonTypeOne(boolean buttonState) {
stateOne = buttonState;
public void setButtonTypeTwo(boolean buttonState) {
stateTwo = buttonState;

protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
if (stateOne) {
mergeDrawableStates(drawableState, STATE_ONE);
if (stateTwo) {
mergeDrawableStates(drawableState, STATE_TWO);
return drawableState;



Though the btnTag instance is a CustomButton, you've declared btnTag as just a regular Button.

final Button btnTag = new CustomButton(getActivity());

Button doesn't have your custom methods, so the compiler can't find setButtonTypeOne().

You can either declare btnTag as your CustomButton type:

final CustomButton btnTag = ...

Or cast it to that type before calling the method on it:

((CustomButton) btnTag).setButtonTypeOne(true);