1

and i don't know it is possible to do what i want.. (sorry for my english)

<TableRow>
    <Button 
        android:id="@+id/button_left"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="moveLeft" 
        android:layout_column="1"
        android:background="@drawable/bt_left"/>


    <com.pauza.simpletetris.MyView
        android:id="@+id/my_view"
        android:layout_width="250dp"
        android:layout_height="490dp"
        android:layout_gravity="center_horizontal"
        android:layout_column="2"
        android:orientation="vertical"/>

from here i call MyView, but in MyView Button_Left not exist. Game activity:

public class GameActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);
    Button bt_right=(Button)findViewById(R.id.button_right);
    Button bt_left=(Button)findViewById(R.id.button_left);
    bt_right.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View v) {
            moveRight();
        }
        });
    bt_left.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View v) {
            moveLeft();
        }
        });
}

protected void moveLeft() {
    // TODO Auto-generated method stub
    Toast.makeText(this, "You clicked left", Toast.LENGTH_LONG) .show();
}

private void moveRight() {

    //call function from com.pauza.simpletetris.MyView <-- possible????
}

I hope you understand what i mean, thank you for everything.

  • The question title is ambiguous, while the question is not clear what is the problem. Do you want to know how to call an onclick or how to set one from XML? – gunar Sep 06 '13 at 09:26

1 Answers1

1

In order to be able to set onClick calback methods from XML (even though I don't recommend it for maintainability reasons), the methods need to be public, the naming must match exactly and it needs an extra parameter: View. So change moveRight and moveLeft to:

public void moveLeft(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "You clicked left", Toast.LENGTH_LONG) .show();
}

public void moveRight(View v) {

    //call function from com.pauza.simpletetris.MyView <-- possible????
}

In xml layout you will have android:onClick="moveLeft" resp android:onClick="moveRight". In this way you don't need to call setOnClickListener.

Community
  • 1
  • 1
gunar
  • 14,660
  • 7
  • 56
  • 87