0

I am trying to create a test application that when I press an ImageButton, a log message appears. Simple.

I want the ImageButton view to work with a class.

I have done this as so: UPDATED WITH CORRECT NAME OF CONSTRUCTOR

public class MainActivity extends Activity {


  MyButtonClass btnOk = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        btnOk = new ButtonClass(this);      
        setContentView(R.layout.activity_main);
  }
}


class MyButtonClass extends ImageButton{

        public MyButtonClass(Context context) {
            super(context);

            findViewById(R.id.btButton);

              OnClickListener oclBtnOk = new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                    // change text of the TextView (tvOut)
                   Log.e("Log This:","Yay! I am working!");
                  }
                };

                setOnClickListener(oclBtnOk);
        }



    }

My layout xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/btButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

When I run the app, I don't get any erros on Log cat and the application does not quit however the ImageButton does not do anything when I press it :/

nmyster
  • 454
  • 1
  • 7
  • 20

3 Answers3

2

First, your Java code will not compile, as MyButtonClass is not a valid Java class.

Second, even if you fix your constructor to be MyButtonClass, you will crash at runtime, as you are trying to find a child of the MyButtonClass via findViewById(), and there is no child.

Third, even if it did compile, and even if you did correctly implement the logic in your constructor, you are not referring to MyButtonClass in your layout resource, and so it will not be used.

Fourth, if you take the time to read the code written by Google and other experienced Android developers, you will see that few, if any, would use inheritance this way. You do not need MyButtonClass at all, as your activity or fragment can (and should) set up the listener. In other words, favor composition over inheritance.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

Why are getting in so mess. Your code needs to be trimmed alot. you can try this code

    public class MyAndroidAppActivity extends Activity {

ImageButton imageButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

}

public void addListenerOnButton() {

    imageButton = (ImageButton) findViewById(R.id.imageButton1);

    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

           Toast.makeText(MyAndroidAppActivity.this,
            "ImageButton is clicked!", Toast.LENGTH_SHORT).show();

        }

    });

}

    }

Best of luck =)

Areeb Gillani
  • 440
  • 4
  • 25
  • The reason I wanted to seperate them into classes is so that I can make each button do more later on. I was planning on making a Audio Capture app based on Google's example. So I wanted a RecordButtonClass and a PlayButtonClass that both extend ImageButton. – nmyster Aug 02 '13 at 13:05
  • Ok then give a try to the of **findViewById** like this >>**this= findViewById(yourButtonName);**...... but i prefer you to make functions of your required stuff and call them in onClick method... the simpler the better...... i mean you can create 100s of different functions for different buttons – Areeb Gillani Aug 02 '13 at 13:12
  • you know the theme behind this is **ImageButton ib = (ImageButton)findViewById(R.id.btButton);** is that you are actually referring your xml element to imageButton. You are also casting it into it... but in your code you are not casting or initializing it. I hope it will work – Areeb Gillani Aug 02 '13 at 13:19
0

use

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageButton ib = (ImageButton)findViewById(R.id.btButton);      
        ib.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("Log This:","Yay! I am working!");

        }
    });
  }
}
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71