Note that there are some explanatory texts on larger screens.

plurals
  1. POImageButton not working within class
    text
    copied!<p>I am trying to create a test application that when I press an ImageButton, a log message appears. Simple.</p> <p>I want the ImageButton view to work with a class. </p> <p>I have done this as so: <strong>UPDATED WITH CORRECT NAME OF CONSTRUCTOR</strong></p> <pre><code>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); } } </code></pre> <p>My layout xml is:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;ImageButton android:id="@+id/btButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>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 :/</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload