137
137

Reputation: 831

Restrict visability of a method to everything but corresponding layout

I would like the method saveTeam() from my class FormTeamsActivity.java to only be visible to its container and the corresponding layout file formteam.xml.

FormTeamsActivity.java :

public class FormTeamActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        //...
    }

    public void saveTeam() { //I only want this visible to formTeam.xml
        /*
        Capture user defined parameters and store them in DB
        */
        finish(); //Conclude activity
    }
}

I cant make it private because I would't be able to call in formTeam.xml:

   <Button
    ...
    android:id="@+id/button_formTeam"
    ...
    android:onClick="saveTeam"/><!-- I would like to call the method here -->

The only work around I have found is to write your own actionListener in the class but I would like to be able to get around this to avoid having a ton of listeners in Activity I write.

Upvotes: 0

Views: 45

Answers (1)

cryptochronoconolite
cryptochronoconolite

Reputation: 101

If you want to use android:onClick, it has to be public. From the Android docs:

This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).

Upvotes: 3

Related Questions