Question

I've found a piece of code for making toast message. As a new android developer, i know that, we have a listener to make a button working. But here there is no listener. So why this code is working?

 public class MainActivity extends Activity {
private String mButtonMessageTemplate;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mButtonMessageTemplate=getString(R.string.button_messege_template);
}
public void showButtonText(View clickedButton){
Button button=(Button) clickedButton;
CharSequence text=button.getText();
String message=String.format(mButtonMessageTemplate, text);
showToast(message);

}

public void showToast(String text) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();      

}

Another question is

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hi_button_lebel" 
        android:onClick="showButtonText"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bye_button_lebel"
        android:onClick="showButtonText" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/yo_button_lebel"
        android:onClick="showButtonText" />

android:onClick="showButtonText" this is using a non-onClickListener method! How?? Please give me a detail answer. Thanks in advance. :)

Was it helpful?

Solution

 android:onClick="showButtonText"

Thats your listener. when that view is clicked, showButtonText method will execute.

This is the View.OnClickListener in xml code. When its compiled it will turn into java code that you are fimialiar with.

NOTE: This is a bad habit to put onclickListener in xml code. What you should do is:

button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
    public void onclick(View v){
    //do what happens on click of button1
        showToast("button1 clicked.");

    }
});

OTHER TIPS

Android lets you use the android:onClick property to atoumatically create a listener that calls that method in your Activity class.

The method must receive an argument of type View in order for this to work.

All your buttons have onClick attribute set to call the method

showButtonText(View clickedButton)

This method has a statement in it to display the toast.

showToast(message);

So everytime you click any of your buttons, it displays the toast.

Here are codes in View.java.
If a view has "onClick" attribute, View's constructor creates and registers "OnClickListener".

public View(Context context, AttributeSet attrs, int defStyle) {
....
    case R.styleable.View_onClick:
    if (context.isRestricted()) {
        throw new IllegalStateException("The android:onClick attribute cannot "
                + "be used within a restricted context");
    }   

    final String handlerName = a.getString(attr);
    if (handlerName != null) {
        setOnClickListener(new OnClickListener() {
            private Method mHandler;

            public void onClick(View v) {
                if (mHandler == null) {
                    try {
                        mHandler = getContext().getClass().getMethod(handlerName,
                                View.class);
                    } catch (NoSuchMethodException e) {
                        int id = getId();
                        String idText = id == NO_ID ? "" : " with id '"
                                + getContext().getResources().getResourceEntryName(
                                    id) + "'";
                        throw new IllegalStateException("Could not find a method " + 
                                handlerName + "(View) in the activity "
                                + getContext().getClass() + " for onClick handler"
                                + " on view " + View.this.getClass() + idText, e); 
                    }   
                }   

                try {
                    mHandler.invoke(getContext(), View.this);
                } catch (IllegalAccessException e) {
                    throw new IllegalStateException("Could not execute non "
                            + "public method of the activity", e); 
                } catch (InvocationTargetException e) {
                    throw new IllegalStateException("Could not execute "
                            + "method of the activity", e); 
                }     
            }             
        });               
    }   
    break;
....
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top