nmyster
nmyster

Reputation: 452

ImageButton not working within class

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 :/

Upvotes: 0

Views: 91

Answers (3)

Tarsem Singh
Tarsem Singh

Reputation: 14199

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!");

        }
    });
  }
}

Upvotes: 0

Areeb Gillani
Areeb Gillani

Reputation: 450

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 =)

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007584

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.

Upvotes: 2

Related Questions