Jordan Little
Jordan Little

Reputation: 119

How to use android:onClick?

I'm new to android programming. How do you use android:onClick? Where do I put the method I want to call?

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

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Button"
        android:onClick="doSomething"/>
<ImageView
    android:id="@+id/icon"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:src="@drawable/molecule" />

Would I put it in the .java file that calls for this layout to be made?

Upvotes: 1

Views: 2989

Answers (2)

Raz
Raz

Reputation: 9058

Yes you need to put it in the activity class where you setContentView this layout. The method for the onclick should be in the form of this:

public void doSomething(View v) {
}

Upvotes: 6

323go
323go

Reputation: 14274

I assume you worked out how to set the content in your activity? In that same activity, add:

public void doSomething( View view ) {
    // onClick code goes here.
}

Upvotes: 2

Related Questions