Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a custom button
    primarykey
    data
    text
    <p>Sorry if this is too basic. I created a new class that extends Button:</p> <pre><code>public class MyButton extends Button { public static final int STATUS_OFF = 0; public static final int STATUS_PLAY1 = 1; public static final int STATUS_PLAY2 = 2; public static final int STATUS_PLAY3 = 3; public int status; private Context ctx; public MyButton(Context context) { super(context); ctx = context; status = STATUS_OFF; super.setBackgroundResource(R.drawable.sound_button_off); } private void click() { switch (status) { case STATUS_OFF: status = STATUS_PLAY1; break; case STATUS_PLAY1: status = STATUS_PLAY2; break; case STATUS_PLAY2: status = STATUS_OFF; break; case STATUS_PLAY3: break; } // OTHER THINGS TO DO } } </code></pre> <p>In my main activity:</p> <pre><code>public class MyActivity extends Activity { private static final int HORIZ = 16; private MyButton[] b = new MyButton[HORIZ]; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rhythm); createButtons(); } private void createButtons() { LinearLayout layout = (LinearLayout)findViewById(R.id.layout_main_linear); for (int w=0; w&lt;HORIZ; w++) { b[w] = new MyButton(MyActivity.this); LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); layout.addView(b[w], p); b[w].setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // HELP ME HERE // WHEN CLICKING THIS BUTTON, IT AUTOMATICALLY SHOULD RUN the private method click(); // IT IS PRIVATE AND SHOULD NOT BE RUN FROM HERE, BUT AUTOMATICALLY EACH TIME THE BUTTON IS CLICKED. } }); } } } </code></pre> <p>How can make it possible that, when I setOnClickListener() from my main class, the click() method is called automatically? In other words, this button is expected to act like any other button, but do something MORE when is clicked. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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